Skip to content

Commit

Permalink
Clean up tests and ensure we delete everything we create (#2058)
Browse files Browse the repository at this point in the history
* clean up tests and ensure we delete everything we create

* Try to fix linux tests

* Increase the dummy data size in release
  • Loading branch information
nirinchev authored Oct 4, 2020
1 parent 06c61ce commit 8c38425
Show file tree
Hide file tree
Showing 24 changed files with 445 additions and 696 deletions.
14 changes: 6 additions & 8 deletions Tests/Realm.Tests/Database/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ public void RefreshAsync_Tests()

Task.Run(() =>
{
using (var realm = Realm.GetInstance(_realm.Config))
using var realm = GetRealm(_realm.Config);
var bgObj = realm.ResolveReference(reference);
realm.Write(() =>
{
var bgObj = realm.ResolveReference(reference);
realm.Write(() =>
{
bgObj.StringValue = "123";
});
}
bgObj.StringValue = "123";
});
}).Wait(); // <- wait to avoid the main thread autoupdating while idle

Assert.That(obj.StringValue, Is.Null);
Expand All @@ -147,7 +145,7 @@ public void RefreshAsync_Tests()

Assert.That(changeTiming, Is.GreaterThan(idleTiming));

async Task<long> MeasureTiming(Func<Task> func)
static async Task<long> MeasureTiming(Func<Task> func)
{
var sw = new Stopwatch();
sw.Start();
Expand Down
42 changes: 15 additions & 27 deletions Tests/Realm.Tests/Database/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
Expand Down Expand Up @@ -160,11 +159,9 @@ public void IList_IsReadOnly_WhenRealmIsReadOnly_ShouldBeTrue()
var config = _configuration.ConfigWithPath(_configuration.DatabasePath);
config.IsReadOnly = true;

using (var readonlyRealm = Realm.GetInstance(config))
{
var readonlyContainer = readonlyRealm.All<ContainerObject>().Single();
Assert.That(readonlyContainer.Items.IsReadOnly);
}
using var readonlyRealm = GetRealm(config);
var readonlyContainer = readonlyRealm.All<ContainerObject>().Single();
Assert.That(readonlyContainer.Items.IsReadOnly);
}

[Test]
Expand Down Expand Up @@ -620,30 +617,21 @@ public void Results_GetFiltered_WhenPredicateIsInvalid_Throws()
[Test]
public void List_IndexOf_WhenObjectBelongsToADifferentRealm_ShouldThrow()
{
var config = new RealmConfiguration(Path.GetTempFileName());
try
var owner = new Owner();
_realm.Write(() =>
{
var owner = new Owner();
_realm.Write(() =>
{
_realm.Add(owner);
});

using (var otherRealm = Realm.GetInstance(config))
{
var otherRealmDog = new Dog();
otherRealm.Write(() =>
{
otherRealm.Add(otherRealmDog);
});
_realm.Add(owner);
});

Assert.That(() => owner.Dogs.IndexOf(otherRealmDog), Throws.InstanceOf<RealmObjectManagedByAnotherRealmException>());
}
}
finally
var config = new RealmConfiguration(Guid.NewGuid().ToString());
using var otherRealm = GetRealm(config);
var otherRealmDog = new Dog();
otherRealm.Write(() =>
{
Realm.DeleteRealm(config);
}
otherRealm.Add(otherRealmDog);
});

Assert.That(() => owner.Dogs.IndexOf(otherRealmDog), Throws.InstanceOf<RealmObjectManagedByAnotherRealmException>());
}

