Skip to content

Commit

Permalink
Fix unit tests and connection string parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Aug 11, 2018
1 parent 4a0b42b commit ce0b8b0
Show file tree
Hide file tree
Showing 24 changed files with 755 additions and 710 deletions.
6 changes: 2 additions & 4 deletions LiteDB.Tests/Database/Aggregate_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ public void Aggregates()

Assert.AreEqual(3, users.Count());
Assert.AreEqual(2, users.Count("Active = true")); // must be a predicate
Assert.AreEqual(2, users.Count(x => x.Active == true));

Assert.AreEqual(3L, users.LongCount());
Assert.AreEqual(1L, users.LongCount("Salary > 100000"));
Assert.AreEqual(1L, users.LongCount(x => x.Salary > 100000));
Assert.AreEqual(1L, users.LongCount("Salary > @0", 100000));

Assert.AreEqual("Zarlos", users.Max(x => x.Name));
Assert.AreEqual("Ana", users.Min(x => x.Name));

Assert.IsTrue(users.Exists(x => x.Salary == 75000));
Assert.IsTrue(users.Exists("Salary = @0", 75000));
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions LiteDB.Tests/Engine/Analyze_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ public void Analyze_Collection_Count()
{
var zip = DataGen.Zip().Take(100).ToArray();

using (var db = new LiteEngine())
using (var db = new LiteDatabase(new MemoryStream()))
{
var col = db.GetCollection("zip", BsonAutoId.Int32);

// init zip collection with 100 document
db.Insert("zip", zip);
col.Insert(zip);

db.EnsureIndex("zip", "city", "$.city", false);
db.EnsureIndex("zip", "loc", "$.loc[*]", false);
col.EnsureIndex("city", "$.city", false);
col.EnsureIndex("loc", "$.loc[*]", false);

var indexes = db.Query("$indexes")
.ToEnumerable()
var indexes = db.GetCollection("$indexes").FindAll()
.ToDictionary(x => x["name"].AsString, x => x);

// testing for just-created indexes (always be zero)
Expand All @@ -41,10 +42,9 @@ public void Analyze_Collection_Count()
Assert.AreEqual(0, indexes["loc"]["keyCount"].AsInt32);
Assert.AreEqual(0, indexes["loc"]["uniqueKeyCount"].AsInt32);

db.Analyze(new[] { "zip" });
db.Analyze("zip");

indexes = db.Query("$indexes")
.ToEnumerable()
indexes = db.GetCollection("$indexes").FindAll()
.ToDictionary(x => x["name"].AsString, x => x);

// count unique values
Expand Down
2 changes: 1 addition & 1 deletion LiteDB.Tests/Engine/Create_Database_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Create_Database_With_Initial_Size()
Assert.AreEqual(initial, file.Size);

// insert minimal data
db.Execute("insert into col1 values {a:1}");
db.Execute("insert into col1 values {a:1}", null);

Assert.AreEqual(initial, file.Size);

Expand Down
7 changes: 5 additions & 2 deletions LiteDB.Tests/Engine/DropCollection_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ public class DropCollection_Tests
public void DropCollection()
{
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteDatabase(file.Filename))
{
Assert.IsFalse(db.GetCollectionNames().Any(x => x == "col"));

db.Insert("col", new BsonDocument { { "a", 1 } });
var col = db.GetCollection("col");

col.Insert(new BsonDocument { ["a"] = 1 });

Assert.IsTrue(db.GetCollectionNames().Any(x => x == "col"));

db.DropCollection("col");
Expand Down
29 changes: 14 additions & 15 deletions LiteDB.Tests/Engine/IndexOrder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ public class IndexOrder_Tests
[TestMethod]
public void Index_Order()
{
using (var tmp = new TempFile())
using (var db = new LiteEngine(tmp.Filename))
using (var db = new LiteDatabase(new MemoryStream()))
{
db.Insert("col", new BsonDocument { { "text", "D" } });
db.Insert("col", new BsonDocument { { "text", "A" } });
db.Insert("col", new BsonDocument { { "text", "E" } });
db.Insert("col", new BsonDocument { { "text", "C" } });
db.Insert("col", new BsonDocument { { "text", "B" } });
var col = db.GetCollection("col");
var indexes = db.GetCollection("$indexes");

db.EnsureIndex("col", "text");
col.Insert(new BsonDocument { { "text", "D" } });
col.Insert(new BsonDocument { { "text", "A" } });
col.Insert(new BsonDocument { { "text", "E" } });
col.Insert(new BsonDocument { { "text", "C" } });
col.Insert(new BsonDocument { { "text", "B" } });

var asc = string.Join("",
db.Query("col")
col.EnsureIndex("text");

var asc = string.Join("", col.Query()
.OrderBy("text")
.Select("text")
.ToValues()
.Select(x => x.AsString));

var desc = string.Join("",
db.Query("col")
var desc = string.Join("", col.Query()
.OrderByDescending("text")
.Select("text")
.ToValues()
Expand All @@ -39,10 +39,9 @@ public void Index_Order()
Assert.AreEqual("ABCDE", asc);
Assert.AreEqual("EDCBA", desc);

var indexes = db.Query("$indexes").Where("collection = 'col'").ToArray();

Assert.AreEqual(1, indexes.Count(x => x["name"] == "text"));
var rr = indexes.Query().ToList();

Assert.AreEqual(1, indexes.Count("name = 'text'"));
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions LiteDB.Tests/Engine/Index_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ public class Index_Tests
[TestMethod]
public void Index_With_No_Name()
{
using (var db = new LiteEngine())
using (var db = new LiteDatabase(new MemoryStream()))
{
db.Insert("users", new BsonDocument { ["name"] = new BsonDocument { ["first"] = "John", ["last"] = "Doe" } });
db.Insert("users", new BsonDocument { ["name"] = new BsonDocument { ["first"] = "Marco", ["last"] = "Pollo" } });
var users = db.GetCollection("users");
var indexes = db.GetCollection("$indexes");

users.Insert(new BsonDocument { ["name"] = new BsonDocument { ["first"] = "John", ["last"] = "Doe" } });
users.Insert(new BsonDocument { ["name"] = new BsonDocument { ["first"] = "Marco", ["last"] = "Pollo" } });

// no index name defined
db.EnsureIndex("users", "name.last");
db.EnsureIndex("users", "$.name.first", true);
users.EnsureIndex("name.last");
users.EnsureIndex("$.name.first", true);

// default name: remove all non-[a-z] chars
Assert.IsNotNull(db.Query("$indexes").Where("collection = 'users' AND name = 'namelast'").ExecuteScalar());
Assert.IsNotNull(db.Query("$indexes").Where("collection = 'users' AND name = 'namefirst'").ExecuteScalar());
Assert.IsNotNull(indexes.FindOne("collection = 'users' AND name = 'namelast'"));
Assert.IsNotNull(indexes.FindOne("collection = 'users' AND name = 'namefirst'"));
}
}
}
Expand Down
31 changes: 18 additions & 13 deletions LiteDB.Tests/Engine/Shrink_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Shrink_After_DropCollection()
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
{
db.Insert("col", DataGen.Zip());
db.Insert("col", DataGen.Zip(), BsonAutoId.Int32);

db.DropCollection("col");

Expand All @@ -37,29 +37,32 @@ public void Shrink_After_DropCollection()
public void Shrink_Large_Files()
{
// do some tests
Action<LiteEngine> DoTest = (db) =>
void DoTest(LiteDatabase db, LiteCollection<BsonDocument> col)
{
Assert.AreEqual(1, db.Count("col"));
Assert.AreEqual(1, col.Count());
Assert.AreEqual(99, db.UserVersion);
};

using (var file = new TempFile())
{
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteDatabase(file.Filename))
{
var col = db.GetCollection("col");

db.UserVersion = 99;
db.EnsureIndex("col", "city", false);

var inserted = db.Insert("col", DataGen.Zip()); // 29.353 docs
var deleted = db.DeleteMany("col", "_id != '01001'"); // delete 29.352 docs
col.EnsureIndex("city", false);

var inserted = col.Insert(DataGen.Zip()); // 29.353 docs
var deleted = col.DeleteMany("_id != '01001'"); // delete 29.352 docs

Assert.AreEqual(29353, inserted);
Assert.AreEqual(29352, deleted);

Assert.AreEqual(1, db.Count("col"));
Assert.AreEqual(1, col.Count());

// must checkpoint
db.Checkpoint(false);
db.Checkpoint();

// file still large than 5mb (even with only 1 document)
Assert.IsTrue(file.Size > 5 * 1024 * 1024);
Expand All @@ -70,17 +73,19 @@ public void Shrink_Large_Files()
// now file are small than 50kb
Assert.IsTrue(file.Size < 50 * 1024);

DoTest(db);
DoTest(db, col);
}

// re-open and shrink again
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteDatabase(file.Filename))
{
DoTest(db);
var col = db.GetCollection("col");

DoTest(db, col);

db.Shrink();

DoTest(db);
DoTest(db, col);
}
}
}
Expand Down
50 changes: 29 additions & 21 deletions LiteDB.Tests/Engine/Transactions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ public void Transaction_Write_Lock_Timeout()
var data1 = DataGen.Person(1, 100).ToArray();
var data2 = DataGen.Person(101, 200).ToArray();

using (var db = new LiteEngine(new EngineSettings { Timeout = TimeSpan.FromSeconds(1) }))
using (var db = new LiteDatabase("filename=:memory:;timeout=1"))
{
var person = db.GetCollection("person");

// init person collection with 100 document
db.Insert("person", data1);
person.Insert(data1);

// task A will open transaction and will insert +100 documents
// but will commit only 2s later
var ta = new Task(() =>
{
db.BeginTrans();

db.Insert("person", data2);
person.Insert(data2);

Task.Delay(2000).Wait();

var count = db.Count("person", "1 = 1");
var count = person.Count();

Assert.AreEqual(data1.Length + data2.Length, count);

Expand All @@ -53,11 +55,11 @@ public void Transaction_Write_Lock_Timeout()

try
{
db.DeleteMany("person", BsonExpression.Create("1=1"));
person.DeleteMany("1 = 1");

Assert.Fail("Must be locked");
}
catch(LiteException ex) when (ex.ErrorCode == LiteException.LOCK_TIMEOUT) { }
catch (LiteException ex) when (ex.ErrorCode == LiteException.LOCK_TIMEOUT) { }

});

Expand All @@ -75,22 +77,24 @@ public void Transaction_Avoid_Drity_Read()
var data1 = DataGen.Person(1, 100).ToArray();
var data2 = DataGen.Person(101, 200).ToArray();

using (var db = new LiteEngine())
using (var db = new LiteDatabase(new MemoryStream()))
{
var person = db.GetCollection("person");

// init person collection with 100 document
db.Insert("person", data1);
person.Insert(data1);

// task A will open transaction and will insert +100 documents
// but will commit only 1s later - this plus +100 document must be visible only inside task A
var ta = new Task(() =>
{
db.BeginTrans();

db.Insert("person", data2);
person.Insert(data2);

Task.Delay(1000).Wait();

var count = db.Count("person", "1 = 1");
var count = person.Count();

Assert.AreEqual(data1.Length + data2.Length, count);

Expand All @@ -104,15 +108,15 @@ public void Transaction_Avoid_Drity_Read()
{
Task.Delay(250).Wait();

var count = db.Count("person", "1 = 1"); // using 1=1 to force full scan
var count = person.Count();

// read 100 documents
Assert.AreEqual(data1.Length, count);

ta.Wait();

// read 200 documets
count = db.Count("person", "1 = 1");
count = person.Count();

Assert.AreEqual(data1.Length + data2.Length, count);
});
Expand All @@ -130,17 +134,19 @@ public void Transaction_Read_Version()
var data1 = DataGen.Person(1, 100).ToArray();
var data2 = DataGen.Person(101, 200).ToArray();

using (var db = new LiteEngine())
using (var db = new LiteDatabase(new MemoryStream()))
{
var person = db.GetCollection("person");

// init person collection with 100 document
db.Insert("person", data1);
person.Insert(data1);

// task A will insert more 100 documents but will commit only 1s later
var ta = new Task(() =>
{
db.BeginTrans();

db.Insert("person", data2);
person.Insert(data2);

Task.Delay(1000).Wait();

Expand All @@ -155,15 +161,15 @@ public void Transaction_Read_Version()

Task.Delay(250).Wait();

var count = db.Count("person", "1 = 1"); // using 1=1 to force full scan
var count = person.Count();

// read 100 documents
Assert.AreEqual(data1.Length, count);

ta.Wait();

// keep reading 100 documets because i'm still in same transaction
count = db.Count("person", "1 = 1");
count = person.Count();

Assert.AreEqual(data1.Length, count);
});
Expand All @@ -180,15 +186,17 @@ public void Test_Transaction_States()
{
var data = DataGen.Person(1, 10);

using (var db = new LiteEngine())
using (var db = new LiteDatabase(new MemoryStream()))
{
var person = db.GetCollection("person");

// first time transaction will be opened
Assert.IsTrue(db.BeginTrans());

// but in second type transaction will be same
Assert.IsFalse(db.BeginTrans());

db.Insert("person", data);
person.Insert(data);

// must commit transaction
Assert.IsTrue(db.Commit());
Expand All @@ -205,9 +213,9 @@ public void Test_Transaction_States()
Assert.IsTrue(db.Rollback());

// auto-commit
db.Insert("person", DataGen.Person().Take(20));
person.Insert(DataGen.Person().Take(20));

Assert.AreEqual(30, db.Count("person"));
Assert.AreEqual(30, person.Count());
}
}
}
Expand Down
Loading

0 comments on commit ce0b8b0

Please sign in to comment.