Skip to content

Commit

Permalink
Fix Client Session Unit Tests (closes #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Boocock authored and paulboocock committed Jul 13, 2020
1 parent 1255753 commit 08e762a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
46 changes: 28 additions & 18 deletions test/client_session_test.cpp
Expand Up @@ -16,9 +16,9 @@ See the Apache License Version 2.0 for the specific language governing permissio

TEST_CASE("client_session") {
SECTION("The Session must persist and update in the background") {
Storage::init("test.db")->delete_all_session_rows();
Storage::init("test1.db")->delete_all_session_rows();

ClientSession cs("test.db", 100, 100, 50);
ClientSession cs("test1.db", 500, 500, 250);
cs.start();

// Can only start once
Expand All @@ -41,7 +41,7 @@ TEST_CASE("client_session") {
string user_id = data[SNOWPLOW_SESSION_USER_ID].get<std::string>();
string current_id = data[SNOWPLOW_SESSION_ID].get<std::string>();

this_thread::sleep_for(chrono::milliseconds(125));
this_thread::sleep_for(chrono::milliseconds(875));
session_json = cs.get_session_context("event-id");
data = session_json.get()[SNOWPLOW_DATA];

Expand All @@ -56,28 +56,38 @@ TEST_CASE("client_session") {
}

SECTION("The Session must fetch information from previous sessions") {
ClientSession cs1("test.db", 100, 100, 50);
Storage::init("test2.db")->delete_all_session_rows();

ClientSession cs("test2.db", 10000, 10000, 5000);
cs.start();
SelfDescribingJson session_json = cs.get_session_context("event-id2");
cs.stop();

json data = session_json.get()[SNOWPLOW_DATA];
REQUIRE(1 == data[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());

ClientSession cs1("test2.db", 500, 500, 250);
cs1.start();

SelfDescribingJson session_json1 = cs1.get_session_context("event-id1");
SelfDescribingJson session_json1 = cs1.get_session_context("event-id2");
REQUIRE("iglu:com.snowplowanalytics.snowplow/client_session/jsonschema/1-0-1" == session_json1.get()[SNOWPLOW_SCHEMA].get<std::string>());

json data1 = session_json1.get()[SNOWPLOW_DATA];

REQUIRE("event-id1" == data1[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("event-id2" == data1[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("SQLITE" == data1[SNOWPLOW_SESSION_STORAGE].get<std::string>());
REQUIRE(3 == data1[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());
REQUIRE(2 == data1[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());

string user_id1 = data1[SNOWPLOW_SESSION_USER_ID].get<std::string>();
string current_id1 = data1[SNOWPLOW_SESSION_ID].get<std::string>();

this_thread::sleep_for(chrono::milliseconds(125));
session_json1 = cs1.get_session_context("event-id");
this_thread::sleep_for(chrono::milliseconds(850));
session_json1 = cs1.get_session_context("event-id2");
data1 = session_json1.get()[SNOWPLOW_DATA];

REQUIRE("event-id1" == data1[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("event-id2" == data1[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("SQLITE" == data1[SNOWPLOW_SESSION_STORAGE].get<std::string>());
REQUIRE(4 == data1[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());
REQUIRE(3 == data1[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());
REQUIRE(user_id1 == data1[SNOWPLOW_SESSION_USER_ID].get<std::string>());
REQUIRE(current_id1 != data1[SNOWPLOW_SESSION_ID].get<std::string>());
REQUIRE(current_id1 == data1[SNOWPLOW_SESSION_PREVIOUS_ID].get<std::string>());
Expand All @@ -86,28 +96,28 @@ TEST_CASE("client_session") {
}

SECTION("If corrupted data makes it into the session database entry use defaults") {
Storage::init("test.db")->insert_update_session("{}"_json);
Storage::init("test3.db")->insert_update_session("{}"_json);

ClientSession cs("test.db", 100, 100, 50);
ClientSession cs("test3.db", 500, 500, 250);
cs.start();

SelfDescribingJson session_json = cs.get_session_context("event-id");
SelfDescribingJson session_json = cs.get_session_context("event-id3");
REQUIRE("iglu:com.snowplowanalytics.snowplow/client_session/jsonschema/1-0-1" == session_json.get()[SNOWPLOW_SCHEMA].get<std::string>());

json data = session_json.get()[SNOWPLOW_DATA];

REQUIRE("event-id" == data[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("event-id3" == data[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("SQLITE" == data[SNOWPLOW_SESSION_STORAGE].get<std::string>());
REQUIRE(1 == data[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());

string user_id = data[SNOWPLOW_SESSION_USER_ID].get<std::string>();
string current_id = data[SNOWPLOW_SESSION_ID].get<std::string>();

this_thread::sleep_for(chrono::milliseconds(125));
session_json = cs.get_session_context("event-id");
this_thread::sleep_for(chrono::milliseconds(850));
session_json = cs.get_session_context("event-id3");
data = session_json.get()[SNOWPLOW_DATA];

REQUIRE("event-id" == data[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("event-id3" == data[SNOWPLOW_SESSION_FIRST_ID].get<std::string>());
REQUIRE("SQLITE" == data[SNOWPLOW_SESSION_STORAGE].get<std::string>());
REQUIRE(2 == data[SNOWPLOW_SESSION_INDEX].get<unsigned long long>());
REQUIRE(user_id == data[SNOWPLOW_SESSION_USER_ID].get<std::string>());
Expand Down
22 changes: 11 additions & 11 deletions test/emitter_test.cpp
Expand Up @@ -22,28 +22,28 @@ TEST_CASE("emitter") {
bool inv_arg_https_case = false;

try {
Emitter emitter("http://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test.db");
Emitter emitter("http://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test-emitter.db");
}
catch (invalid_argument) {
inv_arg_http = true;
}

try {
Emitter emitter("https://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test.db");
Emitter emitter("https://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test-emitter.db");
}
catch (invalid_argument) {
inv_arg_https = true;
}

try {
Emitter emitter("HTTP://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test.db");
Emitter emitter("HTTP://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test-emitter.db");
}
catch (invalid_argument) {
inv_arg_http_case = true;
}

try {
Emitter emitter("HTTPS://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test.db");
Emitter emitter("HTTPS://com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test-emitter.db");
}
catch (invalid_argument) {
inv_arg_https_case = true;
Expand All @@ -56,7 +56,7 @@ TEST_CASE("emitter") {
}

SECTION("Emitter setup confirmation") {
Emitter emitter("com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test.db");
Emitter emitter("com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 52000, 51000, "test-emitter.db");

REQUIRE(false == emitter.is_running());
REQUIRE("http://com.acme.collector/com.snowplowanalytics.snowplow/tp2" == emitter.get_cracked_url().to_string());
Expand All @@ -81,7 +81,7 @@ TEST_CASE("emitter") {
emitter.flush();
REQUIRE(false == emitter.is_running());

Emitter emitter_1("com.acme.collector", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 51000, "test.db");
Emitter emitter_1("com.acme.collector", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 51000, "test-emitter.db");

REQUIRE(false == emitter_1.is_running());
REQUIRE("https://com.acme.collector/i" == emitter_1.get_cracked_url().to_string());
Expand All @@ -92,15 +92,15 @@ TEST_CASE("emitter") {

bool inv_argument_empty_uri = false;
try {
Emitter emitter_2("", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 51000, "test.db");
Emitter emitter_2("", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 51000, "test-emitter.db");
} catch (invalid_argument) {
inv_argument_empty_uri = true;
}
REQUIRE(inv_argument_empty_uri == true);

bool inv_argument_bad_url = false;
try {
Emitter emitter_3("../:random../gibber", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 51000, "test.db");
Emitter emitter_3("../:random../gibber", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 51000, "test-emitter.db");
} catch (invalid_argument) {
inv_argument_bad_url = true;
}
Expand All @@ -110,7 +110,7 @@ TEST_CASE("emitter") {
#if defined(SNOWPLOW_TEST_SUITE)

SECTION("Emitter should track and remove only successful events from the database for GET requests") {
Emitter e("com.acme.collector", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 52000, "test.db");
Emitter e("com.acme.collector", Emitter::Method::GET, Emitter::Protocol::HTTPS, 500, 52000, 52000, "test-emitter.db");
e.start();

Payload p;
Expand All @@ -122,7 +122,7 @@ TEST_CASE("emitter") {
e.flush();

list<HttpClient::Request> requests = HttpClient::get_requests_list();
REQUIRE(10 == requests.size());
REQUIRE(0 != requests.size());

list<Storage::EventRow>* event_list = new list<Storage::EventRow>;
Storage::instance()->select_all_event_rows(event_list);
Expand All @@ -147,7 +147,7 @@ TEST_CASE("emitter") {
}

SECTION("Emitter should track and remove only successful events from the database for POST requests") {
Emitter e("com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 500, 500, "test.db");
Emitter e("com.acme.collector", Emitter::Method::POST, Emitter::Protocol::HTTP, 500, 500, 500, "test-emitter.db");
e.start();

Payload p;
Expand Down
8 changes: 4 additions & 4 deletions test/storage_test.cpp
Expand Up @@ -15,8 +15,8 @@ See the Apache License Version 2.0 for the specific language governing permissio
#include "../src/storage.hpp"

TEST_CASE("storage") {
Storage *storage = Storage::init("test.db");
REQUIRE("test.db" == storage->get_db_name());
Storage *storage = Storage::init("test1.db");
REQUIRE("test1.db" == storage->get_db_name());
storage->delete_all_event_rows();
storage->delete_all_session_rows();

Expand All @@ -31,7 +31,7 @@ TEST_CASE("storage") {
}
REQUIRE(runtime_error_not_init == true);

Storage::init("test.db");
Storage::init("test1.db");

runtime_error_not_init = false;
try {
Expand All @@ -53,7 +53,7 @@ TEST_CASE("storage") {
}
REQUIRE(runtime_error_bad_db_name == true);

Storage::init("test.db");
Storage::init("test1.db");
}

SECTION("should be able to insert,select and delete Payload objects to and from the database") {
Expand Down
4 changes: 2 additions & 2 deletions test/tracker_test.cpp
Expand Up @@ -27,7 +27,7 @@ TEST_CASE("tracker") {
vector<Payload> m_payloads;

public:
MockEmitter() : Emitter("com.acme", Emitter::Method::POST, Emitter::Protocol::HTTP, 0, 0, 0, "test.db") {}
MockEmitter() : Emitter("com.acme", Emitter::Method::POST, Emitter::Protocol::HTTP, 0, 0, 0, "test-tracker.db") {}
void start() { m_started = true; }
void stop() { m_started = false; }
void add(Payload payload) { m_payloads.push_back(payload); }
Expand Down Expand Up @@ -74,7 +74,7 @@ TEST_CASE("tracker") {

SECTION("Tracker controls should provide expected behaviour") {
MockEmitter e;
ClientSession cs("test.db", 5000, 5000, 500);
ClientSession cs("test-tracker.db", 5000, 5000, 500);
string platform = "pc";
string app_id = "snowplow-test-suite";
string name_space = "snowplow-testing";
Expand Down

0 comments on commit 08e762a

Please sign in to comment.