diff --git a/Tests/Realm.Tests/Sync/StaticQueriesTests.cs b/Tests/Realm.Tests/Sync/StaticQueriesTests.cs index f4864edec7..094aee8a50 100644 --- a/Tests/Realm.Tests/Sync/StaticQueriesTests.cs +++ b/Tests/Realm.Tests/Sync/StaticQueriesTests.cs @@ -93,10 +93,10 @@ void AssertCollectionProps(SyncCollectionsObject expected, SyncCollectionsObject }, timeout: 120000); } - public static readonly object[] PrimitiveTestCases = + public static IEnumerable PrimitiveTestCases() { - new object[] { CreateTestCase("Empty object", new SyncAllTypesObject()) }, - new object[] + yield return new object[] { CreateTestCase("Empty object", new SyncAllTypesObject()) }; + yield return new object[] { CreateTestCase("All values", new SyncAllTypesObject { @@ -116,21 +116,21 @@ void AssertCollectionProps(SyncCollectionsObject expected, SyncCollectionsObject RealmValueProperty = "this is a string", StringProperty = "foo bar" }) - }, - new object[] { CreateTestCase("Bool RealmValue", new SyncAllTypesObject { RealmValueProperty = true }) }, - new object[] { CreateTestCase("Int RealmValue", new SyncAllTypesObject { RealmValueProperty = 123 }) }, - new object[] { CreateTestCase("Long RealmValue", new SyncAllTypesObject { RealmValueProperty = 9999999999 }) }, - new object[] { CreateTestCase("Null RealmValue", new SyncAllTypesObject { RealmValueProperty = RealmValue.Null }) }, - new object[] { CreateTestCase("String RealmValue", new SyncAllTypesObject { RealmValueProperty = "abc" }) }, - new object[] { CreateTestCase("Data RealmValue", new SyncAllTypesObject { RealmValueProperty = GetBytes(10) }) }, - new object[] { CreateTestCase("Float RealmValue", new SyncAllTypesObject { RealmValueProperty = 15.2f }) }, - new object[] { CreateTestCase("Double RealmValue", new SyncAllTypesObject { RealmValueProperty = -123.45678909876 }) }, - new object[] { CreateTestCase("Decimal RealmValue", new SyncAllTypesObject { RealmValueProperty = 1.1111111111111111111M }) }, - new object[] { CreateTestCase("Decimal RealmValue", new SyncAllTypesObject { RealmValueProperty = 1.1111111111111111111M }) }, - new object[] { CreateTestCase("Decimal128 RealmValue", new SyncAllTypesObject { RealmValueProperty = new Decimal128(2.1111111111111111111M) }) }, - new object[] { CreateTestCase("ObjectId RealmValue", new SyncAllTypesObject { RealmValueProperty = ObjectId.GenerateNewId() }) }, - new object[] { CreateTestCase("Guid RealmValue", new SyncAllTypesObject { RealmValueProperty = Guid.NewGuid() }) }, - }; + }; + yield return new object[] { CreateTestCase("Bool RealmValue", new SyncAllTypesObject { RealmValueProperty = true }) }; + yield return new object[] { CreateTestCase("Int RealmValue", new SyncAllTypesObject { RealmValueProperty = 123 }) }; + yield return new object[] { CreateTestCase("Long RealmValue", new SyncAllTypesObject { RealmValueProperty = 9999999999 }) }; + yield return new object[] { CreateTestCase("Null RealmValue", new SyncAllTypesObject { RealmValueProperty = RealmValue.Null }) }; + yield return new object[] { CreateTestCase("String RealmValue", new SyncAllTypesObject { RealmValueProperty = "abc" }) }; + yield return new object[] { CreateTestCase("Data RealmValue", new SyncAllTypesObject { RealmValueProperty = GetBytes(10) }) }; + yield return new object[] { CreateTestCase("Float RealmValue", new SyncAllTypesObject { RealmValueProperty = 15.2f }) }; + yield return new object[] { CreateTestCase("Double RealmValue", new SyncAllTypesObject { RealmValueProperty = -123.45678909876 }) }; + yield return new object[] { CreateTestCase("Decimal RealmValue", new SyncAllTypesObject { RealmValueProperty = 1.1111111111111111111M }) }; + yield return new object[] { CreateTestCase("Decimal RealmValue", new SyncAllTypesObject { RealmValueProperty = 1.1111111111111111111M }) }; + yield return new object[] { CreateTestCase("Decimal128 RealmValue", new SyncAllTypesObject { RealmValueProperty = new Decimal128(2.1111111111111111111M) }) }; + yield return new object[] { CreateTestCase("ObjectId RealmValue", new SyncAllTypesObject { RealmValueProperty = ObjectId.GenerateNewId() }) }; + yield return new object[] { CreateTestCase("Guid RealmValue", new SyncAllTypesObject { RealmValueProperty = Guid.NewGuid() }) }; + } [TestCaseSource(nameof(PrimitiveTestCases))] public void RealmObjectAPI_Primitive_AtlasToRealm(TestCaseData testCase) @@ -269,9 +269,9 @@ public void RealmObjectAPI_DateTime_RealmToAtlas_SpecialCase() }, timeout: 120000); } - public static readonly object[] CounterTestCases = + public static IEnumerable CounterTestCases() { - new object[] + yield return new object[] { CreateTestCase("All values", new CounterObject { @@ -285,8 +285,8 @@ public void RealmObjectAPI_DateTime_RealmToAtlas_SpecialCase() NullableInt32Property = 49394939, NullableInt64Property = 889898965342443 }) - }, - new object[] + }; + yield return new object[] { CreateTestCase("Nullable values", new CounterObject { @@ -300,8 +300,8 @@ public void RealmObjectAPI_DateTime_RealmToAtlas_SpecialCase() NullableInt32Property = null, NullableInt64Property = null, }) - }, - }; + }; + } [TestCaseSource(nameof(CounterTestCases))] public void RealmObjectAPI_Counter_AtlasToRealm(TestCaseData testCase) @@ -347,13 +347,13 @@ public void RealmObjectAPI_Counter_RealmToAtlas(TestCaseData test }, timeout: 120000); } - public static readonly object[] AsymmetricTestCases = + public static IEnumerable AsymmetricTestCases() { - new object[] + yield return new object[] { CreateTestCase("Base", new BasicAsymmetricObject { PartitionLike = "testString" }) - }, - }; + }; + } [TestCaseSource(nameof(AsymmetricTestCases))] public void RealmObjectAPI_Asymmetric_RealmToAtlas(TestCaseData testCase) @@ -375,16 +375,16 @@ public void RealmObjectAPI_Asymmetric_RealmToAtlas(TestCaseData ObjectTestCases() { - new object[] + yield return new object[] { CreateTestCase("All values", new SyncAllTypesObject { ObjectProperty = new IntPropertyObject { Int = 23 }, }) - }, - }; + }; + } [TestCaseSource(nameof(ObjectTestCases))] public void RealmObjectAPI_Object_AtlasToRealm(TestCaseData testCase) @@ -443,17 +443,17 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData }, timeout: 120000); } - public static readonly object[] LinksTestCases = + public static IEnumerable LinksTestCases() { - new object[] + yield return new object[] { CreateTestCase("Single link", new LinksObject("singleLink") { Link = new("second") { Value = 2 }, Value = 1, }), - }, - new object[] + }; + yield return new object[] { CreateTestCase("List", new LinksObject("listLink") { @@ -464,8 +464,8 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData }, Value = 987 }), - }, - new object[] + }; + yield return new object[] { CreateTestCase("Dictionary", new LinksObject("dictLink") { @@ -477,8 +477,8 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData }, Value = 999 }) - }, - new object[] + }; + yield return new object[] { CreateTestCase("Set", new LinksObject("setLink") { @@ -489,8 +489,8 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData }, Value = 123 }), - }, - new object[] + }; + yield return new object[] { CreateTestCase("All types", new LinksObject("parent") { @@ -513,8 +513,8 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData ["dict_null"] = null } }), - } - }; + }; + } [TestCaseSource(nameof(LinksTestCases))] public void RealmObjectAPI_Links_AtlasToRealm(TestCaseData testCase) @@ -547,6 +547,7 @@ public void RealmObjectAPI_Links_AtlasToRealm(TestCaseData testCase AssertEqual(linkObj!.Link, obj.Link); + await WaitForConditionAsync(() => { return linkObj.List.Count == obj.List.Count; }); Assert.That(linkObj.List.Count, Is.EqualTo(obj.List.Count)); for (int i = 0; i < linkObj.List.Count; i++) @@ -554,14 +555,18 @@ public void RealmObjectAPI_Links_AtlasToRealm(TestCaseData testCase AssertEqual(linkObj.List[i], obj.List[i]); } + await WaitForConditionAsync(() => { return linkObj.Dictionary.Count == obj.Dictionary.Count; }); Assert.That(linkObj.Dictionary.Count, Is.EqualTo(obj.Dictionary.Count)); foreach (var key in obj.Dictionary.Keys) { Assert.That(linkObj.Dictionary.ContainsKey(key)); + + await WaitForConditionAsync(() => { return (linkObj.Dictionary[key] == null) == (obj.Dictionary[key] == null); }); AssertEqual(linkObj.Dictionary[key], obj.Dictionary[key]); } + await WaitForConditionAsync(() => { return linkObj.Set.Count == obj.Set.Count; }); Assert.That(linkObj.Set.Count, Is.EqualTo(obj.Set.Count)); var orderedOriginalSet = obj.Set.OrderBy(a => a.Id).ToList(); @@ -653,16 +658,16 @@ static async Task AssertEqual(MongoClient.Collection collection, Li }, timeout: 120000); } - public static readonly object[] RealmValueLinkTestCases = + public static IEnumerable RealmValueLinkTestCases() { - new object[] + yield return new object[] { CreateTestCase("Single link", new RealmValueObject { RealmValueProperty = new IntPropertyObject { Int = 2 }, }), - }, - new object[] + }; + yield return new object[] { CreateTestCase("List", new RealmValueObject { @@ -672,8 +677,8 @@ static async Task AssertEqual(MongoClient.Collection collection, Li new IntPropertyObject { Int = 200 }, }, }), - }, - new object[] + }; + yield return new object[] { CreateTestCase("Dictionary", new RealmValueObject { @@ -684,8 +689,8 @@ static async Task AssertEqual(MongoClient.Collection collection, Li ["key_2"] = new IntPropertyObject { Int = 200 }, }, }) - }, - new object[] + }; + yield return new object[] { CreateTestCase("Set", new RealmValueObject { @@ -695,8 +700,8 @@ static async Task AssertEqual(MongoClient.Collection collection, Li new IntPropertyObject { Int = 200 }, }, }), - }, - new object[] + }; + yield return new object[] { CreateTestCase("All types", new RealmValueObject { @@ -718,8 +723,8 @@ static async Task AssertEqual(MongoClient.Collection collection, Li ["dict_null"] = RealmValue.Null } }), - } - }; + }; + } [TestCaseSource(nameof(RealmValueLinkTestCases))] public void RealmObjectAPI_RealmValueLinks_AtlasToRealm(TestCaseData testCase) @@ -753,10 +758,16 @@ public void RealmObjectAPI_RealmValueLinks_AtlasToRealm(TestCaseData sender.Count >= totalCount); await realmObjs.WaitForEventAsync((sender, _) => sender.Count == 1 && realm.Find(obj.Id) != null); - var realmValObj = realm.Find(obj.Id); + var realmValObj = realm.Find(obj.Id)!; + await WaitForConditionAsync(() => + { + return realmValObj.RealmValueProperty.Type == RealmValueType.Null == + (obj.RealmValueProperty.Type == RealmValueType.Null); + }); AssertEqual(realmValObj!.RealmValueProperty, obj.RealmValueProperty); + await WaitForConditionAsync(() => { return realmValObj.RealmValueList.Count == obj.RealmValueList.Count; }); Assert.That(realmValObj.RealmValueList.Count, Is.EqualTo(obj.RealmValueList.Count)); for (int i = 0; i < realmValObj.RealmValueList.Count; i++) @@ -764,14 +775,21 @@ public void RealmObjectAPI_RealmValueLinks_AtlasToRealm(TestCaseData { return realmValObj.RealmValueDictionary.Count == obj.RealmValueDictionary.Count; }); Assert.That(realmValObj.RealmValueDictionary.Count, Is.EqualTo(obj.RealmValueDictionary.Count)); foreach (var key in obj.RealmValueDictionary.Keys) { Assert.That(realmValObj.RealmValueDictionary.ContainsKey(key)); + await WaitForConditionAsync(() => + { + return realmValObj.RealmValueDictionary[key].Type == RealmValueType.Null == (obj.RealmValueDictionary[key].Type == RealmValueType.Null); + }); + AssertEqual(realmValObj.RealmValueDictionary[key], obj.RealmValueDictionary[key]); } + await WaitForConditionAsync(() => { return realmValObj.RealmValueSet.Count == obj.RealmValueSet.Count; }); Assert.That(realmValObj.RealmValueSet.Count, Is.EqualTo(obj.RealmValueSet.Count)); var orderedOriginalSet = obj.RealmValueSet.OrderBy(a => a.As().Id).ToList(); @@ -871,9 +889,9 @@ static async Task AssertEqual(MongoClient.Collection collecti }, timeout: 120000); } - public static readonly object[] EmbeddedTestCases = + public static IEnumerable EmbeddedTestCases() { - new object[] + yield return new object[] { CreateTestCase("Single", new ObjectWithEmbeddedProperties { @@ -886,8 +904,8 @@ static async Task AssertEqual(MongoClient.Collection collecti StringProperty = "bla bla" } }) - }, - new object[] + }; + yield return new object[] { CreateTestCase("Recursive", new ObjectWithEmbeddedProperties { @@ -904,9 +922,8 @@ static async Task AssertEqual(MongoClient.Collection collecti } } }) - }, - - new object[] + }; + yield return new object[] { CreateTestCase("List", new ObjectWithEmbeddedProperties { @@ -930,8 +947,8 @@ static async Task AssertEqual(MongoClient.Collection collecti } }, }) - }, - new object[] + }; + yield return new object[] { CreateTestCase("Dictionary", new ObjectWithEmbeddedProperties { @@ -956,8 +973,8 @@ static async Task AssertEqual(MongoClient.Collection collecti ["key3"] = null, }, }) - }, - new object[] + }; + yield return new object[] { CreateTestCase("All types", new ObjectWithEmbeddedProperties { @@ -1021,8 +1038,8 @@ static async Task AssertEqual(MongoClient.Collection collecti ["key3"] = null, }, }) - } - }; + }; + } [TestCaseSource(nameof(EmbeddedTestCases))] public void RealmObjectAPI_Embedded_AtlasToRealm(TestCaseData testCase) @@ -1252,10 +1269,8 @@ public void RealmObjectAPI_MissingField_GetsDefaultValueWhenDeserialized() private async Task> GetCollection(string appConfigType = AppConfigType.Default) where T : class, IRealmObjectBase { - var app = App.Create(SyncTestHelpers.GetAppConfig(appConfigType)); - var user = await GetUserAsync(app); - - SyncConfigurationBase config = appConfigType == AppConfigType.FlexibleSync ? GetFLXIntegrationConfig(user) : GetIntegrationConfig(user); + var config = await GetConfig(appConfigType); + var user = config.User; using var realm = await GetRealmAsync(config, true); var client = user.GetMongoClient(ServiceName); @@ -1267,10 +1282,8 @@ public void RealmObjectAPI_MissingField_GetsDefaultValueWhenDeserialized() private async Task> GetCollectionAsBson(string collectionName, string appConfigType = AppConfigType.Default) { - var app = App.Create(SyncTestHelpers.GetAppConfig(appConfigType)); - var user = await GetUserAsync(app); - - SyncConfigurationBase config = appConfigType == AppConfigType.FlexibleSync ? GetFLXIntegrationConfig(user) : GetIntegrationConfig(user); + var config = await GetConfig(appConfigType); + var user = config.User; using var realm = await GetRealmAsync(config, true); var client = user.GetMongoClient(ServiceName); @@ -1282,6 +1295,14 @@ public void RealmObjectAPI_MissingField_GetsDefaultValueWhenDeserialized() return collection; } + private async Task GetConfig(string appConfigType = AppConfigType.Default) + { + var app = App.Create(SyncTestHelpers.GetAppConfig(appConfigType)); + var user = await GetUserAsync(app); + + return appConfigType == AppConfigType.FlexibleSync ? GetFLXIntegrationConfig(user) : GetIntegrationConfig(user); + } + private static void AssertEmbedded(ObjectWithEmbeddedProperties syncObj, ObjectWithEmbeddedProperties obj) { var props = EmbeddedAllTypesObject.RealmSchema