forked from snowplow/snowplow-cpp-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnowplow_test.cpp
More file actions
147 lines (117 loc) · 6.03 KB
/
snowplow_test.cpp
File metadata and controls
147 lines (117 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright (c) 2023 Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License Version 2.0.
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
#include "../include/snowplow/snowplow.hpp"
#include "catch.hpp"
#include "http/test_http_client.hpp"
using namespace snowplow;
using std::chrono::milliseconds;
using std::this_thread::sleep_for;
TEST_CASE("snowplow") {
SECTION("return nullptr for unininitialized trackers") {
REQUIRE(Snowplow::get_tracker("xyz") == nullptr);
}
SECTION("creates and retrieves a tracker") {
auto tracker = Snowplow::create_tracker("ns", "https://collector.com", POST, "test-emitter.db");
REQUIRE(tracker);
REQUIRE(tracker->get_namespace() == "ns");
auto got_tracker = Snowplow::get_tracker("ns");
REQUIRE(got_tracker);
REQUIRE(tracker.get() == got_tracker.get());
REQUIRE(Snowplow::remove_tracker("ns"));
REQUIRE(Snowplow::get_tracker("ns") == nullptr);
}
SECTION("setting default tracker") {
// no default tracker on startup
REQUIRE(!Snowplow::get_default_tracker());
// sets the first tracker as default
auto tracker1 = Snowplow::create_tracker("ns1", "https://collector.com", POST, "test-emitter.db");
REQUIRE(Snowplow::get_default_tracker()->get_namespace() == "ns1");
// doesn't change the default tracker when creating new ones
auto tracker2 = Snowplow::create_tracker("ns2", "https://collector.com", POST, "test-emitter.db");
REQUIRE(Snowplow::get_default_tracker()->get_namespace() == "ns1");
// changes the default tracker on request
Snowplow::set_default_tracker(tracker2);
REQUIRE(Snowplow::get_default_tracker()->get_namespace() == "ns2");
// register a new default tracker
NetworkConfiguration network_config3("https://collector.com");
EmitterConfiguration emitter_config3(make_shared<SqliteStorage>("test-emitter.db"));
auto emitter3 = make_shared<Emitter>(network_config3, emitter_config3);
auto tracker3 = make_shared<Tracker>(emitter3, nullptr, nullptr, "mob", "", "ns3");
Snowplow::set_default_tracker(tracker3);
REQUIRE(Snowplow::get_default_tracker()->get_namespace() == "ns3");
REQUIRE(Snowplow::get_tracker("ns3"));
REQUIRE(Snowplow::remove_tracker(tracker1));
REQUIRE(Snowplow::remove_tracker(tracker2));
REQUIRE(Snowplow::remove_tracker(tracker3));
}
SECTION("registers a custom tracker") {
NetworkConfiguration network_config("https://collector.com");
EmitterConfiguration emitter_config(make_shared<SqliteStorage>("test-emitter.db"));
auto emitter = make_shared<Emitter>(network_config, emitter_config);
auto tracker = make_shared<Tracker>(emitter, nullptr, nullptr, "mob", "", "custom");
Snowplow::register_tracker(tracker);
REQUIRE(Snowplow::get_tracker("custom")->get_namespace() == "custom");
REQUIRE(Snowplow::get_default_tracker()->get_namespace() == "custom");
REQUIRE(Snowplow::remove_tracker("custom"));
}
SECTION("handles multiple trackers") {
auto storage1 = std::make_shared<SqliteStorage>("test-emitter-1.db");
auto storage2 = std::make_shared<SqliteStorage>("test-emitter-2.db");
auto http_client1 = new TestHttpClient();
NetworkConfiguration network_config1("http://com.acme.collector", POST);
network_config1.set_http_client(move(unique_ptr<TestHttpClient>(http_client1)));
auto tracker1 = Snowplow::create_tracker(
TrackerConfiguration("ns1"),
network_config1,
EmitterConfiguration(storage1)
);
auto http_client2 = new TestHttpClient();
NetworkConfiguration network_config2("http://com.acme.collector", POST);
network_config2.set_http_client(move(unique_ptr<TestHttpClient>(http_client2)));
auto tracker2 = Snowplow::create_tracker(
TrackerConfiguration("ns2"),
network_config2,
EmitterConfiguration(storage2)
);
tracker1->track(StructuredEvent("cat1", "act1"));
tracker2->track(StructuredEvent("cat2", "act2"));
tracker1->track(StructuredEvent("cat3", "act3"));
tracker1->flush();
tracker2->flush();
REQUIRE(http_client1->get_instance_requests_list().size() == 2);
REQUIRE(http_client2->get_instance_requests_list().size() == 1);
TestHttpClient::reset();
REQUIRE(Snowplow::remove_tracker("ns1"));
REQUIRE(Snowplow::remove_tracker("ns2"));
}
SECTION("creates a common session and event store when given the same database path") {
NetworkConfiguration network_config("http://com.acme.collector");
SessionConfiguration session_config("test-emitter.db");
EmitterConfiguration emitter_config("test-emitter.db");
auto tracker = Snowplow::create_tracker(
TrackerConfiguration("ns"), network_config, emitter_config, session_config);
auto session_db = session_config.get_session_store();
auto event_db = emitter_config.get_event_store();
REQUIRE((SqliteStorage *)session_db.get() == (SqliteStorage *)event_db.get());
REQUIRE(Snowplow::remove_tracker("ns"));
}
SECTION("creates different session and event stores given different paths") {
NetworkConfiguration network_config("http://com.acme.collector");
SessionConfiguration session_config("test-emitter-1.db");
EmitterConfiguration emitter_config("test-emitter-2.db");
auto tracker = Snowplow::create_tracker(
TrackerConfiguration("ns"), network_config, emitter_config, session_config);
auto session_db = session_config.get_session_store();
auto event_db = emitter_config.get_event_store();
REQUIRE((SqliteStorage *)session_db.get() != (SqliteStorage *)event_db.get());
REQUIRE(Snowplow::remove_tracker("ns"));
}
}