Skip to content

Commit ced623b

Browse files
committed
Add server-side tests for events, the player store, registration and callbacks
GAStorePlayersTests covers the ga_players table directly: upsert and replace, empty id and data handling, parameter escaping, delete, listing, counting, the strict eviction cutoff, trim ordering and tie-breaks, and that the rows survive a reopen, are cleared by a drop and are independent of the event rows. GameAnalyticsServerTests gains four groups: - server events: payloads and rejections for design, business, resource, error and progression events sent under the server's identity - player registration: addPlayer, duplicates, getPlayer/findPlayer, removePlayer rules, implicit registration on session start, savePlayer, saveAllPlayers and restore by a second server instance - player callbacks: firing order and player identity, exit state, changes made in onPlayerJoin reaching the session start event, failed init, null and replaced callbacks, and no onNewPlayer for restored players - player vs server events: identities, sessions, transaction numbers and progression attempts stay separate, and submission toggling applies to both The callbacks recorder in the server fixture now tracks the call sequence and the player state seen on exit.
1 parent 27f19fc commit ced623b

2 files changed

Lines changed: 963 additions & 2 deletions

File tree

‎test/GAStorePlayersTests.cpp‎

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
//
2+
// GA-SDK-CPP
3+
// Tests for the ga_players table in the sqlite store (the raw persistence layer
4+
// under PlayerDatabase): upsert, delete, listing, counting and retention queries
5+
//
6+
7+
#include <gtest/gtest.h>
8+
9+
#include <GAState.h>
10+
#include <GAStore.h>
11+
#include <GAUtilities.h>
12+
13+
#include <map>
14+
#include <string>
15+
16+
using namespace gameanalytics;
17+
18+
namespace
19+
{
20+
constexpr const char* kGameKey = "bd624ee6f8e6efb32a054f8d7ba11618";
21+
constexpr const char* kGameSecret = "7f5c3f682cbd217841efba92e92ffb1b3b6612bc";
22+
23+
class GAStorePlayersTest : public ::testing::Test
24+
{
25+
protected:
26+
void SetUp() override
27+
{
28+
state::GAState::setKeys(kGameKey, kGameSecret);
29+
ASSERT_TRUE(store::GAStore::ensureDatabase(false, kGameKey));
30+
ASSERT_TRUE(store::GAStore::getTableReady());
31+
clear();
32+
}
33+
34+
void TearDown() override
35+
{
36+
clear();
37+
}
38+
39+
static void clear()
40+
{
41+
store::GAStore::executeQuerySync("DELETE FROM ga_players;");
42+
store::GAStore::executeQuerySync("DELETE FROM ga_events;");
43+
}
44+
45+
// user_id -> data for every stored row
46+
static std::map<std::string, std::string> rows()
47+
{
48+
json out;
49+
store::GAStore::getPlayers(out);
50+
51+
std::map<std::string, std::string> result;
52+
if (out.is_array())
53+
{
54+
for (auto& row : out)
55+
{
56+
result[row.value("user_id", "")] = row.value("data", "");
57+
}
58+
}
59+
return result;
60+
}
61+
62+
static int64_t lastSeen(std::string const& uid)
63+
{
64+
json out;
65+
store::GAStore::executeQuerySync("SELECT last_seen FROM ga_players WHERE user_id = ?;", {uid}, out);
66+
if (out.is_array() && !out.empty())
67+
{
68+
return utilities::getOptionalValue<int64_t>(out[0], "last_seen", -1);
69+
}
70+
return -1;
71+
}
72+
};
73+
}
74+
75+
TEST_F(GAStorePlayersTest, TableStartsEmpty)
76+
{
77+
EXPECT_EQ(0, store::GAStore::countPlayers());
78+
EXPECT_TRUE(rows().empty());
79+
}
80+
81+
TEST_F(GAStorePlayersTest, SetPlayerInsertsARow)
82+
{
83+
store::GAStore::setPlayer("p1", "{\"user_id\":\"p1\"}", 100);
84+
85+
EXPECT_EQ(1, store::GAStore::countPlayers());
86+
EXPECT_EQ("{\"user_id\":\"p1\"}", rows()["p1"]);
87+
EXPECT_EQ(100, lastSeen("p1"));
88+
}
89+
90+
TEST_F(GAStorePlayersTest, SetPlayerReplacesAnExistingRow)
91+
{
92+
store::GAStore::setPlayer("p1", "v1", 100);
93+
store::GAStore::setPlayer("p1", "v2", 200);
94+
95+
EXPECT_EQ(1, store::GAStore::countPlayers());
96+
EXPECT_EQ("v2", rows()["p1"]);
97+
EXPECT_EQ(200, lastSeen("p1"));
98+
}
99+
100+
TEST_F(GAStorePlayersTest, SetPlayerIgnoresAnEmptyUserId)
101+
{
102+
store::GAStore::setPlayer("", "data", 100);
103+
EXPECT_EQ(0, store::GAStore::countPlayers());
104+
}
105+
106+
TEST_F(GAStorePlayersTest, SetPlayerWithEmptyDataDeletesTheRow)
107+
{
108+
store::GAStore::setPlayer("p1", "v1", 100);
109+
store::GAStore::setPlayer("p1", "", 200);
110+
111+
EXPECT_EQ(0, store::GAStore::countPlayers());
112+
}
113+
114+
TEST_F(GAStorePlayersTest, DataWithQuotesAndUnicodeRoundTrips)
115+
{
116+
const std::string data = "{\"name\":\"O'Brien \\\"the\\\" ☃\",\"semi\":\"a;b--c\"}";
117+
store::GAStore::setPlayer("p'1;--", data, 1);
118+
119+
std::map<std::string, std::string> stored = rows();
120+
ASSERT_EQ(1u, stored.size());
121+
EXPECT_EQ(data, stored["p'1;--"]);
122+
}
123+
124+
TEST_F(GAStorePlayersTest, RemovePlayerDeletesOnlyThatRow)
125+
{
126+
store::GAStore::setPlayer("p1", "d1", 1);
127+
store::GAStore::setPlayer("p2", "d2", 2);
128+
129+
store::GAStore::removePlayer("p1");
130+
store::GAStore::removePlayer("missing");
131+
store::GAStore::removePlayer("");
132+
133+
std::map<std::string, std::string> stored = rows();
134+
EXPECT_EQ(1u, stored.size());
135+
EXPECT_EQ("d2", stored["p2"]);
136+
}
137+
138+
TEST_F(GAStorePlayersTest, GetPlayersListsUserIdAndData)
139+
{
140+
store::GAStore::setPlayer("p1", "d1", 1);
141+
store::GAStore::setPlayer("p2", "d2", 2);
142+
store::GAStore::setPlayer("p3", "d3", 3);
143+
144+
json out;
145+
store::GAStore::getPlayers(out);
146+
147+
ASSERT_TRUE(out.is_array());
148+
ASSERT_EQ(3u, out.size());
149+
for (auto& row : out)
150+
{
151+
EXPECT_TRUE(row.contains("user_id"));
152+
EXPECT_TRUE(row.contains("data"));
153+
EXPECT_EQ(2u, row.size());
154+
}
155+
156+
std::map<std::string, std::string> stored = rows();
157+
EXPECT_EQ("d1", stored["p1"]);
158+
EXPECT_EQ("d2", stored["p2"]);
159+
EXPECT_EQ("d3", stored["p3"]);
160+
}
161+
162+
TEST_F(GAStorePlayersTest, CountPlayersTracksInsertsAndDeletes)
163+
{
164+
EXPECT_EQ(0, store::GAStore::countPlayers());
165+
store::GAStore::setPlayer("p1", "d", 1);
166+
store::GAStore::setPlayer("p2", "d", 1);
167+
EXPECT_EQ(2, store::GAStore::countPlayers());
168+
store::GAStore::removePlayer("p1");
169+
EXPECT_EQ(1, store::GAStore::countPlayers());
170+
}
171+
172+
// ---- retention queries ----
173+
174+
TEST_F(GAStorePlayersTest, EvictPlayersInactiveSinceDropsRowsBeforeTheCutoff)
175+
{
176+
store::GAStore::setPlayer("old", "d", 100);
177+
store::GAStore::setPlayer("border", "d", 200);
178+
store::GAStore::setPlayer("recent", "d", 300);
179+
180+
// strict: a row last seen exactly at the cutoff is kept
181+
EXPECT_EQ(1, store::GAStore::evictPlayersInactiveSince(200));
182+
183+
std::map<std::string, std::string> stored = rows();
184+
EXPECT_EQ(0u, stored.count("old"));
185+
EXPECT_EQ(1u, stored.count("border"));
186+
EXPECT_EQ(1u, stored.count("recent"));
187+
188+
EXPECT_EQ(0, store::GAStore::evictPlayersInactiveSince(0));
189+
EXPECT_EQ(2, store::GAStore::evictPlayersInactiveSince(1000));
190+
EXPECT_EQ(0, store::GAStore::countPlayers());
191+
}
192+
193+
TEST_F(GAStorePlayersTest, TrimPlayersKeepsTheMostRecentlySeenRows)
194+
{
195+
store::GAStore::setPlayer("a", "d", 10);
196+
store::GAStore::setPlayer("b", "d", 40);
197+
store::GAStore::setPlayer("c", "d", 20);
198+
store::GAStore::setPlayer("d", "d", 30);
199+
200+
EXPECT_EQ(2, store::GAStore::trimPlayers(2));
201+
202+
std::map<std::string, std::string> stored = rows();
203+
EXPECT_EQ(2u, stored.size());
204+
EXPECT_EQ(1u, stored.count("b"));
205+
EXPECT_EQ(1u, stored.count("d"));
206+
}
207+
208+
TEST_F(GAStorePlayersTest, TrimPlayersBreaksTiesByUserId)
209+
{
210+
store::GAStore::setPlayer("z", "d", 5);
211+
store::GAStore::setPlayer("a", "d", 5);
212+
store::GAStore::setPlayer("m", "d", 5);
213+
214+
EXPECT_EQ(1, store::GAStore::trimPlayers(2));
215+
216+
std::map<std::string, std::string> stored = rows();
217+
EXPECT_EQ(1u, stored.count("a"));
218+
EXPECT_EQ(1u, stored.count("m"));
219+
EXPECT_EQ(0u, stored.count("z"));
220+
}
221+
222+
TEST_F(GAStorePlayersTest, TrimPlayersIsANoOpWhenUnderTheCap)
223+
{
224+
store::GAStore::setPlayer("a", "d", 1);
225+
store::GAStore::setPlayer("b", "d", 2);
226+
227+
EXPECT_EQ(0, store::GAStore::trimPlayers(2));
228+
EXPECT_EQ(0, store::GAStore::trimPlayers(5));
229+
EXPECT_EQ(0, store::GAStore::trimPlayers(-1));
230+
EXPECT_EQ(2, store::GAStore::countPlayers());
231+
232+
EXPECT_EQ(2, store::GAStore::trimPlayers(0));
233+
EXPECT_EQ(0, store::GAStore::countPlayers());
234+
}
235+
236+
// ---- interaction with the rest of the store ----
237+
238+
TEST_F(GAStorePlayersTest, PlayerRowsSurviveReopeningTheDatabase)
239+
{
240+
store::GAStore::setPlayer("p1", "d1", 7);
241+
242+
ASSERT_TRUE(store::GAStore::ensureDatabase(false, kGameKey));
243+
244+
EXPECT_EQ(1, store::GAStore::countPlayers());
245+
EXPECT_EQ("d1", rows()["p1"]);
246+
EXPECT_EQ(7, lastSeen("p1"));
247+
}
248+
249+
TEST_F(GAStorePlayersTest, DroppingTheDatabaseClearsPlayers)
250+
{
251+
store::GAStore::setPlayer("p1", "d1", 7);
252+
253+
ASSERT_TRUE(store::GAStore::ensureDatabase(true, kGameKey));
254+
255+
EXPECT_TRUE(store::GAStore::getTableReady());
256+
EXPECT_EQ(0, store::GAStore::countPlayers());
257+
258+
// the table is usable again after the drop
259+
store::GAStore::setPlayer("p2", "d2", 8);
260+
EXPECT_EQ(1, store::GAStore::countPlayers());
261+
}
262+
263+
TEST_F(GAStorePlayersTest, PlayerRowsAreIndependentOfEventRows)
264+
{
265+
store::GAStore::setPlayer("p1", "d1", 1);
266+
store::GAStore::executeQuerySync("INSERT INTO ga_events (status, category, session_id, client_ts, event) VALUES(?, ?, ?, ?, ?);",
267+
{"new", "design", "s1", "1", "{\"user_id\":\"p1\"}"});
268+
269+
json events;
270+
store::GAStore::executeQuerySync("SELECT COUNT(*) AS n FROM ga_events;", events);
271+
ASSERT_TRUE(events.is_array());
272+
EXPECT_EQ(1, utilities::getOptionalValue<int64_t>(events[0], "n", 0));
273+
274+
store::GAStore::executeQuerySync("DELETE FROM ga_players;");
275+
store::GAStore::executeQuerySync("SELECT COUNT(*) AS n FROM ga_events;", events);
276+
EXPECT_EQ(1, utilities::getOptionalValue<int64_t>(events[0], "n", 0));
277+
278+
store::GAStore::setPlayer("p1", "d1", 1);
279+
store::GAStore::executeQuerySync("DELETE FROM ga_events;");
280+
EXPECT_EQ(1, store::GAStore::countPlayers());
281+
}

0 commit comments

Comments
 (0)