2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
21 #include <rua_stat_internal.h>
26 #include "sqlite3_mock.h"
27 #include "test_fixture.h"
30 using ::testing::DoAll;
31 using ::testing::SetArgPointee;
32 using ::testing::Return;
34 static int __rua_stat_tag_iter(const char* stat_tag, void* user_data) {
38 class StatMocks : public ::testing::NiceMock<SqlMock> {};
40 class RuaStatTest : public TestFixture {
42 RuaStatTest() : TestFixture(std::make_unique<StatMocks>()) {}
43 virtual ~RuaStatTest() {}
45 virtual void SetUp() {
48 virtual void TearDown() {
53 TEST_F(RuaStatTest, rua_stat_update_for_uid_N) {
54 int ret = rua_stat_update_for_uid(const_cast<char*>("caller"),
55 const_cast<char*>("tag"), 5001);
59 TEST_F(RuaStatTest, rua_stat_get_stat_tags) {
60 char** table = (char** )calloc(10, sizeof(char*));
61 ASSERT_TRUE(table != nullptr);
62 sqlite3* db = (sqlite3*) table;
64 EXPECT_CALL(GetMock<SqlMock>(),
65 sqlite3_open_v2(_, _, _, _)).
66 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
67 EXPECT_CALL(GetMock<SqlMock>(),
68 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
69 EXPECT_CALL(GetMock<SqlMock>(),
70 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
71 EXPECT_CALL(GetMock<SqlMock>(),
72 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
73 EXPECT_CALL(GetMock<SqlMock>(),
74 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
75 EXPECT_CALL(GetMock<SqlMock>(),
76 sqlite3_finalize(_)).WillOnce(Return(0));
78 int ret = rua_stat_get_stat_tags(const_cast<char*>("caller"),
79 __rua_stat_tag_iter, nullptr);
85 TEST_F(RuaStatTest, rua_stat_db_update) {
86 char** table = (char** )calloc(10, sizeof(char*));
87 ASSERT_TRUE(table != nullptr);
88 sqlite3* db = (sqlite3*) table;
90 EXPECT_CALL(GetMock<SqlMock>(),
91 sqlite3_open_v2(_, _, _, _)).
92 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
93 EXPECT_CALL(GetMock<SqlMock>(),
94 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
95 EXPECT_CALL(GetMock<SqlMock>(),
96 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
97 EXPECT_CALL(GetMock<SqlMock>(),
98 sqlite3_exec(_, _, _, _, _)).WillRepeatedly(Return(SQLITE_OK));
99 EXPECT_CALL(GetMock<SqlMock>(),
100 sqlite3_bind_int(_, _, _)).WillRepeatedly(Return(0));
101 EXPECT_CALL(GetMock<SqlMock>(),
102 sqlite3_bind_double(_, _, _)).WillOnce(Return(0));
103 EXPECT_CALL(GetMock<SqlMock>(),
104 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
105 EXPECT_CALL(GetMock<SqlMock>(),
106 sqlite3_step(_)).WillRepeatedly(Return(SQLITE_DONE));
107 EXPECT_CALL(GetMock<SqlMock>(),
108 sqlite3_finalize(_)).WillRepeatedly(Return(0));
110 int ret = rua_stat_db_update(const_cast<char*>("caller"),
111 const_cast<char*>("rua_stat_tag"));
112 EXPECT_EQ(ret, SQLITE_DONE);