[Test]
Expand Down
40 changes: 19 additions & 21 deletions Tests/Realm.Tests/Database/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,28 @@ public void UnableToOpenWithNoKey()
{
// Arrange
_configuration.EncryptionKey = TestHelpers.GetEncryptionKey();
using (Realm.GetInstance(_configuration))
using (GetRealm(_configuration))
{
}

_configuration.EncryptionKey = null;

// Assert
Assert.That(() => Realm.GetInstance(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
Assert.That(() => GetRealm(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
}

[Test]
public void UnableToOpenWithKeyIfNotEncrypted()
{
// Arrange
using (Realm.GetInstance(_configuration))
using (GetRealm(_configuration))
{
}

_configuration.EncryptionKey = TestHelpers.GetEncryptionKey();

// Assert
Assert.That(() => Realm.GetInstance(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
Assert.That(() => GetRealm(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
}

[Test]
Expand All @@ -144,14 +144,14 @@ public void UnableToOpenWithDifferentKey()
// Arrange
_configuration.EncryptionKey = TestHelpers.GetEncryptionKey();

using (Realm.GetInstance(_configuration))
using (GetRealm(_configuration))
{
}

_configuration.EncryptionKey[0] = 42;

// Assert
Assert.That(() => Realm.GetInstance(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
Assert.That(() => GetRealm(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
}

[Test]
Expand All @@ -160,7 +160,7 @@ public void AbleToReopenEncryptedWithSameKey()
// Arrange
_configuration.EncryptionKey = TestHelpers.GetEncryptionKey(42);

using (Realm.GetInstance(_configuration))
using (GetRealm(_configuration))
{
}

Expand All @@ -172,7 +172,7 @@ public void AbleToReopenEncryptedWithSameKey()
// Assert
Assert.That(() =>
{
using (Realm.GetInstance(config2))
using (GetRealm(config2))
{
}
}, Throws.Nothing);
Expand All @@ -185,7 +185,7 @@ public void ReadOnlyFilesMustExist()
_configuration.IsReadOnly = true;

// Assert
Assert.That(() => Realm.GetInstance(_configuration), Throws.TypeOf<RealmFileAccessErrorException>());
Assert.That(() => GetRealm(_configuration), Throws.TypeOf<RealmFileNotFoundException>());
}

[Test, TestExplicit("Currently, a RealmMismatchedConfigException is thrown. Registered as #580")]
Expand All @@ -198,15 +198,15 @@ public void ReadOnlyRealmsWillNotAutoMigrate()
"ForMigrationsToCopyAndMigrate.realm", Path.GetFileName(_configuration.DatabasePath));

// Assert
Assert.That(() => Realm.GetInstance(_configuration), Throws.TypeOf<RealmMigrationNeededException>());
Assert.That(() => GetRealm(_configuration), Throws.TypeOf<RealmMigrationNeededException>());
}

[Test]
public void ReadOnlyRealmsArentWritable()
{
// Arrange
_configuration.SchemaVersion = 0; // must set version before file can be opened readOnly
using (var openToCreate = Realm.GetInstance(_configuration))
using (var openToCreate = GetRealm(_configuration))
{
openToCreate.Write(() =>
{
Expand All @@ -216,17 +216,15 @@ public void ReadOnlyRealmsArentWritable()

_configuration.IsReadOnly = true;

using (var openedReadonly = Realm.GetInstance(_configuration))
using var openedReadonly = GetRealm(_configuration);
// Assert
Assert.That(() =>
{
// Assert
Assert.That(() =>
openedReadonly.Write(() =>
{
openedReadonly.Write(() =>
{
openedReadonly.Add(new Person());
});
}, Throws.TypeOf<RealmInvalidTransactionException>());
}
openedReadonly.Add(new Person());
});
}, Throws.TypeOf<RealmInvalidTransactionException>());
}

[Test]
Expand All @@ -245,7 +243,7 @@ public void DuplicateClassNames_ThrowsException()
.Message.Contains("Foo.DuplicateClass").And
.Message.Contains("Bar.DuplicateClass");

Assert.That(() => Realm.GetInstance(config), constraint);
Assert.That(() => GetRealm(config), constraint);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions Tests/Realm.Tests/Database/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;

Expand Down Expand Up @@ -136,17 +135,15 @@ public class IndexedDateTimeOffsetObject : RealmObject
public void IndexedDateTimeOffsetTest()
{
// Arrange
var config = new RealmConfiguration(Path.GetTempFileName())
var config = new RealmConfiguration(Guid.NewGuid().ToString())
{
ObjectClasses = new[] { typeof(IndexedDateTimeOffsetObject) }
};

// Act and "assert" that no exception is thrown here
using (Realm.GetInstance(config))
using (GetRealm(config))
{
}

Realm.DeleteRealm(config);
}

[Test]
Expand Down
28 changes: 13 additions & 15 deletions Tests/Realm.Tests/Database/InMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void CustomSetUp()
[Test]
public void InMemoryRealm_WhenDeleted_RemovesAuxiliaryFiles()
{
using (var realm = Realm.GetInstance(_config))
using (var realm = GetRealm(_config))
{
realm.Write(() => realm.Add(new IntPropertyObject
{
Expand All @@ -53,7 +53,7 @@ public void InMemoryRealm_WhenDeleted_RemovesAuxiliaryFiles()

Assert.That(File.Exists(_config.DatabasePath), Is.False);

using (var realm = Realm.GetInstance(_config))
using (var realm = GetRealm(_config))
{
Assert.That(File.Exists(_config.DatabasePath));
Assert.That(realm.All<IntPropertyObject>(), Is.Empty);
Expand All @@ -67,7 +67,7 @@ public void InMemoryRealm_ReceivesNotifications()
{
var tcs = new TaskCompletionSource<ChangeSet>();

var realm = Realm.GetInstance(_config);
var realm = GetRealm(_config);

try
{
Expand All @@ -86,13 +86,11 @@ public void InMemoryRealm_ReceivesNotifications()

await Task.Run(() =>
{
using (var otherRealm = Realm.GetInstance(_config))
using var otherRealm = GetRealm(_config);
otherRealm.Write(() => otherRealm.Add(new IntPropertyObject
{
otherRealm.Write(() => otherRealm.Add(new IntPropertyObject
{
Int = 42
}));
}
Int = 42
}));
});

var backgroundChanges = await tcs.Task;
Expand All @@ -112,8 +110,8 @@ await Task.Run(() =>
[Test]
public void InMemoryRealm_WhenMultipleInstancesOpen_DoesntDeleteData()
{
var first = Realm.GetInstance(_config);
var second = Realm.GetInstance(_config);
var first = GetRealm(_config);
var second = GetRealm(_config);

first.Write(() => first.Add(new IntPropertyObject
{
Expand Down Expand Up @@ -163,7 +161,7 @@ public void InMemoryRealm_WhenGarbageCollected_DeletesData()

Assert.That(File.Exists(_config.DatabasePath), Is.False);

using (var realm = Realm.GetInstance(_config))
using (var realm = GetRealm(_config))
{
Assert.That(realm.All<IntPropertyObject>(), Is.Empty);
}
Expand All @@ -178,17 +176,17 @@ public void InMemoryRealm_WhenEncrypted_RequiresEncryptionKey()
EncryptionKey = TestHelpers.GetEncryptionKey(23)
};

using (var realm = Realm.GetInstance(encryptedConfig))
using (var realm = GetRealm(encryptedConfig))
{
realm.Write(() => realm.Add(new IntPropertyObject
{
Int = 42
}));

Assert.That(() => Realm.GetInstance(_config), Throws.TypeOf<RealmMismatchedConfigException>());
Assert.That(() => GetRealm(_config), Throws.TypeOf<RealmMismatchedConfigException>());
}

Assert.That(() => Realm.GetInstance(_config), Throws.Nothing);
Assert.That(() => GetRealm(_config), Throws.Nothing);
}
}
}
Loading

0 comments on commit 8c38425

Please sign in to comment.