From 2536079f6f0dcfaf2a2274b1223571ab621130f3 Mon Sep 17 00:00:00 2001 From: mbdavid Date: Wed, 26 Jun 2024 10:27:23 -0300 Subject: [PATCH] Fix Person_Tests to run each test in a new database instance --- LiteDB.Tests/Query/OrderBy_Tests.cs | 21 ++++++++++++++++++++- LiteDB.Tests/Query/Person_Tests.cs | 21 ++++++++++++--------- LiteDB.Tests/Query/QueryApi_Tests.cs | 8 ++++++++ LiteDB.Tests/Query/Select_Tests.cs | 17 +++++++++++++++++ LiteDB.Tests/Query/Where_Tests.cs | 20 ++++++++++++++++++++ 5 files changed, 77 insertions(+), 10 deletions(-) diff --git a/LiteDB.Tests/Query/OrderBy_Tests.cs b/LiteDB.Tests/Query/OrderBy_Tests.cs index 09a084a65..e0e17a9e5 100644 --- a/LiteDB.Tests/Query/OrderBy_Tests.cs +++ b/LiteDB.Tests/Query/OrderBy_Tests.cs @@ -4,11 +4,15 @@ namespace LiteDB.Tests.QueryTest { - public class OrderBy_Tests : Person_Tests + public class OrderBy_Tests { [Fact] public void Query_OrderBy_Using_Index() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + collection.EnsureIndex(x => x.Name); var r0 = local @@ -27,6 +31,10 @@ public void Query_OrderBy_Using_Index() [Fact] public void Query_OrderBy_Using_Index_Desc() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + collection.EnsureIndex(x => x.Name); var r0 = local @@ -45,6 +53,10 @@ public void Query_OrderBy_Using_Index_Desc() [Fact] public void Query_OrderBy_With_Func() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + collection.EnsureIndex(x => x.Date.Day); var r0 = local @@ -63,6 +75,10 @@ public void Query_OrderBy_With_Func() [Fact] public void Query_OrderBy_With_Offset_Limit() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + // no index var r0 = local @@ -85,6 +101,9 @@ public void Query_OrderBy_With_Offset_Limit() [Fact] public void Query_Asc_Desc() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var asc = collection.Find(Query.All(Query.Ascending)).ToArray(); var desc = collection.Find(Query.All(Query.Descending)).ToArray(); diff --git a/LiteDB.Tests/Query/Person_Tests.cs b/LiteDB.Tests/Query/Person_Tests.cs index d55ee1053..d0fb6a852 100644 --- a/LiteDB.Tests/Query/Person_Tests.cs +++ b/LiteDB.Tests/Query/Person_Tests.cs @@ -5,23 +5,26 @@ namespace LiteDB.Tests.QueryTest { public class Person_Tests : IDisposable { - protected readonly Person[] local; - - protected ILiteDatabase db; - protected ILiteCollection collection; + private readonly Person[] _local; + private readonly ILiteDatabase _db; + private readonly ILiteCollection _collection; public Person_Tests() { - this.local = DataGen.Person().ToArray(); + this._local = DataGen.Person().ToArray(); - db = new LiteDatabase(":memory:"); - collection = db.GetCollection("person"); - collection.Insert(this.local); + _db = new LiteDatabase(":memory:"); + _collection = _db.GetCollection("person"); + _collection.Insert(this._local); } + public ILiteCollection GetCollection() => _collection; + + public Person[] GetLocal() => _local; + public void Dispose() { - db?.Dispose(); + _db?.Dispose(); } } } \ No newline at end of file diff --git a/LiteDB.Tests/Query/QueryApi_Tests.cs b/LiteDB.Tests/Query/QueryApi_Tests.cs index d92ece994..a6a158d20 100644 --- a/LiteDB.Tests/Query/QueryApi_Tests.cs +++ b/LiteDB.Tests/Query/QueryApi_Tests.cs @@ -10,6 +10,10 @@ public class QueryApi_Tests : Person_Tests [Fact] public void Query_And() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local.Where(x => x.Age == 22 && x.Active == true).ToArray(); var r1 = collection.Find(Query.And(Query.EQ("Age", 22), Query.EQ("Active", true))).ToArray(); @@ -20,6 +24,10 @@ public void Query_And() [Fact] public void Query_And_Same_Field() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local.Where(x => x.Age > 22 && x.Age < 25).ToArray(); var r1 = collection.Find(Query.And(Query.GT("Age", 22), Query.LT("Age", 25))).ToArray(); diff --git a/LiteDB.Tests/Query/Select_Tests.cs b/LiteDB.Tests/Query/Select_Tests.cs index 66117640c..b2e67342d 100644 --- a/LiteDB.Tests/Query/Select_Tests.cs +++ b/LiteDB.Tests/Query/Select_Tests.cs @@ -10,6 +10,10 @@ public class Select_Tests : Person_Tests [Fact] public void Query_Select_Key_Only() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + collection.EnsureIndex(x => x.Address.City); // must orderBy mem data because index will be sorted @@ -30,6 +34,10 @@ public void Query_Select_Key_Only() [Fact] public void Query_Select_New_Document() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local .Select(x => new {city = x.Address.City.ToUpper(), phone0 = x.Phones[0], address = new Address {Street = x.Name}}) .ToArray(); @@ -49,6 +57,9 @@ public void Query_Select_New_Document() [Fact] public void Query_Or_With_Null() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var r = collection.Find(Query.Or( Query.GTE("Date", new DateTime(2001, 1, 1)), Query.EQ("Date", null) @@ -58,6 +69,10 @@ public void Query_Or_With_Null() [Fact] public void Query_Find_All_Predicate() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r = collection.Find(x => true).ToArray(); r.Should().HaveCount(1000); @@ -66,6 +81,8 @@ public void Query_Find_All_Predicate() [Fact] public void Query_With_No_Collection() { + using var db = new LiteDatabase(":memory:"); + using (var r = db.Execute("SELECT DAY(NOW()) as DIA")) { while(r.Read()) diff --git a/LiteDB.Tests/Query/Where_Tests.cs b/LiteDB.Tests/Query/Where_Tests.cs index e411bc30f..07a2eb955 100644 --- a/LiteDB.Tests/Query/Where_Tests.cs +++ b/LiteDB.Tests/Query/Where_Tests.cs @@ -15,6 +15,10 @@ class Entity [Fact] public void Query_Where_With_Parameter() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local .Where(x => x.Address.State == "FL") .ToArray(); @@ -29,6 +33,10 @@ public void Query_Where_With_Parameter() [Fact] public void Query_Multi_Where_With_Like() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local .Where(x => x.Age >= 10 && x.Age <= 40) .Where(x => x.Name.StartsWith("Ge")) @@ -45,6 +53,10 @@ public void Query_Multi_Where_With_Like() [Fact] public void Query_Single_Where_With_And() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local .Where(x => x.Age == 25 && x.Active) .ToArray(); @@ -59,6 +71,10 @@ public void Query_Single_Where_With_And() [Fact] public void Query_Single_Where_With_Or_And_In() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var r0 = local .Where(x => x.Age == 25 || x.Age == 26 || x.Age == 27) .ToArray(); @@ -78,6 +94,10 @@ public void Query_Single_Where_With_Or_And_In() [Fact] public void Query_With_Array_Ids() { + using var db = new Person_Tests(); + var collection = db.GetCollection(); + var local = db.GetLocal(); + var ids = new int[] { 1, 2, 3 }; var r0 = local