Skip to content

Commit

Permalink
feat: fixing up (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Jan 15, 2024
1 parent a578745 commit 93e0843
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions countlyCommon/TestingRelated/ModuleBackendModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public async void RecordEvent_NullOrEmpty_AppKey()
cc.EnableBackendMode();
// made all queues 1 to look to the queue to detect eq changes
cc.SetEventQueueSizeToSend(1).SetBackendModeAppEQSizeToSend(1).SetBackendModeServerEQSizeToSend(1);
Countly.Instance.Init(cc).Wait();

Countly.Instance.BackendMode().RecordEvent("DEVICE_ID", "", TestHelper.v[2]);
ValidateEventInRequestQueue(TestHelper.v[2], "DEVICE_ID", TestHelper.APP_KEY);
Expand Down Expand Up @@ -327,6 +328,43 @@ public void RecordUserProperties()
"action", Dict("$push", "black")))));
}

[Fact]
/// <summary>
/// "RecordUserProperties" with different user properties
/// Validate that a user properties request is generated after each call and expected parameters should be added
/// RQ size must be 1 and parameters should match
/// </summary>
public void RecordUserProperties_Modificators()
{
CountlyConfig cc = TestHelper.GetConfig();
cc.EnableBackendMode();

Countly.Instance.Init(cc).Wait();

IDictionary<string, object> userProperties = Dict();

userProperties["marks"] = "{$inc: 1}";
userProperties["point"] = "{$mul: 1.89}";
userProperties["gpa"] = "{$min: 1.89}";
userProperties["gpa"] = "{$max: 1.89}";
userProperties["fav"] = "{$setOnce: \"FAV\"}";
userProperties["permissions"] = "{$pull: [\"Create\", \"Update\"]}";
userProperties["langs"] = "{$push: [\"Python\", \"Ruby\", \"Ruby\"]}";
userProperties["langs"] = "{$addToSet: [\"Python\", \"Python\"]}";

Countly.Instance.BackendMode().RecordUserProperties(TestHelper.v[0], userProperties);
ValidateRequestInQueue(TestHelper.v[0], TestHelper.APP_KEY, Dict("user_details", Json(
"custom", Dict(
"marks", Dict("$inc", 1),
"point", Dict("$mul", 1.89),
"gpa", Dict("$max", 1.89),
"fav", Dict("$setOnce", "FAV"),
"fav", Dict("$setOnce", "FAV"),
"permissions", Dict("$pull", new string[] { "Create", "Update" }),
"langs", Dict("$addToSet", new string[] { "Python", "Python" })
))));
}

[Fact]
/// <summary>
/// "RecordUserProperties" with null and empty properties
Expand Down Expand Up @@ -745,7 +783,7 @@ public void StopView()
Countly.Instance.BackendMode().StopView(TestHelper.v[0], TestHelper.v[4], 180, Segm("bip", "boop"), "Android", TestHelper.v[2], 1044151383000);
ValidateEventInRequestQueue("[CLY]_view", TestHelper.v[0], TestHelper.v[2], duration: 180, segmentation: Segm("name", TestHelper.v[4], "segment", "Android", "bip", "boop"), reqCount: 2, rqIdx: 1, timestamp: 1044151383000);

Countly.Instance.BackendMode().StopView(TestHelper.v[0], TestHelper.v[5], -56, appKey: TestHelper.v[-56]);
Countly.Instance.BackendMode().StopView(TestHelper.v[0], TestHelper.v[5], -56, appKey: TestHelper.v[6]);
Assert.Equal(2, Countly.Instance.StoredRequests.Count);
}

Expand Down Expand Up @@ -791,9 +829,9 @@ public void BeginSession_MetricOverride()
{ "_os", "OS" },
{ "_os_version", "OS_V" },
{ "_app_version", "AV" },
{ "_resolution", "100x100" },
{ "_locale", "LOCALE" },
{ "_device", "Test" },
{ "_resolution", "100x100" },
{ "_device", "Test" },
{ "_carrier", "CARRIER" },
{ "_build_version", "1.0" },
{ "", "1.0" },
Expand All @@ -803,7 +841,18 @@ public void BeginSession_MetricOverride()
Countly.Instance.Init(cc).Wait();

Countly.Instance.BackendMode().BeginSession(TestHelper.v[0], TestHelper.v[1], timestamp: 1044151383000);
ValidateRequestInQueue(TestHelper.v[0], TestHelper.v[1], Dict("begin_session", "1", "metrics", Json("_os", "OS", "_os_version", "OS_V", "_resolution", "100x100", "_app_version", "AV", "_locale", "LOCALE", "_device", "Test", "_carrier", "CARRIER", "_build_version", "1.0")), 0, 1, 1044151383000);
ValidateRequestInQueue(TestHelper.v[0], TestHelper.v[1], Dict("begin_session", "1", "metrics", "CUSTOM_VALIDATED"), 0, 1, 1044151383000,
new Dictionary<string, Action<string, object>>(){{"metrics", (actual, expected) => {
Dictionary<string,object> convertedMetrics = JsonConvert.DeserializeObject<Dictionary<string,object>>(actual);
IDictionary<string, object> expectedDict = Dict("_os", "OS", "_os_version", "OS_V", "_app_version", "AV", "_locale", "LOCALE", "_resolution", "100x100", "_device", "Test", "_carrier", "CARRIER", "_build_version", "1.0");
Assert.Equal(convertedMetrics.Count, expectedDict.Count);
foreach(KeyValuePair<string, object> pair in expectedDict)
{
Assert.Equal(pair.Value,convertedMetrics[pair.Key]);
}
} } });
}

[Fact]
Expand Down Expand Up @@ -961,15 +1010,19 @@ private void ValidateEventInRequestQueue(string key, string deviceId, string app
Assert.True(events[eventIdx].Timestamp > 0);
}

private void ValidateRequestInQueue(string deviceId, string appKey, IDictionary<string, object> paramaters, int rqIdx = 0, int rqSize = 1, long timestamp = 0)
private void ValidateRequestInQueue(string deviceId, string appKey, IDictionary<string, object> paramaters, int rqIdx = 0, int rqSize = 1, long timestamp = 0, IDictionary<string, Action<string, object>> customValidators = null)
{
Assert.Equal(rqSize, Countly.Instance.StoredRequests.Count);
string request = Countly.Instance.StoredRequests.ElementAt(rqIdx).Request;
Dictionary<string, string> queryParams = TestHelper.GetParams(request);
ValidateBaseParams(queryParams, deviceId, appKey, timestamp);
Assert.Equal(10 + paramaters.Count, queryParams.Count); //TODO 11 after merge
foreach (KeyValuePair<string, object> item in paramaters) {
Assert.Equal(item.Value.ToString(), queryParams[item.Key]);
if (customValidators != null && customValidators.ContainsKey(item.Key)) {
customValidators[item.Key].Invoke(queryParams[item.Key], item.Value);
} else {
Assert.Equal(item.Value.ToString(), queryParams[item.Key]);
}
}
}

Expand Down

0 comments on commit 93e0843

Please sign in to comment.