From 1279dea4fae347da18d5726ba558909b31720e53 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 26 Jul 2024 11:52:22 +0300 Subject: [PATCH 1/6] fix: segmentation same keys override --- .../countlyCommon/Entities/Segmentation.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/countlyCommon/countlyCommon/Entities/Segmentation.cs b/countlyCommon/countlyCommon/Entities/Segmentation.cs index cf626a3f..2de5aa93 100644 --- a/countlyCommon/countlyCommon/Entities/Segmentation.cs +++ b/countlyCommon/countlyCommon/Entities/Segmentation.cs @@ -53,7 +53,18 @@ public Segmentation() /// Segmenation value public void Add(string Key, string Value) { - segmentation.Add(new SegmentationItem(Key, Value)); + // Check if a segmentation item with the same key already exists + var existingItem = segmentation.Find(item => item.Key == Key); + if (existingItem != null) + { + // Update the value if the key exists + existingItem.Value = Value; + } + else + { + // Add a new item if the key doesn't exist + segmentation.Add(new SegmentationItem(Key, Value)); + } } public int CompareTo(Segmentation other) From e8bf63ee15a8832a83720bc28f062bc642b89a64 Mon Sep 17 00:00:00 2001 From: arifBurakDemiray Date: Fri, 26 Jul 2024 11:40:23 +0000 Subject: [PATCH 2/6] feat: add test cases --- countlyCommon/TestingRelated/EventTests.cs | 27 ++++++ .../TestingRelated/SegmentationTests.cs | 92 +++++++++++++++++++ .../countlyCommon/Entities/Segmentation.cs | 15 +-- net35/CountlyTest_35/CountlyTest_35.csproj | 3 + net45/CountlyTest_45/CountlyTest_45.csproj | 3 + netstd/CountlyTest_461/CountlyTest_461.csproj | 3 + 6 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 countlyCommon/TestingRelated/SegmentationTests.cs diff --git a/countlyCommon/TestingRelated/EventTests.cs b/countlyCommon/TestingRelated/EventTests.cs index 06eac5ad..dcba5821 100644 --- a/countlyCommon/TestingRelated/EventTests.cs +++ b/countlyCommon/TestingRelated/EventTests.cs @@ -75,6 +75,33 @@ public async void TestEventLimits() Countly.Instance.SessionEnd().Wait(); } + [Fact] + /// + /// It validates that segmentation cannot have same keys + /// + public async void TestEventSameSegmentationKey() + { + CountlyConfig cc = TestHelper.CreateConfig(); + + Countly.Instance.Init(cc).Wait(); + Countly.Instance.SessionBegin().Wait(); + + Countly.Instance.deferUpload = true; + + Segmentation segm = new Segmentation(); + segm.Add("key1", "value1"); + segm.Add("key2", "value2"); + segm.Add("key2", "value3"); + + Assert.True(segm.segmentation.Count == 2); // not 3 becasue key 2 overridden and segmentation does not permit same keys + bool res = await Countly.RecordEvent("test_event", 1, 23, 5.0, Segmentation: segm); + Assert.True(res); + + CountlyEvent countlyEvent = Countly.Instance.Events[0]; + validateEventData(countlyEvent, "test_event", 1, 23, 5, segm); + Countly.Instance.SessionEnd().Wait(); + } + /// /// It validates the cancellation of timed events on changing device id without merge. /// diff --git a/countlyCommon/TestingRelated/SegmentationTests.cs b/countlyCommon/TestingRelated/SegmentationTests.cs new file mode 100644 index 00000000..a9aec94e --- /dev/null +++ b/countlyCommon/TestingRelated/SegmentationTests.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using CountlySDK; +using CountlySDK.Entities; +using Xunit; +using static CountlySDK.CountlyCommon.CountlyBase; + +namespace TestProject_common +{ + public class SegmentationTests : IDisposable + { + /// + /// Test setup + /// + public SegmentationTests() + { + CountlyImpl.SetPCLStorageIfNeeded(); + Countly.Halt(); + TestHelper.CleanDataFiles(); + Countly.Instance.deferUpload = true; + } + + /// + /// Test cleanup + /// + public void Dispose() + { + Countly.Instance.HaltInternal().Wait(); + } + + [Fact] + /// + /// It validates the invalid key values + /// All values are accepted + /// Only validated invalid keys + /// + public void InvalidKeyValues() + { + Segmentation segmentation = new Segmentation(); + + segmentation.Add(null, null); + Assert.Empty(segmentation.segmentation); + + segmentation.Add("", null); + Assert.Empty(segmentation.segmentation); + + segmentation.Add(" ", null); + Assert.Equal(" ", segmentation.segmentation[0].Key); + Assert.Null(segmentation.segmentation[0].Value); + } + + [Fact] + /// + /// It validates valid values + /// + public void ValidKeyValues() + { + Segmentation segmentation = new Segmentation(); + + segmentation.Add("1", "2"); + Assert.Equal("1", segmentation.segmentation[0].Key); + Assert.Equal("2", segmentation.segmentation[0].Value); + + segmentation.Add("3", "4"); + Assert.Equal("3", segmentation.segmentation[1].Key); + Assert.Equal("4", segmentation.segmentation[1].Value); + Assert.Equal(2, segmentation.segmentation.Count); + + } + + [Fact] + /// + /// It validates valid values with same keys, + /// this test shows no same keys accepted + /// + public void ValidKeyValues_SameKeys() + { + Segmentation segmentation = new Segmentation(); + + segmentation.Add("1", "2"); + Assert.Equal("1", segmentation.segmentation[0].Key); + Assert.Equal("2", segmentation.segmentation[0].Value); + + segmentation.Add("1", "4"); + Assert.Equal("1", segmentation.segmentation[0].Key); + Assert.Equal("4", segmentation.segmentation[0].Value); + Assert.Single(segmentation.segmentation); + + } + + } +} diff --git a/countlyCommon/countlyCommon/Entities/Segmentation.cs b/countlyCommon/countlyCommon/Entities/Segmentation.cs index 2de5aa93..12afe536 100644 --- a/countlyCommon/countlyCommon/Entities/Segmentation.cs +++ b/countlyCommon/countlyCommon/Entities/Segmentation.cs @@ -47,21 +47,22 @@ public Segmentation() } /// - /// Add new segmentation value + /// Add new segmentation value, omits the null keys + /// overrides the same keys /// /// Segmenation key /// Segmenation value public void Add(string Key, string Value) { + if (string.IsNullOrEmpty(Key)) { + return; + } // Check if a segmentation item with the same key already exists - var existingItem = segmentation.Find(item => item.Key == Key); - if (existingItem != null) - { + SegmentationItem existingItem = segmentation.Find(item => item.Key == Key); + if (existingItem != null) { // Update the value if the key exists existingItem.Value = Value; - } - else - { + } else { // Add a new item if the key doesn't exist segmentation.Add(new SegmentationItem(Key, Value)); } diff --git a/net35/CountlyTest_35/CountlyTest_35.csproj b/net35/CountlyTest_35/CountlyTest_35.csproj index d2c982f9..96291580 100644 --- a/net35/CountlyTest_35/CountlyTest_35.csproj +++ b/net35/CountlyTest_35/CountlyTest_35.csproj @@ -86,6 +86,9 @@ RequestTestCases.cs + + SegmentationTests.cs + SessionTests.cs diff --git a/net45/CountlyTest_45/CountlyTest_45.csproj b/net45/CountlyTest_45/CountlyTest_45.csproj index d366c9ff..86af235b 100644 --- a/net45/CountlyTest_45/CountlyTest_45.csproj +++ b/net45/CountlyTest_45/CountlyTest_45.csproj @@ -85,6 +85,9 @@ RequestTestCases.cs + + SegmentationTests.cs + SessionTests.cs diff --git a/netstd/CountlyTest_461/CountlyTest_461.csproj b/netstd/CountlyTest_461/CountlyTest_461.csproj index 8de5b8fb..7165d95f 100644 --- a/netstd/CountlyTest_461/CountlyTest_461.csproj +++ b/netstd/CountlyTest_461/CountlyTest_461.csproj @@ -96,6 +96,9 @@ RequestTestCases.cs + + SegmentationTests.cs + SessionTests.cs From b9ecbf65cd64b7a0305c34c58013498022b3a113 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 26 Jul 2024 14:44:15 +0300 Subject: [PATCH 3/6] feat: changelog for it --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9dc8efd..3783dc8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ -## XX.XX.XX +## 24.1.1 +* !! Major breaking change !! Same, null, and empty keys are now prohibited for Segmentation. + * Fixed an issue where some requests are not url encoded. ## 24.1.0 From 2af21a7fa60fd731af4651063dbed958beae4660 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray <57103426+arifBurakDemiray@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:38:41 +0300 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3783dc8e..b3a86e4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,5 @@ ## 24.1.1 -* !! Major breaking change !! Same, null, and empty keys are now prohibited for Segmentation. - +* Fixed a bug where same, null, and empty keys were permitted in the Segmentation, not anymore. * Fixed an issue where some requests are not url encoded. ## 24.1.0 From e609643094cb27c9d8534d86fc2d85f5ed93ffe9 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray <57103426+arifBurakDemiray@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:40:21 +0300 Subject: [PATCH 5/6] Update EventTests.cs --- countlyCommon/TestingRelated/EventTests.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/countlyCommon/TestingRelated/EventTests.cs b/countlyCommon/TestingRelated/EventTests.cs index dcba5821..30a00914 100644 --- a/countlyCommon/TestingRelated/EventTests.cs +++ b/countlyCommon/TestingRelated/EventTests.cs @@ -93,6 +93,11 @@ public async void TestEventSameSegmentationKey() segm.Add("key2", "value2"); segm.Add("key2", "value3"); + Assert.Equal("key1", segm.segmentation[0].Key); + Assert.Equal("value1", segm.segmentation[0].Value); + Assert.Equal("key2", segm.segmentation[1].Key); + Assert.Equal("value3", segm.segmentation[1].Value); + Assert.True(segm.segmentation.Count == 2); // not 3 becasue key 2 overridden and segmentation does not permit same keys bool res = await Countly.RecordEvent("test_event", 1, 23, 5.0, Segmentation: segm); Assert.True(res); From 9aa2b2139dea9bd416497c8c60a78b7d3d789606 Mon Sep 17 00:00:00 2001 From: turtledreams <62231246+turtledreams@users.noreply.github.com> Date: Mon, 29 Jul 2024 15:16:31 +0900 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3a86e4a..1ca561e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## 24.1.1 -* Fixed a bug where same, null, and empty keys were permitted in the Segmentation, not anymore. +* Fixed a bug where same, null, and empty keys were permitted in the Segmentation. * Fixed an issue where some requests are not url encoded. ## 24.1.0