Skip to content

Commit

Permalink
Ported over some tests from LiteDB 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rejemy committed Mar 3, 2020
1 parent e6925a3 commit 8e63173
Show file tree
Hide file tree
Showing 45 changed files with 3,318 additions and 0 deletions.
244 changes: 244 additions & 0 deletions UltraLiteDB.Tests/Database/AutoId_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;

namespace UltraLiteDB.Tests.Database
{
#region Model

public class EntityInt
{
public int Id { get; set; }
public string Name { get; set; }
}

public class EntityLong
{
public long Id { get; set; }
public string Name { get; set; }
}

public class EntityGuid
{
public Guid Id { get; set; }
public string Name { get; set; }
}

public class EntityOid
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}

public class EntityString
{
public string Id { get; set; }
public string Name { get; set; }
}

#endregion

[TestClass]
public class AutoId_Tests
{
[TestMethod]
public void AutoId_Strong_Typed()
{
var mapper = new BsonMapper();

using (var db = new UltraLiteDatabase(new MemoryStream(), mapper))
{
var cs_int = db.GetCollection<EntityInt>("int");
var cs_long = db.GetCollection<EntityLong>("long");
var cs_guid = db.GetCollection<EntityGuid>("guid");
var cs_oid = db.GetCollection<EntityOid>("oid");
var cs_str = db.GetCollection<EntityString>("str");

// int32
var cint_1 = new EntityInt() { Name = "R1" };
var cint_2 = new EntityInt() { Name = "R2" };
var cint_3 = new EntityInt() { Name = "R3" };
var cint_4 = new EntityInt() { Name = "R4" };

// long
var clong_1 = new EntityLong() { Name = "R1" };
var clong_2 = new EntityLong() { Name = "R2" };
var clong_3 = new EntityLong() { Name = "R3" };
var clong_4 = new EntityLong() { Name = "R4" };

// guid
var cguid_1 = new EntityGuid() { Name = "R1" };
var cguid_2 = new EntityGuid() { Name = "R2" };
var cguid_3 = new EntityGuid() { Name = "R3" };
var cguid_4 = new EntityGuid() { Name = "R4" };

// oid
var coid_1 = new EntityOid() { Name = "R1" };
var coid_2 = new EntityOid() { Name = "R2" };
var coid_3 = new EntityOid() { Name = "R3" };
var coid_4 = new EntityOid() { Name = "R4" };

// string - there is no AutoId for string
var cstr_1 = new EntityString() { Id = "a", Name = "R1" };
var cstr_2 = new EntityString() { Id = "b", Name = "R2" };
var cstr_3 = new EntityString() { Id = "c", Name = "R3" };
var cstr_4 = new EntityString() { Id = "d", Name = "R4" };

// insert first 3 documents
cs_int.Insert(new[] { cint_1, cint_2, cint_3 });
cs_long.Insert(new[] { clong_1, clong_2, clong_3 });
cs_guid.Insert(new[] { cguid_1, cguid_2, cguid_3 });
cs_oid.Insert(new[] { coid_1, coid_2, coid_3 });
cs_str.Insert(new[] { cstr_1, cstr_2, cstr_3 });

// change document 2
cint_2.Name = "Changed 2";
clong_2.Name = "Changed 2";
cguid_2.Name = "Changed 2";
coid_2.Name = "Changed 2";
cstr_2.Name = "Changed 2";

// update document 2
var nu_int = cs_int.Update(cint_2);
var nu_long = cs_long.Update(clong_2);
var nu_guid = cs_guid.Update(cguid_2);
var nu_oid = cs_oid.Update(coid_2);
var nu_str = cs_str.Update(cstr_2);

Assert.IsTrue(nu_int);
Assert.IsTrue(nu_long);
Assert.IsTrue(nu_guid);
Assert.IsTrue(nu_oid);
Assert.IsTrue(nu_str);

// change document 3
cint_3.Name = "Changed 3";
clong_3.Name = "Changed 3";
cguid_3.Name = "Changed 3";
coid_3.Name = "Changed 3";
cstr_3.Name = "Changed 3";

// upsert (update) document 3
var fu_int = cs_int.Upsert(cint_3);
var fu_long = cs_long.Upsert(clong_3);
var fu_guid = cs_guid.Upsert(cguid_3);
var fu_oid = cs_oid.Upsert(coid_3);
var fu_str = cs_str.Upsert(cstr_3);

Assert.IsFalse(fu_int);
Assert.IsFalse(fu_long);
Assert.IsFalse(fu_guid);
Assert.IsFalse(fu_oid);
Assert.IsFalse(fu_str);

// test if was changed
Assert.AreEqual(cint_3.Name, cs_int.FindOne(Query.EQ("_id", cint_3.Id)).Name);
Assert.AreEqual(clong_3.Name, cs_long.FindOne(Query.EQ("_id", clong_3.Id)).Name);
Assert.AreEqual(cguid_3.Name, cs_guid.FindOne(Query.EQ("_id", cguid_3.Id)).Name);
Assert.AreEqual(coid_3.Name, cs_oid.FindOne(Query.EQ("_id", coid_3.Id)).Name);
Assert.AreEqual(cstr_3.Name, cs_str.FindOne(Query.EQ("_id", cstr_3.Id)).Name);

// upsert (insert) document 4
var tu_int = cs_int.Upsert(cint_4);
var tu_long = cs_long.Upsert(clong_4);
var tu_guid = cs_guid.Upsert(cguid_4);
var tu_oid = cs_oid.Upsert(coid_4);
var tu_str = cs_str.Upsert(cstr_4);

Assert.IsTrue(tu_int);
Assert.IsTrue(tu_long);
Assert.IsTrue(tu_guid);
Assert.IsTrue(tu_oid);
Assert.IsTrue(tu_str);

// test if was included
Assert.AreEqual(cint_4.Name, cs_int.FindOne(Query.EQ("_id", cint_4.Id)).Name);
Assert.AreEqual(clong_4.Name, cs_long.FindOne(Query.EQ("_id", clong_4.Id)).Name);
Assert.AreEqual(cguid_4.Name, cs_guid.FindOne(Query.EQ("_id", cguid_4.Id)).Name);
Assert.AreEqual(coid_4.Name, cs_oid.FindOne(Query.EQ("_id", coid_4.Id)).Name);
Assert.AreEqual(cstr_4.Name, cs_str.FindOne(Query.EQ("_id", cstr_4.Id)).Name);

// count must be 4
Assert.AreEqual(4, cs_int.Count(Query.All()));
Assert.AreEqual(4, cs_long.Count(Query.All()));
Assert.AreEqual(4, cs_guid.Count(Query.All()));
Assert.AreEqual(4, cs_oid.Count(Query.All()));
Assert.AreEqual(4, cs_str.Count(Query.All()));

// for Int32 (or Int64) - add "bouble" on sequence
var cint_10 = new EntityInt { Id = 10, Name = "R10" };
var cint_11 = new EntityInt { Name = "R11" };
var cint_7 = new EntityInt { Id = 7, Name = "R7" };
var cint_12 = new EntityInt { Name = "R12" };

cs_int.Insert(cint_10); // "loose" sequente between 5-9
cs_int.Insert(cint_11); // insert as 11
cs_int.Insert(cint_7); // insert as 7
cs_int.Insert(cint_12); // insert as 12

Assert.AreEqual(10, cint_10.Id);
Assert.AreEqual(11, cint_11.Id);
Assert.AreEqual(7, cint_7.Id);
Assert.AreEqual(12, cint_12.Id);
}
}

[TestMethod]
public void AutoId_No_Duplicate_After_Delete()
{
// using strong type
using (var db = new UltraLiteDatabase(new MemoryStream()))
{
var col = db.GetCollection<EntityInt>("col1");

var one = new EntityInt { Name = "One" };
var two = new EntityInt { Name = "Two" };
var three = new EntityInt { Name = "Three" };
var four = new EntityInt { Name = "Four" };

// insert
col.Insert(one);
col.Insert(two);

Assert.AreEqual(1, one.Id);
Assert.AreEqual(2, two.Id);

// now delete first 2 rows
col.Delete(one.Id);
col.Delete(two.Id);

// and insert new documents
col.Insert(new EntityInt[] { three, four });

Assert.AreEqual(3, three.Id);
Assert.AreEqual(4, four.Id);
}

// using bsondocument/engine
using (var db = new UltraLiteEngine(new MemoryStream()))
{
var one = new BsonDocument { ["Name"] = "One" };
var two = new BsonDocument { ["Name"] = "Two" };
var three = new BsonDocument { ["Name"] = "Three" };
var four = new BsonDocument { ["Name"] = "Four" };

db.Insert("col", one, BsonAutoId.Int32);
db.Insert("col", two, BsonAutoId.Int32);

Assert.AreEqual(1, one["_id"].AsInt32);
Assert.AreEqual(2, two["_id"].AsInt32);

// now delete first 2 rows
db.Delete("col", one["_id"].AsInt32);
db.Delete("col", two["_id"].AsInt32);

// and insert new documents
db.Insert("col", new BsonDocument[] { three, four }, BsonAutoId.Int32);

Assert.AreEqual(3, three["_id"].AsInt32);
Assert.AreEqual(4, four["_id"].AsInt32);
}
}
}
}
124 changes: 124 additions & 0 deletions UltraLiteDB.Tests/Database/ConnectionString_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace UltraLiteDB.Tests.Database
{
[TestClass]
public class ConnectionString_Tests
{
[TestMethod]
public void ConnectionString_NoArguments()
{
// Verify that the default ConnectionString contains the appropriate defaults
var defaults = new ConnectionString();
AssertDefaults(defaults, false);
}

[TestMethod]
public void ConnectionString_Parser()
{
// only filename
var onlyfile = new ConnectionString(@"demo.db");

Assert.AreEqual(@"demo.db", onlyfile.Filename);
AssertDefaults(onlyfile, true);

// file with spaces without "
var normal = new ConnectionString(@"filename=c:\only file\demo.db; journal=false");

Assert.AreEqual(@"c:\only file\demo.db", normal.Filename);
Assert.AreEqual(false, normal.Journal);

// filename with timeout
var filenameTimeout = new ConnectionString(@"filename = my demo.db ; timeout = 1:00:00");

Assert.AreEqual(@"my demo.db", filenameTimeout.Filename);
Assert.AreEqual(TimeSpan.FromHours(1), filenameTimeout.Timeout);

// file with spaces with " and ;
var fullConnectionString = @"filename=""c:\only;file\""d\""emo.db"";
journal =false;
password = ""john-doe "" ;
cache SIZE = 1000 ;
timeout = 00:05:00 ;
initial size = 10 MB ;
mode = excluSIVE ;
limit SIZE = 20mb;
log = 255 ;
utc=true ;
upgrade=true;
async=true";
var full = new ConnectionString(fullConnectionString);

Assert.AreEqual(@"c:\only;file""d""emo.db", full.Filename);
Assert.AreEqual(false, full.Journal);
Assert.AreEqual("john-doe ", full.Password);
Assert.AreEqual(1000, full.CacheSize);
Assert.AreEqual(TimeSpan.FromMinutes(5), full.Timeout);
Assert.AreEqual(10 * 1024 * 1024, full.InitialSize);
Assert.AreEqual(20 * 1024 * 1024, full.LimitSize);
Assert.AreEqual(255, full.Log);
Assert.AreEqual(true, full.UtcDate);
Assert.AreEqual(true, full.Async);
}

[TestMethod]
public void ConnectionString_Sets_Log_Level()
{
var connectionString = "filename=foo;";
var db = new UltraLiteDatabase(connectionString);
Assert.AreEqual(0, db.Log.Level);

connectionString = "filename=foo;log=" + Logger.FULL;
db = new UltraLiteDatabase(connectionString);
Assert.AreEqual(Logger.FULL, db.Log.Level);
}

[TestMethod]
public void ConnectionString_MetaTest()
{
// This test is a meta test that verifies that all of the properties present in ConnectionString are also tested by this test.
// If this test fails, you should make sure that you don't need to update this test. (In particular, you almost certainly need to update AssertDefaults.)
var expectedProperties = new HashSet<string>()
{
"Filename",
"Journal",
"Password",
"CacheSize",
"Timeout",
"Mode",
"InitialSize",
"LimitSize",
"Log",
"UtcDate",
"Upgrade",
"Async",
"Flush"
};

var actualProperties = new HashSet<string>(typeof(ConnectionString).GetProperties().Select(p => p.Name));
actualProperties.ExceptWith(expectedProperties);

// If the below assert fails, properties were added to ConnectionString without updating this test.
Assert.AreEqual(0, actualProperties.Count);
}

private void AssertDefaults(ConnectionString connectionString, bool skipFilename)
{
if (!skipFilename)
{ Assert.AreEqual("", connectionString.Filename); }

Assert.AreEqual(true, connectionString.Journal);
Assert.AreEqual(null, connectionString.Password);
Assert.AreEqual(5000, connectionString.CacheSize);
Assert.AreEqual(new TimeSpan(0, 1, 0), connectionString.Timeout);
Assert.AreEqual(0, connectionString.InitialSize);
Assert.AreEqual(long.MaxValue, connectionString.LimitSize);
Assert.AreEqual(Logger.NONE, connectionString.Log);
Assert.AreEqual(false, connectionString.UtcDate);
Assert.AreEqual(false, connectionString.Async);
}
}
}
Loading

0 comments on commit 8e63173

Please sign in to comment.