From 179b5faf7932093f99f636b9c604f53837c717c4 Mon Sep 17 00:00:00 2001 From: Mastermindzh Date: Mon, 30 Oct 2023 21:22:43 +0100 Subject: [PATCH] ci: moved from MSTest to Xunit --- .github/workflows/ci.yml | 4 +- CHANGELOG.md | 4 + GrowthBook.Tests/GrowthBook.Tests.csproj | 51 ++++ {Test => GrowthBook.Tests}/GrowthBookTests.cs | 140 +++++------ .../Json/FeatureDictionary.json | 0 .../Json/GrowthBookContext.NoFeatures.json | 0 .../Json/GrowthBookContext.json | 0 .../Json/JsonTestHelpers.cs | 2 +- ...SingleFeatureDictionary.WithNameSpace.json | 0 .../Json/custom-cases.json | 0 .../Json/standard-cases.json | 0 .../NamespaceTupleConverterTests.cs | 26 +- .../UtilitiesTests.cs | 230 +++++++++--------- GrowthBook/GrowthBook.csproj | 2 +- Test/GrowthBook.Tests.csproj | 41 ---- Test/packages.config | 6 - 16 files changed, 261 insertions(+), 245 deletions(-) create mode 100644 GrowthBook.Tests/GrowthBook.Tests.csproj rename {Test => GrowthBook.Tests}/GrowthBookTests.cs (50%) rename {Test => GrowthBook.Tests}/Json/FeatureDictionary.json (100%) rename {Test => GrowthBook.Tests}/Json/GrowthBookContext.NoFeatures.json (100%) rename {Test => GrowthBook.Tests}/Json/GrowthBookContext.json (100%) rename {Test => GrowthBook.Tests}/Json/JsonTestHelpers.cs (96%) rename Test/Json/SingleFeatureDictionary.WithNameSpace..json => GrowthBook.Tests/Json/SingleFeatureDictionary.WithNameSpace.json (100%) rename {Test => GrowthBook.Tests}/Json/custom-cases.json (100%) rename {Test => GrowthBook.Tests}/Json/standard-cases.json (100%) rename {Test => GrowthBook.Tests}/NamespaceTupleConverterTests.cs (81%) rename Test/Utilities.cs => GrowthBook.Tests/UtilitiesTests.cs (63%) delete mode 100644 Test/GrowthBook.Tests.csproj delete mode 100644 Test/packages.config diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4778c8e..dfd7e70 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,8 +25,8 @@ jobs: run: dotnet build GrowthBook/GrowthBook.csproj --configuration Release # current tests are written for visual studio only - # - name: Test - # run: dotnet test Test/Growthook.Tests.csproj --configuration Release --no-build + - name: Test + run: dotnet test GrowthBook.Tests/GrowthBook.Tests.csproj --logger:"console;verbosity=normal" - name: pack run: dotnet pack --configuration Release --no-build --output dist GrowthBook/GrowthBook.csproj diff --git a/CHANGELOG.md b/CHANGELOG.md index d635173..6848e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.2] + +- ci: moved from MSTest to Xunit + ## [0.1.1] - Handle null namespace property. diff --git a/GrowthBook.Tests/GrowthBook.Tests.csproj b/GrowthBook.Tests/GrowthBook.Tests.csproj new file mode 100644 index 0000000..e9b5283 --- /dev/null +++ b/GrowthBook.Tests/GrowthBook.Tests.csproj @@ -0,0 +1,51 @@ + + + + net7.0 + latest + + + + AnyCPU + + + + + + + + + + + + + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Test/GrowthBookTests.cs b/GrowthBook.Tests/GrowthBookTests.cs similarity index 50% rename from Test/GrowthBookTests.cs rename to GrowthBook.Tests/GrowthBookTests.cs index ceac6ff..a21d4c5 100644 --- a/Test/GrowthBookTests.cs +++ b/GrowthBook.Tests/GrowthBookTests.cs @@ -1,57 +1,40 @@ using System; using System.Collections.Generic; -using System.IO; +using System.Linq; using System.Reflection; -using GrowthBook; -using Growthbook.Tests.Json; -using Microsoft.VisualStudio.TestTools.UnitTesting; + +using GrowthBook.Tests.Json; + using Newtonsoft.Json.Linq; -using Test; -namespace Growthbook.Tests +using Xunit; + +namespace GrowthBook.Tests { - [TestClass] public class GrowthBookTests { - public static JObject testCases; - public static JObject customCases; - [ClassInitialize] - public static void TestFixtureSetup(TestContext context) + #region helpers + public static JObject getStandardCases() { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } - - testCases = JObject.Parse(JsonTestHelpers.GetTestJson("standard-cases")); - customCases = JObject.Parse(JsonTestHelpers.GetTestJson("custom-cases")); + return JObject.Parse(JsonTestHelpers.GetTestJson("standard-cases")); } - public static string GetTestNames(MethodInfo methodInfo, object[] values) + public static JObject getCustomCases() { - return $"{methodInfo.Name} - { values[0] }"; + return JObject.Parse(JsonTestHelpers.GetTestJson("custom-cases")); } - [TestMethod] - [DynamicData(nameof(RunTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void Run(string testname, Context context, Experiment experiment, JToken expectedValue, bool inExperiment, bool hashUsed) + public static string GetTestNames(MethodInfo methodInfo, object[] values) { - if (testname is null) - { - throw new ArgumentNullException(nameof(testname)); - } - - GrowthBook.GrowthBook gb = new GrowthBook.GrowthBook(context); - ExperimentResult actual = gb.Run(experiment); - Assert.AreEqual(inExperiment, actual.InExperiment); - Assert.AreEqual(hashUsed, actual.HashUsed); - Assert.IsTrue(JToken.DeepEquals(actual.Value, expectedValue)); + return $"{methodInfo.Name} - {values[0]}"; } + #endregion + #region data public static IEnumerable RunTests() { - foreach (JArray testCase in (JArray)testCases["run"]) + foreach (JArray testCase in (JArray)getStandardCases()["run"]) { yield return new object[] { testCase[0].ToString(), @@ -63,49 +46,62 @@ public static IEnumerable RunTests() }; } } + public static IEnumerable EvalFeatureTests() + { + foreach (JArray testCase in (JArray)getStandardCases()["feature"]) + { + yield return new object[] { + testCase[0].ToString(), + testCase[1].ToObject(), + testCase[2].ToString(), + testCase[3].ToObject(), + }; + } + } + #endregion - [TestMethod] + [Fact] public void Run_ShouldCallTrackingCallbackOnce() { - JArray testCase = (JArray)customCases["run"]; + JArray testCase = (JArray)getCustomCases()["run"]; int trackingCounter = 0; Context context = testCase[0].ToObject(); context.TrackingCallback = (Experiment experiment, ExperimentResult result) => { - Assert.IsTrue(JToken.DeepEquals(result.Value, testCase[2])); - Assert.AreEqual(testCase[3].ToObject(), result.InExperiment); - Assert.AreEqual(testCase[4].ToObject(), result.HashUsed); + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); trackingCounter++; }; - GrowthBook.GrowthBook gb = new GrowthBook.GrowthBook(context); + GrowthBook gb = new GrowthBook(context); gb.Run(testCase[1].ToObject()); gb.Run(testCase[1].ToObject()); - Assert.AreEqual(1, trackingCounter); + Assert.Equal(1, trackingCounter); } - [TestMethod] + [Fact] public void Run_ShouldCallSubscribedCallbacks() { - JArray testCase = (JArray)customCases["run"]; - GrowthBook.GrowthBook gb = new GrowthBook.GrowthBook(testCase[0].ToObject()); + JArray testCase = (JArray)getCustomCases()["run"]; + GrowthBook gb = new GrowthBook(testCase[0].ToObject()); int subCounterOne = 0; gb.Subscribe((Experiment experiment, ExperimentResult result) => { - Assert.IsTrue(JToken.DeepEquals(result.Value, testCase[2])); - Assert.AreEqual(testCase[3].ToObject(), result.InExperiment); - Assert.AreEqual(testCase[4].ToObject(), result.HashUsed); + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); subCounterOne++; }); int subCounterTwo = 0; Action unsubscribe = gb.Subscribe((Experiment experiment, ExperimentResult result) => { - Assert.IsTrue(JToken.DeepEquals(result.Value, testCase[2])); - Assert.AreEqual(testCase[3].ToObject(), result.InExperiment); - Assert.AreEqual(testCase[4].ToObject(), result.HashUsed); + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); subCounterTwo++; }); unsubscribe(); @@ -113,45 +109,49 @@ public void Run_ShouldCallSubscribedCallbacks() int subCounterThree = 0; gb.Subscribe((Experiment experiment, ExperimentResult result) => { - Assert.IsTrue(JToken.DeepEquals(result.Value, testCase[2])); - Assert.AreEqual(testCase[3].ToObject(), result.InExperiment); - Assert.AreEqual(testCase[4].ToObject(), result.HashUsed); + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); subCounterThree++; }); gb.Run(testCase[1].ToObject()); gb.Run(testCase[1].ToObject()); gb.Run(testCase[1].ToObject()); - Assert.AreEqual(1, subCounterOne); - Assert.AreEqual(0, subCounterTwo); - Assert.AreEqual(1, subCounterThree); + Assert.Equal(1, subCounterOne); + Assert.Equal(0, subCounterTwo); + Assert.Equal(1, subCounterThree); } - [TestMethod] - [DynamicData(nameof(EvalFeatureTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void EvalFeature(string testname, Context context, string key, FeatureResult expected) + [Theory] + [MemberData(nameof(RunTests))] + public void Run(string testname, Context context, Experiment experiment, JToken expectedValue, bool inExperiment, bool hashUsed) { if (testname is null) { throw new ArgumentNullException(nameof(testname)); } - GrowthBook.GrowthBook gb = new GrowthBook.GrowthBook(context); - FeatureResult actual = gb.EvalFeature(key); - Assert.AreEqual(expected, actual); + GrowthBook gb = new GrowthBook(context); + ExperimentResult actual = gb.Run(experiment); + Assert.Equal(inExperiment, actual.InExperiment); + Assert.Equal(hashUsed, actual.HashUsed); + Assert.True(JToken.DeepEquals(actual.Value, expectedValue)); } - public static IEnumerable EvalFeatureTests() + [Theory] + [MemberData(nameof(EvalFeatureTests))] + public void EvalFeature(string testname, Context context, string key, FeatureResult expected) { - foreach (JArray testCase in (JArray)testCases["feature"]) + if (testname is null) { - yield return new object[] { - testCase[0].ToString(), - testCase[1].ToObject(), - testCase[2].ToString(), - testCase[3].ToObject(), - }; + throw new ArgumentNullException(nameof(testname)); } + + GrowthBook gb = new GrowthBook(context); + FeatureResult actual = gb.EvalFeature(key); + Assert.Equal(expected, actual); } + } } diff --git a/Test/Json/FeatureDictionary.json b/GrowthBook.Tests/Json/FeatureDictionary.json similarity index 100% rename from Test/Json/FeatureDictionary.json rename to GrowthBook.Tests/Json/FeatureDictionary.json diff --git a/Test/Json/GrowthBookContext.NoFeatures.json b/GrowthBook.Tests/Json/GrowthBookContext.NoFeatures.json similarity index 100% rename from Test/Json/GrowthBookContext.NoFeatures.json rename to GrowthBook.Tests/Json/GrowthBookContext.NoFeatures.json diff --git a/Test/Json/GrowthBookContext.json b/GrowthBook.Tests/Json/GrowthBookContext.json similarity index 100% rename from Test/Json/GrowthBookContext.json rename to GrowthBook.Tests/Json/GrowthBookContext.json diff --git a/Test/Json/JsonTestHelpers.cs b/GrowthBook.Tests/Json/JsonTestHelpers.cs similarity index 96% rename from Test/Json/JsonTestHelpers.cs rename to GrowthBook.Tests/Json/JsonTestHelpers.cs index 769e086..705d647 100644 --- a/Test/Json/JsonTestHelpers.cs +++ b/GrowthBook.Tests/Json/JsonTestHelpers.cs @@ -2,7 +2,7 @@ using System.IO; using System.Reflection; -namespace Growthbook.Tests.Json; +namespace GrowthBook.Tests.Json; public static class JsonTestHelpers { diff --git a/Test/Json/SingleFeatureDictionary.WithNameSpace..json b/GrowthBook.Tests/Json/SingleFeatureDictionary.WithNameSpace.json similarity index 100% rename from Test/Json/SingleFeatureDictionary.WithNameSpace..json rename to GrowthBook.Tests/Json/SingleFeatureDictionary.WithNameSpace.json diff --git a/Test/Json/custom-cases.json b/GrowthBook.Tests/Json/custom-cases.json similarity index 100% rename from Test/Json/custom-cases.json rename to GrowthBook.Tests/Json/custom-cases.json diff --git a/Test/Json/standard-cases.json b/GrowthBook.Tests/Json/standard-cases.json similarity index 100% rename from Test/Json/standard-cases.json rename to GrowthBook.Tests/Json/standard-cases.json diff --git a/Test/NamespaceTupleConverterTests.cs b/GrowthBook.Tests/NamespaceTupleConverterTests.cs similarity index 81% rename from Test/NamespaceTupleConverterTests.cs rename to GrowthBook.Tests/NamespaceTupleConverterTests.cs index 51a0713..d2b044f 100644 --- a/Test/NamespaceTupleConverterTests.cs +++ b/GrowthBook.Tests/NamespaceTupleConverterTests.cs @@ -1,15 +1,19 @@ +using System; using System.Collections.Generic; -using GrowthBook; -using Growthbook.Tests.Json; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Reflection; + +using GrowthBook.Tests.Json; + using Newtonsoft.Json; -namespace Test +using Xunit; + +namespace GrowthBook.Tests { - [TestClass] public class NamespaceTupleConverterTests { - [TestMethod] + [Fact] public void CreateFromJson_NoFeatures_ShouldSucceed() { string json = JsonTestHelpers.GetTestJson("GrowthBookContext.NoFeatures"); @@ -17,7 +21,7 @@ public void CreateFromJson_NoFeatures_ShouldSucceed() var gb = JsonConvert.DeserializeObject(json); } - [TestMethod] + [Fact] public void CreateFromJson_WithFeatures_ShouldSucceed() { string json = JsonTestHelpers.GetTestJson("GrowthBookContext"); @@ -25,7 +29,7 @@ public void CreateFromJson_WithFeatures_ShouldSucceed() var gb = JsonConvert.DeserializeObject(json); } - [TestMethod] + [Fact] public void CreateFeaturesFromJson_WithFeatures_ShouldSucceed() { string json = JsonTestHelpers.GetTestJson("FeatureDictionary"); @@ -33,13 +37,15 @@ public void CreateFeaturesFromJson_WithFeatures_ShouldSucceed() var gb = JsonConvert.DeserializeObject>(json); } - [TestMethod] + [Fact] public void CreateFeaturesFromJson_OneFeatureWithNameSpace_ShouldSucceed() { - string json = JsonTestHelpers.GetTestJson("SingleFeatureDictionary.WithNameSpace."); + string json = JsonTestHelpers.GetTestJson("SingleFeatureDictionary.WithNameSpace"); var gb = JsonConvert.DeserializeObject>(json); } + } + } diff --git a/Test/Utilities.cs b/GrowthBook.Tests/UtilitiesTests.cs similarity index 63% rename from Test/Utilities.cs rename to GrowthBook.Tests/UtilitiesTests.cs index 7d8b508..29dacd8 100644 --- a/Test/Utilities.cs +++ b/GrowthBook.Tests/UtilitiesTests.cs @@ -1,35 +1,29 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Reflection; -using GrowthBook; -using Growthbook.Tests.Json; -using Microsoft.VisualStudio.TestTools.UnitTesting; + +using GrowthBook.Tests.Json; + using Newtonsoft.Json.Linq; -using Test; -namespace Growthbook.Tests +using Xunit; + +namespace GrowthBook.Tests { - [TestClass] - public class Utilities + public class UtilitiesTests { - public static JObject testCases; - [ClassInitialize] - public static void TestFixtureSetup(TestContext context) - { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + #region helpers - testCases = JObject.Parse(JsonTestHelpers.GetTestJson("standard-cases")); + public static JObject getStandardCases() + { + return JObject.Parse(JsonTestHelpers.GetTestJson("standard-cases")); } public static string GetTestNames(MethodInfo methodInfo, object[] values) { - return $"{methodInfo.Name} - { values[0] }"; + return $"{methodInfo.Name} - {values[0]}"; } public double RoundStandard(double input) @@ -56,42 +50,48 @@ public IList RoundBucketRanges(IList input) } return results; } - - [TestMethod] - [DynamicData(nameof(HashTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void Hash(string input, double expected) - { - double actual = GrowthBook.Utilities.Hash(input); - Assert.AreEqual(expected, actual); - } - - public static IEnumerable HashTests() + #endregion + #region data + public static IEnumerable GetBucketRangeTests() { - foreach (JArray testCase in (JArray)testCases["hash"]) + foreach (JArray testCase in (JArray)getStandardCases()["getBucketRange"]) { + List expected = new List(); + foreach (JArray jArray in testCase[2]) + { + expected.Add(new BucketRange(jArray[0].ToObject(), jArray[1].ToObject())); + } yield return new object[] { testCase[0].ToString(), - testCase[1].ToObject() + testCase[1][0].ToObject(), + testCase[1][1].ToObject(), + testCase[1][2].ToObject(), + expected, }; } } - [TestMethod] - [DynamicData(nameof(InNamespaceTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void InNamespace(string testName, string userId, string id, double start, double end, bool expected) + public static IEnumerable ChooseVariationTests() { - if (testName is null) + foreach (JArray testCase in (JArray)getStandardCases()["chooseVariation"]) { - throw new ArgumentNullException(nameof(testName)); + List ranges = new List(); + foreach (JArray jArray in testCase[2]) + { + ranges.Add(new BucketRange(jArray[0].ToObject(), jArray[1].ToObject())); + } + yield return new object[] { + testCase[0].ToString(), + testCase[1].ToObject(), + ranges, + testCase[3].ToObject(), + }; } - - bool actual = GrowthBook.Utilities.InNamespace(userId, new GrowthBook.Namespace(id, start, end)); - Assert.AreEqual(expected, actual); } public static IEnumerable InNamespaceTests() { - foreach (JArray testCase in (JArray)testCases["inNamespace"]) + foreach (JArray testCase in (JArray)getStandardCases()["inNamespace"]) { yield return new object[] { testCase[0].ToString(), @@ -104,139 +104,141 @@ public static IEnumerable InNamespaceTests() } } - [TestMethod] - [DynamicData(nameof(GetEqualWeightsTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void GetEqualWeights(int input, IList expected) + public static IEnumerable GetQueryStringOverrideTests() { - IList actual = GrowthBook.Utilities.GetEqualWeights(input); - Assert.IsTrue(RoundArray(expected).SequenceEqual(RoundArray(actual))); + foreach (JArray testCase in (JArray)getStandardCases()["getQueryStringOverride"]) + { + yield return new object[] { + testCase[0].ToString(), + testCase[1].ToString(), + testCase[2].ToString(), + testCase[3].ToObject(), + testCase[4].ToObject(), + }; + } } - public static IEnumerable GetEqualWeightsTests() + public static IEnumerable EvalConditionTests() { - foreach (JArray testCase in (JArray)testCases["getEqualWeights"]) + + foreach (JArray testCase in (JArray)getStandardCases()["evalCondition"]) { yield return new object[] { - testCase[0].ToObject(), - testCase[1].ToObject(), + testCase[0].ToString(), + testCase[1], + testCase[2], + testCase[3].ToObject() }; } } - [TestMethod] - [DynamicData(nameof(GetBucketRangeTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void GetBucketRanges(string testName, int numVariations, double coverage, double[] weights, List expected) + public static IEnumerable GetEqualWeightsTests() { - if (testName is null) + foreach (JArray testCase in (JArray)getStandardCases()["getEqualWeights"]) { - throw new ArgumentNullException(nameof(testName)); + yield return new object[] { + testCase[0].ToObject(), + testCase[1].ToObject(), + }; } - - IList actual = GrowthBook.Utilities.GetBucketRanges(numVariations, coverage, weights); - Assert.IsTrue(RoundBucketRanges(expected).SequenceEqual(RoundBucketRanges(actual))); } - public static IEnumerable GetBucketRangeTests() + + public static IEnumerable HashTests() { - foreach (JArray testCase in (JArray)testCases["getBucketRange"]) + foreach (JArray testCase in (JArray)getStandardCases()["hash"]) { - List expected = new List(); - foreach (JArray jArray in testCase[2]) - { - expected.Add(new BucketRange(jArray[0].ToObject(), jArray[1].ToObject())); - } yield return new object[] { testCase[0].ToString(), - testCase[1][0].ToObject(), - testCase[1][1].ToObject(), - testCase[1][2].ToObject(), - expected, + testCase[1].ToObject() }; } } + #endregion - [TestMethod] - [DynamicData(nameof(ChooseVariationTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void ChooseVariation(string testName, double n, List ranges, int expected) + + [Theory] + [MemberData(nameof(EvalConditionTests))] + public void EvalCondition(string testName, JObject condition, JToken attributes, bool expected) { if (testName is null) { throw new ArgumentNullException(nameof(testName)); } - int actual = GrowthBook.Utilities.ChooseVariation(n, ranges); - Assert.AreEqual(expected, actual); + bool actual = Utilities.EvalCondition(attributes, condition); + Assert.Equal(expected, actual); } - public static IEnumerable ChooseVariationTests() + + [Theory] + [MemberData(nameof(HashTests))] + public void Hash(string input, double expected) { - foreach (JArray testCase in (JArray)testCases["chooseVariation"]) - { - List ranges = new List(); - foreach (JArray jArray in testCase[2]) - { - ranges.Add(new BucketRange(jArray[0].ToObject(), jArray[1].ToObject())); - } - yield return new object[] { - testCase[0].ToString(), - testCase[1].ToObject(), - ranges, - testCase[3].ToObject(), - }; - } + double actual = Utilities.Hash(input); + Assert.Equal(expected, actual); } - [TestMethod] - [DynamicData(nameof(GetQueryStringOverrideTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void GetQueryStringOverride(string testName, string id, string url, int numVariations, int? expected) + [Theory] + [MemberData(nameof(InNamespaceTests))] + public void InNamespace(string testName, string userId, string id, double start, double end, bool expected) { if (testName is null) { throw new ArgumentNullException(nameof(testName)); } - int? actual = GrowthBook.Utilities.GetQueryStringOverride(id, url, numVariations); - Assert.AreEqual(expected, actual); + bool actual = Utilities.InNamespace(userId, new Namespace(id, start, end)); + Assert.Equal(expected, actual); } - public static IEnumerable GetQueryStringOverrideTests() + [Theory] + [MemberData(nameof(GetEqualWeightsTests))] + public void GetEqualWeights(int input, IList expected) + { + IList actual = Utilities.GetEqualWeights(input); + Assert.True(RoundArray(expected).SequenceEqual(RoundArray(actual))); + } + + + [Theory] + [MemberData(nameof(GetBucketRangeTests))] + public void GetBucketRanges(string testName, int numVariations, double coverage, double[] weights, List expected) { - foreach (JArray testCase in (JArray)testCases["getQueryStringOverride"]) + if (testName is null) { - yield return new object[] { - testCase[0].ToString(), - testCase[1].ToString(), - testCase[2].ToString(), - testCase[3].ToObject(), - testCase[4].ToObject(), - }; + throw new ArgumentNullException(nameof(testName)); } + + IList actual = Utilities.GetBucketRanges(numVariations, coverage, weights); + Assert.True(RoundBucketRanges(expected).SequenceEqual(RoundBucketRanges(actual))); } - [TestMethod] - [DynamicData(nameof(EvalConditionTests), DynamicDataSourceType.Method, DynamicDataDisplayName = nameof(GetTestNames))] - public void EvalCondition(string testName, JObject condition, JToken attributes, bool expected) + [Theory] + [MemberData(nameof(ChooseVariationTests))] + public void ChooseVariation(string testName, double n, List ranges, int expected) { if (testName is null) { throw new ArgumentNullException(nameof(testName)); } - bool actual = GrowthBook.Utilities.EvalCondition(attributes, condition); - Assert.AreEqual(expected, actual); + int actual = Utilities.ChooseVariation(n, ranges); + Assert.Equal(expected, actual); } - public static IEnumerable EvalConditionTests() + [Theory] + [MemberData(nameof(GetQueryStringOverrideTests))] + public void GetQueryStringOverride(string testName, string id, string url, int numVariations, int? expected) { - foreach (JArray testCase in (JArray)testCases["evalCondition"]) + if (testName is null) { - yield return new object[] { - testCase[0].ToString(), - testCase[1], - testCase[2], - testCase[3].ToObject() - }; + throw new ArgumentNullException(nameof(testName)); } + + int? actual = Utilities.GetQueryStringOverride(id, url, numVariations); + Assert.Equal(expected, actual); } + } } diff --git a/GrowthBook/GrowthBook.csproj b/GrowthBook/GrowthBook.csproj index 33307ad..8cccb65 100644 --- a/GrowthBook/GrowthBook.csproj +++ b/GrowthBook/GrowthBook.csproj @@ -11,7 +11,7 @@ https://github.com/growthbook/growthbook-csharp.git git GrowthBook,A/B test,feature toggle - 0.1.1 + 0.1.2 diff --git a/Test/GrowthBook.Tests.csproj b/Test/GrowthBook.Tests.csproj deleted file mode 100644 index ea910a8..0000000 --- a/Test/GrowthBook.Tests.csproj +++ /dev/null @@ -1,41 +0,0 @@ - - - - net6 - latest - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Test/packages.config b/Test/packages.config deleted file mode 100644 index 4928b4b..0000000 --- a/Test/packages.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file