1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h"
18 #include <memory>
19 
20 #include "perfetto/ext/base/status_or.h"
21 #include "perfetto/trace_processor/basic_types.h"
22 #include "src/base/test/status_matchers.h"
23 #include "src/trace_processor/db/table.h"
24 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
25 #include "src/trace_processor/storage/trace_storage.h"
26 #include "test/gtest_and_gmock.h"
27 
28 namespace perfetto::trace_processor {
29 namespace {
30 
TEST(Ancestor,SliceTableNullConstraint)31 TEST(Ancestor, SliceTableNullConstraint) {
32   // Insert a row to make sure that we are not returning an empty table just
33   // because the source is empty.
34   TraceStorage storage;
35   storage.mutable_slice_table()->Insert({});
36 
37   Ancestor generator{Ancestor::Type::kSlice, &storage};
38 
39   // Check that if we pass start_id = NULL as a constraint, we correctly return
40   // an empty table.
41   base::StatusOr<std::unique_ptr<Table>> res =
42       generator.ComputeTable({SqlValue()});
43   ASSERT_OK(res);
44   ASSERT_EQ(res->get()->row_count(), 0u);
45 }
46 
TEST(Ancestor,CallsiteTableNullConstraint)47 TEST(Ancestor, CallsiteTableNullConstraint) {
48   // Insert a row to make sure that we are not returning an empty table just
49   // because the source is empty.
50   TraceStorage storage;
51   storage.mutable_stack_profile_callsite_table()->Insert({});
52 
53   Ancestor generator{Ancestor::Type::kStackProfileCallsite, &storage};
54 
55   // Check that if we pass start_id = NULL as a constraint, we correctly return
56   // an empty table.
57   base::StatusOr<std::unique_ptr<Table>> res =
58       generator.ComputeTable({SqlValue()});
59   ASSERT_OK(res);
60   ASSERT_EQ(res->get()->row_count(), 0u);
61 }
62 
TEST(Ancestor,SliceByStackTableNullConstraint)63 TEST(Ancestor, SliceByStackTableNullConstraint) {
64   // Insert a row to make sure that we are not returning an empty table just
65   // because the source is empty.
66   TraceStorage storage;
67   storage.mutable_slice_table()->Insert({});
68 
69   Ancestor generator{Ancestor::Type::kSliceByStack, &storage};
70 
71   // Check that if we pass start_id = NULL as a constraint, we correctly return
72   // an empty table.
73   base::StatusOr<std::unique_ptr<Table>> res =
74       generator.ComputeTable({SqlValue()});
75   ASSERT_OK(res);
76   ASSERT_EQ(res->get()->row_count(), 0u);
77 }
78 
79 }  // namespace
80 }  // namespace perfetto::trace_processor
81