From d1dd6703073f08025b3f32c67452932412673e90 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Tue, 27 Feb 2024 02:44:05 +0100 Subject: [PATCH] Fix warnings --- Realm.sln.DotSettings | 1 + Tests/Realm.Tests/Sync/MongoClientTests.cs | 6 +- Tests/Realm.Tests/Sync/StaticQueriesTests.cs | 72 ++++++++++---------- Tests/Realm.Tests/TestHelpers.cs | 7 ++ 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/Realm.sln.DotSettings b/Realm.sln.DotSettings index 40903ac831..9e64533eaf 100644 --- a/Realm.sln.DotSettings +++ b/Realm.sln.DotSettings @@ -97,6 +97,7 @@ True True True + True True True True diff --git a/Tests/Realm.Tests/Sync/MongoClientTests.cs b/Tests/Realm.Tests/Sync/MongoClientTests.cs index 7300b80184..6dc426b85a 100644 --- a/Tests/Realm.Tests/Sync/MongoClientTests.cs +++ b/Tests/Realm.Tests/Sync/MongoClientTests.cs @@ -2108,7 +2108,8 @@ public void MongoCollection_FindOneAndDelete_FilterSortProjection() var projection = BsonDocument.Parse("{ LongValue: 1 }"); var result = await collection.FindOneAndDeleteAsync(filter, sort, projection); - Assert.That(result.Id, Is.EqualTo(inserted[1].Id)); + Assert.That(result, Is.Not.Null); + Assert.That(result!.Id, Is.EqualTo(inserted[1].Id)); Assert.That(result.StringValue, Is.Null); Assert.That(result.LongValue, Is.EqualTo(inserted[1].LongValue)); @@ -2126,7 +2127,8 @@ public void MongoCollection_FindOne_Remapped() var inserted = await InsertRemappedData(collection); var result = await collection.FindOneAsync(); - Assert.That(result.Id, Is.EqualTo(inserted[0].Id)); + Assert.That(result, Is.Not.Null); + Assert.That(result!.Id, Is.EqualTo(inserted[0].Id)); Assert.That(result.StringValue, Is.EqualTo(inserted[0].StringValue)); Assert.That(result.MappedLink!.Id, Is.EqualTo(inserted[1].Id)); Assert.That(result.MappedList[0].Id, Is.EqualTo(inserted[2].Id)); diff --git a/Tests/Realm.Tests/Sync/StaticQueriesTests.cs b/Tests/Realm.Tests/Sync/StaticQueriesTests.cs index 0879cd696e..f4864edec7 100644 --- a/Tests/Realm.Tests/Sync/StaticQueriesTests.cs +++ b/Tests/Realm.Tests/Sync/StaticQueriesTests.cs @@ -28,7 +28,6 @@ using Realms.Schema; using Realms.Sync; using Realms.Sync.Exceptions; - using static Realms.Tests.TestHelpers; namespace Realms.Tests.Sync @@ -63,15 +62,15 @@ public void RealmObjectAPI_Collections() var syncObj1 = syncObjects.Single(); - AssertProps(obj1, syncObj1); + AssertCollectionProps(obj1, syncObj1); realm.Write(() => realm.Add(syncObj2)); var filter = new { _id = syncObj2.Id }; - var obj2 = await WaitForConditionAsync(() => collection.FindOneAsync(filter), item => Task.FromResult(item != null)); + var obj2 = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(filter)); - AssertProps(syncObj2, obj2); + AssertCollectionProps(syncObj2, obj2); void FillCollectionProps(SyncCollectionsObject obj) { @@ -81,7 +80,7 @@ void FillCollectionProps(SyncCollectionsObject obj) } } - void AssertProps(SyncCollectionsObject expected, SyncCollectionsObject actual) + void AssertCollectionProps(SyncCollectionsObject expected, SyncCollectionsObject actual) { foreach (var prop in props) { @@ -94,7 +93,7 @@ void AssertProps(SyncCollectionsObject expected, SyncCollectionsObject actual) }, timeout: 120000); } - public static readonly object[] PrimitiveTestCases = new[] + public static readonly object[] PrimitiveTestCases = { new object[] { CreateTestCase("Empty object", new SyncAllTypesObject()) }, new object[] @@ -175,7 +174,7 @@ public void RealmObjectAPI_Primitive_RealmToAtlas(TestCaseData collection.FindOneAsync(filter), item => Task.FromResult(item != null)); + var syncObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(filter)); AssertProps(props, obj, syncObj); }, timeout: 120000); @@ -189,7 +188,7 @@ public void RealmObjectAPI_Primitive_RealmToAtlas(TestCaseData te }, timeout: 120000); } - public static readonly object[] DateTimeTestCasesRealmToAtlas = new[] + public static readonly object[] DateTimeTestCasesRealmToAtlas = { new object[] { CreateTestCase("PostEpoch", new DateTimeOffset(638404890727190000, TimeSpan.Zero)) }, new object[] { CreateTestCase("PostEpoch-subprecision", new DateTimeOffset(638404890727196472, TimeSpan.Zero)) }, @@ -238,7 +237,7 @@ public void RealmObjectAPI_DateTime_RealmToAtlas(TestCaseData te var filter = new { _id = obj.Id }; - var syncObj = await WaitForConditionAsync(() => collection.FindOneAsync(filter), item => Task.FromResult(item != null)); + var syncObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(filter)); AssertAreEqual(syncObj.DateTimeOffsetProperty, obj.DateTimeOffsetProperty); }, timeout: 120000); @@ -261,7 +260,7 @@ public void RealmObjectAPI_DateTime_RealmToAtlas_SpecialCase() var filter = new { _id = obj.Id }; - var syncObj = await WaitForConditionAsync(() => collection.FindOneAsync(filter), item => Task.FromResult(item != null)); + var syncObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(filter)); var originalUnixMs = obj.DateTimeOffsetProperty.ToUnixTimeMilliseconds(); var expectedUnixMs = syncObj.DateTimeOffsetProperty.ToUnixTimeMilliseconds(); @@ -270,7 +269,7 @@ public void RealmObjectAPI_DateTime_RealmToAtlas_SpecialCase() }, timeout: 120000); } - public static readonly object[] CounterTestCases = new[] + public static readonly object[] CounterTestCases = { new object[] { @@ -342,13 +341,13 @@ public void RealmObjectAPI_Counter_RealmToAtlas(TestCaseData test var filter = new { _id = obj.Id }; - var syncObj = await WaitForConditionAsync(() => collection.FindOneAsync(filter), item => Task.FromResult(item != null)); + var syncObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(filter)); AssertProps(props, obj, syncObj); }, timeout: 120000); } - public static readonly object[] AsymmetricTestCases = new[] + public static readonly object[] AsymmetricTestCases = { new object[] { @@ -370,13 +369,13 @@ public void RealmObjectAPI_Asymmetric_RealmToAtlas(TestCaseData realm.Add(obj)); - var syncObj = await WaitForConditionAsync(() => collection.FindOneAsync(filter), item => Task.FromResult(item != null)); + var syncObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(filter)); Assert.That(stringProperty, Is.EqualTo(syncObj.PartitionLike)); }, timeout: 120000); } - public static readonly object[] ObjectTestCases = new[] + public static readonly object[] ObjectTestCases = { new object[] { @@ -433,8 +432,8 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData await realm.All().Where(o => o.Id == obj.ObjectProperty!.Id).SubscribeAsync(); realm.Write(() => realm.Add(obj)); - var syncAllTypeObj = await WaitForConditionAsync(() => syncAllTypesCollection.FindOneAsync(new { _id = obj.Id }), item => Task.FromResult(item != null)); - var intPropertyObj = await WaitForConditionAsync(() => intPropertyCollection.FindOneAsync(new { _id = obj.ObjectProperty!.Id }), item => Task.FromResult(item != null)); + var syncAllTypeObj = await WaitForNonNullObjectAsync(() => syncAllTypesCollection.FindOneAsync(new { _id = obj.Id })); + var intPropertyObj = await WaitForNonNullObjectAsync(() => intPropertyCollection.FindOneAsync(new { _id = obj.ObjectProperty!.Id })); Assert.That(syncAllTypeObj.ObjectProperty!.Id, Is.EqualTo(obj.ObjectProperty!.Id)); Assert.That(syncAllTypeObj.ObjectProperty!.Int, Is.Not.EqualTo(obj.ObjectProperty!.Int)); @@ -444,7 +443,7 @@ public void RealmObjectAPI_Object_RealmToAtlas(TestCaseData }, timeout: 120000); } - public static readonly object[] LinksTestCases = new[] + public static readonly object[] LinksTestCases = { new object[] { @@ -536,7 +535,7 @@ public void RealmObjectAPI_Links_AtlasToRealm(TestCaseData testCase await collection.InsertManyAsync(elementsToInsert!); // How many objects we expect - var totalCount = obj.List.Count + obj.Set.Count + obj.Dictionary.Where(d => d.Value != null).Count() + 1 + (obj.Link is null ? 0 : 1); + var totalCount = obj.List.Count + obj.Set.Count + obj.Dictionary.Count(d => d.Value != null) + 1 + (obj.Link is null ? 0 : 1); using var realm = await GetFLXIntegrationRealmAsync(); var linkObjs = await realm.All().SubscribeAsync(); @@ -582,8 +581,8 @@ static void AssertEqual(LinksObject? retrieved, LinksObject? original) else { Assert.That(retrieved, Is.Not.Null); - Assert.That(retrieved!.Id, Is.EqualTo(original!.Id)); - Assert.That(retrieved!.Value, Is.EqualTo(original!.Value)); + Assert.That(retrieved!.Id, Is.EqualTo(original.Id)); + Assert.That(retrieved.Value, Is.EqualTo(original.Value)); } } }, timeout: 120000); @@ -606,7 +605,7 @@ public void RealmObjectAPI_Links_RealmToAtlas(TestCaseData testCase realm.Write(() => realm.Add(obj)); await WaitForUploadAsync(realm); - var linkObj = await WaitForConditionAsync(() => collection.FindOneAsync(new { _id = obj.Id }), item => Task.FromResult(item != null)); + var linkObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(new { _id = obj.Id })); await AssertEqual(collection, linkObj.Link, obj.Link); @@ -646,7 +645,7 @@ static async Task AssertEqual(MongoClient.Collection collection, Li Assert.That(partiallyRetrieved!.Id, Is.EqualTo(original.Id)); Assert.That(partiallyRetrieved.Value, Is.Not.EqualTo(original.Value)); - var fullyRetrieved = await WaitForConditionAsync(() => collection.FindOneAsync(new { _id = original.Id }), item => Task.FromResult(item != null)); + var fullyRetrieved = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(new { _id = original.Id })); Assert.That(fullyRetrieved.Id, Is.EqualTo(original.Id)); Assert.That(fullyRetrieved.Value, Is.EqualTo(original.Value)); @@ -654,7 +653,7 @@ static async Task AssertEqual(MongoClient.Collection collection, Li }, timeout: 120000); } - public static readonly object[] RealmValueLinkTestCases = new[] + public static readonly object[] RealmValueLinkTestCases = { new object[] { @@ -734,18 +733,18 @@ public void RealmObjectAPI_RealmValueLinks_AtlasToRealm(TestCaseData o.As()) - .Concat(obj.RealmValueSet.Select(o => o.As()) - .Concat(obj.RealmValueDictionary.Values.Select(o => o.As()).Where(v => v is not null))); + .Concat(obj.RealmValueSet.Select(o => o.As()) + .Concat(obj.RealmValueDictionary.Values.Select(o => o.As()).Where(v => v is not null))); if (obj.RealmValueProperty != RealmValue.Null) { elementsToInsert = elementsToInsert.Concat(new[] { obj.RealmValueProperty.As() }); } - await intCollection.InsertManyAsync(elementsToInsert!); + await intCollection.InsertManyAsync(elementsToInsert); // How many objects we expect - var totalCount = obj.RealmValueList.Count + obj.RealmValueSet.Count + obj.RealmValueDictionary.Where(d => d.Value != RealmValue.Null).Count(); + var totalCount = obj.RealmValueList.Count + obj.RealmValueSet.Count + obj.RealmValueDictionary.Count(d => d.Value != RealmValue.Null); using var realm = await GetFLXIntegrationRealmAsync(); var intObjs = await realm.All().SubscribeAsync(); @@ -820,7 +819,7 @@ public void RealmObjectAPI_RealmValueLinks_RealmToAtlas(TestCaseData realm.Add(obj)); await WaitForUploadAsync(realm); - var realmValObj = await WaitForConditionAsync(() => realmValCollection.FindOneAsync(new { _id = obj.Id }), item => Task.FromResult(item != null)); + var realmValObj = await WaitForNonNullObjectAsync(() => realmValCollection.FindOneAsync(new { _id = obj.Id })); await AssertEqual(intCollection, realmValObj.RealmValueProperty, obj.RealmValueProperty); @@ -864,7 +863,7 @@ static async Task AssertEqual(MongoClient.Collection collecti Assert.That(retrievedAsObj.Id, Is.EqualTo(originalAsObj.Id)); Assert.That(retrievedAsObj.Int, Is.Not.EqualTo(originalAsObj.Int)); - var fullyRetrieved = await WaitForConditionAsync(() => collection.FindOneAsync(new { _id = originalAsObj.Id }), item => Task.FromResult(item != null)); + var fullyRetrieved = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(new { _id = originalAsObj.Id })); Assert.That(fullyRetrieved.Id, Is.EqualTo(originalAsObj.Id)); Assert.That(fullyRetrieved.Int, Is.EqualTo(originalAsObj.Int)); @@ -1059,7 +1058,7 @@ public void RealmObjectAPI_Embedded_RealmToAtlas(TestCaseData realm.Add(obj)); await WaitForUploadAsync(realm); - var syncObj = await WaitForConditionAsync(() => collection.FindOneAsync(new { _id = obj.PrimaryKey }), item => Task.FromResult(item != null)); + var syncObj = await WaitForNonNullObjectAsync(() => collection.FindOneAsync(new { _id = obj.PrimaryKey })); AssertEmbedded(syncObj, obj); }, timeout: 120000); @@ -1132,7 +1131,7 @@ public void RealmObjectAPI_ExtraFields_IgnoredWhenUsingTypedCollection() var retrieved = await typedCollection.FindOneAsync(new { _id = primaryKey }); Assert.That(retrieved, Is.Not.Null); - Assert.That(retrieved.Value, Is.EqualTo(doc["Value"].AsString)); + Assert.That(retrieved!.Value, Is.EqualTo(doc["Value"].AsString)); }, timeout: 120000); } @@ -1151,7 +1150,7 @@ public void RealmObjectAPI_MismatchedType_ThrowsOnInsertWhenCollectionInSchema() { "Value", ObjectId.GenerateNewId() }, // Wrong type }; - var ex = await TestHelpers.AssertThrows(() => collection.InsertOneAsync(doc)); + var ex = await AssertThrows(() => collection.InsertOneAsync(doc)); Assert.That(ex.Message, Does.Contain("insert not permitted")); }, timeout: 120000); } @@ -1206,7 +1205,7 @@ public void RealmObjectAPI_MissingField_ThrowsOnInsertWhenCollectionInSchema() { "Int", 23 }, // Missing the GuidProperty field }; - var ex = await TestHelpers.AssertThrows(() => collection.InsertOneAsync(doc)); + var ex = await AssertThrows(() => collection.InsertOneAsync(doc)); Assert.That(ex.Message, Does.Contain("insert not permitted")); }, timeout: 120000); } @@ -1242,7 +1241,8 @@ public void RealmObjectAPI_MissingField_GetsDefaultValueWhenDeserialized() var typedCollection = db.GetCollection(collectionName); var retrieved = await typedCollection.FindOneAsync(new { _id = primaryKey }); - Assert.That(retrieved.Id, Is.EqualTo(primaryKey)); + Assert.That(retrieved, Is.Not.Null); + Assert.That(retrieved!.Id, Is.EqualTo(primaryKey)); Assert.That(retrieved.Int, Is.EqualTo(doc["Int"].AsInt32)); Assert.That(retrieved.GuidProperty, Is.EqualTo(default(Guid))); }, timeout: 120000); diff --git a/Tests/Realm.Tests/TestHelpers.cs b/Tests/Realm.Tests/TestHelpers.cs index aa1d5f7f65..f7ed5966de 100644 --- a/Tests/Realm.Tests/TestHelpers.cs +++ b/Tests/Realm.Tests/TestHelpers.cs @@ -286,6 +286,13 @@ public static async Task WaitForConditionAsync(Func> producer, Fun return value; } + public static async Task WaitForNonNullObjectAsync(Func> producer) + { + var value = await WaitForConditionAsync(producer, v => Task.FromResult(v != null)); + Assert.That(value, Is.Not.Null); + return value!; + } + public static void RunAsyncTest(Func testFunc, int timeout = 30000, Task? errorTask = null) { AsyncContext.Run(async () =>