1 /*
2 * Copyright (C) 2020 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/sqlite/sqlite_utils.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 namespace perfetto {
22 namespace trace_processor {
23 namespace {
24
25 class GetColumnsForTableTest : public ::testing::Test {
26 public:
GetColumnsForTableTest()27 GetColumnsForTableTest() {
28 sqlite3* db = nullptr;
29 PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK);
30 PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK);
31 db_.reset(db);
32 }
33
PrepareValidStatement(const std::string & sql)34 void PrepareValidStatement(const std::string& sql) {
35 int size = static_cast<int>(sql.size());
36 sqlite3_stmt* stmt;
37 ASSERT_EQ(sqlite3_prepare_v2(*db_, sql.c_str(), size, &stmt, nullptr),
38 SQLITE_OK);
39 stmt_.reset(stmt);
40 }
41
RunStatement(const std::string & sql)42 void RunStatement(const std::string& sql) {
43 PrepareValidStatement(sql);
44 ASSERT_EQ(sqlite3_step(stmt_.get()), SQLITE_DONE);
45 }
46
47 protected:
48 ScopedDb db_;
49 ScopedStmt stmt_;
50 };
51
TEST_F(GetColumnsForTableTest,ValidInput)52 TEST_F(GetColumnsForTableTest, ValidInput) {
53 RunStatement("CREATE TABLE foo (name STRING, ts INT, dur INT);");
54 std::vector<SqliteTable::Column> columns;
55 auto status = sqlite_utils::GetColumnsForTable(*db_, "foo", columns);
56 ASSERT_TRUE(status.ok());
57 }
58
TEST_F(GetColumnsForTableTest,UnknownType)59 TEST_F(GetColumnsForTableTest, UnknownType) {
60 // Currently GetColumnsForTable does not work with tables containing types it
61 // doesn't recognise. This just ensures that the query fails rather than
62 // crashing.
63 RunStatement("CREATE TABLE foo (name NUM, ts INT, dur INT);");
64 std::vector<SqliteTable::Column> columns;
65 auto status = sqlite_utils::GetColumnsForTable(*db_, "foo", columns);
66 ASSERT_FALSE(status.ok());
67 }
68
69 } // namespace
70 } // namespace trace_processor
71 } // namespace perfetto
72