|
1 | | -#include "countly.hpp" |
| 1 | +#include "countly/event.hpp" |
2 | 2 | #include "doctest.h" |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | +using namespace cly; |
| 6 | + |
| 7 | +void valideEventParams(nlohmann::json eventJson, std::string key, int count) { |
| 8 | + CHECK(eventJson["key"].get<std::string>() == key); |
| 9 | + CHECK(eventJson["count"].get<int>() == count); |
| 10 | + CHECK(std::to_string(eventJson["timestamp"].get<long long>()).size() == 13); |
| 11 | +} |
3 | 12 |
|
4 | 13 | TEST_CASE("events are serialized correctly") { |
5 | 14 | SUBCASE("without segmentation") { |
6 | 15 | SUBCASE("without sum") { |
7 | 16 | cly::Event event("win", 1); |
8 | | - CHECK(event.serialize() == "{\"count\":1,\"key\":\"win\"}"); |
| 17 | + |
| 18 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 19 | + valideEventParams(e, "win", 1); |
9 | 20 | } |
10 | 21 |
|
11 | 22 | SUBCASE("with sum") { |
12 | 23 | cly::Event event("buy", 2, 9.99); |
13 | | - CHECK(event.serialize() == "{\"count\":2,\"key\":\"buy\",\"sum\":9.99}"); |
| 24 | + |
| 25 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 26 | + CHECK(e["sum"].get<double>() == 9.99); |
| 27 | + valideEventParams(e, "buy", 2); |
14 | 28 | } |
15 | 29 | } |
16 | 30 |
|
17 | 31 | SUBCASE("with segmentation") { |
18 | 32 | SUBCASE("with signed integer") { |
19 | 33 | cly::Event event("lose", 3); |
20 | 34 | event.addSegmentation("points", -144); |
21 | | - CHECK(event.serialize() == "{\"count\":3,\"key\":\"lose\",\"segmentation\":{\"points\":-144}}"); |
| 35 | + |
| 36 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 37 | + valideEventParams(e, "lose", 3); |
| 38 | + |
| 39 | + nlohmann::json s = e["segmentation"].get<nlohmann::json>(); |
| 40 | + CHECK(s["points"].get<int>() == -144); |
22 | 41 | } |
23 | 42 |
|
24 | 43 | SUBCASE("with unsigned integer") { |
25 | 44 | cly::Event event("win", 1); |
26 | 45 | event.addSegmentation("points", 232U); |
27 | | - CHECK(event.serialize() == "{\"count\":1,\"key\":\"win\",\"segmentation\":{\"points\":232}}"); |
| 46 | + |
| 47 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 48 | + valideEventParams(e, "win", 1); |
| 49 | + |
| 50 | + nlohmann::json s = e["segmentation"].get<nlohmann::json>(); |
| 51 | + CHECK(s["points"].get<unsigned int>() == 232U); |
28 | 52 | } |
29 | 53 |
|
30 | 54 | SUBCASE("with boolean") { |
31 | 55 | cly::Event event("win", 1); |
32 | 56 | event.addSegmentation("alive", true); |
33 | | - CHECK(event.serialize() == "{\"count\":1,\"key\":\"win\",\"segmentation\":{\"alive\":true}}"); |
| 57 | + |
| 58 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 59 | + valideEventParams(e, "win", 1); |
| 60 | + |
| 61 | + nlohmann::json s = e["segmentation"].get<nlohmann::json>(); |
| 62 | + CHECK(s["alive"].get<bool>() == true); |
34 | 63 | } |
35 | 64 |
|
36 | 65 | SUBCASE("with string") { |
37 | 66 | cly::Event event("message", 1); |
38 | 67 | event.addSegmentation("sender", "TheLegend27"); |
39 | | - CHECK(event.serialize() == "{\"count\":1,\"key\":\"message\",\"segmentation\":{\"sender\":\"TheLegend27\"}}"); |
| 68 | + |
| 69 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 70 | + valideEventParams(e, "message", 1); |
| 71 | + |
| 72 | + nlohmann::json s = e["segmentation"].get<nlohmann::json>(); |
| 73 | + CHECK(s["sender"].get<std::string>() == "TheLegend27"); |
40 | 74 | } |
41 | 75 |
|
42 | 76 | SUBCASE("with multiple values") { |
43 | 77 | cly::Event event("buy", 5); |
44 | 78 | event.addSegmentation("quantity", 27); |
45 | 79 | event.addSegmentation("searchQuery", "cheap cheese"); |
46 | | - CHECK(event.serialize() == "{\"count\":5,\"key\":\"buy\",\"segmentation\":{\"quantity\":27,\"searchQuery\":\"cheap cheese\"}}"); |
47 | | - } |
48 | 80 |
|
49 | | - SUBCASE("with changing values") { |
50 | | - cly::Event event("lose", 3); |
51 | | - event.addSegmentation("points", -144); |
52 | | - event.addSegmentation("points", 2000); |
53 | | - CHECK(event.serialize() == "{\"count\":3,\"key\":\"lose\",\"segmentation\":{\"points\":2000}}"); |
54 | | - } |
| 81 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 82 | + valideEventParams(e, "buy", 5); |
55 | 83 |
|
56 | | - SUBCASE("with count, sum, duration and segmentation") { |
57 | | - cly::Event event("lose", 3, 10, 100); |
58 | | - event.addSegmentation("points", 2000); |
59 | | - CHECK(event.serialize() == "{\"count\":3,\"dur\":100.0,\"key\":\"lose\",\"segmentation\":{\"points\":2000},\"sum\":10.0}"); |
| 84 | + nlohmann::json s = e["segmentation"].get<nlohmann::json>(); |
| 85 | + CHECK(s["quantity"].get<int>() == 27); |
| 86 | + CHECK(s["searchQuery"].get<std::string>() == "cheap cheese"); |
60 | 87 | } |
61 | 88 |
|
62 | 89 | SUBCASE("with multibyte strings") { |
63 | 90 | cly::Event event("测试", 1); |
64 | 91 | event.addSegmentation("苹果", "美味"); |
65 | | - CHECK(event.serialize() == "{\"count\":1,\"key\":\"测试\",\"segmentation\":{\"苹果\":\"美味\"}}"); |
| 92 | + |
| 93 | + nlohmann::json e = nlohmann::json::parse(event.serialize()); |
| 94 | + valideEventParams(e, "测试", 1); |
| 95 | + |
| 96 | + nlohmann::json s = e["segmentation"].get<nlohmann::json>(); |
| 97 | + CHECK(s["苹果"].get<std::string>() == "美味"); |
66 | 98 | } |
67 | 99 | } |
68 | 100 | } |
0 commit comments