Skip to content

Commit

Permalink
Fix Person_Tests to run each test in a new database instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Jun 26, 2024
1 parent b82f30e commit 2536079
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
21 changes: 20 additions & 1 deletion LiteDB.Tests/Query/OrderBy_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();

Expand Down
21 changes: 12 additions & 9 deletions LiteDB.Tests/Query/Person_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ namespace LiteDB.Tests.QueryTest
{
public class Person_Tests : IDisposable
{
protected readonly Person[] local;

protected ILiteDatabase db;
protected ILiteCollection<Person> collection;
private readonly Person[] _local;
private readonly ILiteDatabase _db;
private readonly ILiteCollection<Person> _collection;

public Person_Tests()
{
this.local = DataGen.Person().ToArray();
this._local = DataGen.Person().ToArray();

db = new LiteDatabase(":memory:");
collection = db.GetCollection<Person>("person");
collection.Insert(this.local);
_db = new LiteDatabase(":memory:");
_collection = _db.GetCollection<Person>("person");
_collection.Insert(this._local);
}

public ILiteCollection<Person> GetCollection() => _collection;

public Person[] GetLocal() => _local;

public void Dispose()
{
db?.Dispose();
_db?.Dispose();
}
}
}
8 changes: 8 additions & 0 deletions LiteDB.Tests/Query/QueryApi_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions LiteDB.Tests/Query/Select_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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())
Expand Down
20 changes: 20 additions & 0 deletions LiteDB.Tests/Query/Where_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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"))
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit 2536079

Please sign in to comment.