Skip to content

Commit

Permalink
Merge branch 'master' into ReadTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed Jun 7, 2024
2 parents d417e10 + 610e530 commit 1517f66
Show file tree
Hide file tree
Showing 23 changed files with 604 additions and 101 deletions.
96 changes: 96 additions & 0 deletions LiteDB.Tests/Database/Contains_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using LiteDB;
using FluentAssertions;
using Xunit;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System;

namespace LiteDB.Tests.Database
{
public class Contains_Tests
{
[Fact]
public void ArrayContains_ShouldHaveCount1()
{
var random = new Random();
var randomValue = random.Next();

using(var database = new LiteDatabase(new MemoryStream()))
{
var collection = database.GetCollection<ItemWithEnumerable>();
collection.Insert(new ItemWithEnumerable
{
Array = new int[] { randomValue }
});

var result = collection.Find(i => i.Array.Contains(randomValue)).ToList();
result.Should().HaveCount(1);
}
}

[Fact]
public void EnumerableAssignedArrayContains_ShouldHaveCount1()
{
var random = new Random();
var randomValue = random.Next();

using(var database = new LiteDatabase(new MemoryStream()))
{
var collection = database.GetCollection<ItemWithEnumerable>();
collection.Insert(new ItemWithEnumerable
{
Enumerable = new int[] { randomValue }
});

var result = collection.Find(i => i.Enumerable.Contains(randomValue)).ToList();
result.Should().HaveCount(1);
}
}

[Fact]
public void EnumerableAssignedListContains_ShouldHaveCount1()
{
var random = new Random();
var randomValue = random.Next();

using(var database = new LiteDatabase(new MemoryStream()))
{
var collection = database.GetCollection<ItemWithEnumerable>();
collection.Insert(new ItemWithEnumerable
{
Enumerable = new List<int> { randomValue }
});

var result = collection.Find(i => i.Enumerable.Contains(randomValue)).ToList();
result.Should().HaveCount(1);
}
}

[Fact]
public void ListContains_ShouldHaveCount1()
{
var random = new Random();
var randomValue = random.Next();

using(var database = new LiteDatabase(new MemoryStream()))
{
var collection = database.GetCollection<ItemWithEnumerable>();
collection.Insert(new ItemWithEnumerable
{
List = new List<int> { randomValue }
});

var result = collection.Find(i => i.List.Contains(randomValue)).ToList();
result.Should().HaveCount(1);
}
}

public class ItemWithEnumerable
{
public int[] Array { get; set; }
public IEnumerable<int> Enumerable { get; set; }
public IList<int> List { get; set; }
}
}
}
44 changes: 44 additions & 0 deletions LiteDB.Tests/Internals/Extensions_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using LiteDB.Utils.Extensions;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xunit;

namespace LiteDB.Tests.Internals;

public class Extensions_Test
{
// Asserts that chained IEnumerable<T>.OnDispose(()=> { }) calls the action on dispose, even when chained
[Fact]
public void EnumerableExtensions_OnDispose()
{
var disposed = false;
var disposed1 = false;
var enumerable = new[] { 1, 2, 3 }.OnDispose(() => disposed = true).OnDispose(() => disposed1 = true);

foreach (var item in enumerable)
{
// do nothing
}

Assert.True(disposed);
Assert.True(disposed1);
}

// tests IDisposable StartDisposable(this Stopwatch stopwatch)
[Fact]
public async Task StopWatchExtensions_StartDisposable()
{
var stopwatch = new System.Diagnostics.Stopwatch();
using (stopwatch.StartDisposable())
{
await Task.Delay(100);
}

Assert.True(stopwatch.ElapsedMilliseconds > 0);
}
}
47 changes: 47 additions & 0 deletions LiteDB.Tests/Issues/Issue2265_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

using Xunit;

namespace LiteDB.Tests.Issues;

// issue 2265
public class Issue2265_Tests
{
public class Weights
{
public int Id { get; set; } = 0;

// comment out [BsonRef] and the the test works
[BsonRef("weights")]
public Weights[] Parents { get; set; }

public Weights(int id, Weights[] parents)
{
Id = id;
Parents = parents;
}

public Weights()
{
Id = 0;
Parents = Array.Empty<Weights>();
}
}

[Fact]
public void Test()
{
using (var db = new LiteDatabase(":memory:"))
{
var c = db.GetCollection<Weights>("weights");
Weights? w = c.FindOne(x => true);
if (w == null)
{
w = new Weights();
c.Insert(w);
}

//return w;
}
}
}
67 changes: 67 additions & 0 deletions LiteDB.Tests/Issues/Issue2298_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2298_Tests
{
public struct Mass
{
public enum Units
{ Pound, Kilogram }

public Mass(double value, Units unit)
{ Value = value; Unit = unit; }

public double Value { get; init; }
public Units Unit { get; init; }
}

public class QuantityRange<T>
{
public QuantityRange(double min, double max, Enum unit)
{ Min = min; Max = max; Unit = unit; }

public double Min { get; init; }
public double Max { get; init; }
public Enum Unit { get; init; }
}

public static QuantityRange<Mass> MassRangeBuilder(BsonDocument document)
{
var doc = JsonDocument.Parse(document.ToString()).RootElement;
var min = doc.GetProperty(nameof(QuantityRange<Mass>.Min)).GetDouble();
var max = doc.GetProperty(nameof(QuantityRange<Mass>.Max)).GetDouble();
var unit = Enum.Parse<Mass.Units>(doc.GetProperty(nameof(QuantityRange<Mass>.Unit)).GetString());

var restored = new QuantityRange<Mass>(min, max, unit);
return restored;
}

[Fact]
public void We_Dont_Need_Ctor()
{
BsonMapper.Global.RegisterType<QuantityRange<Mass>>(
serialize: (range) => new BsonDocument
{
{ nameof(QuantityRange<Mass>.Min), range.Min },
{ nameof(QuantityRange<Mass>.Max), range.Max },
{ nameof(QuantityRange<Mass>.Unit), range.Unit.ToString() }
},
deserialize: (document) => MassRangeBuilder(document as BsonDocument)
);

var range = new QuantityRange<Mass>(100, 500, Mass.Units.Pound);
var filename = "Demo.DB";
var DB = new LiteDatabase(filename);
var collection = DB.GetCollection<QuantityRange<Mass>>("DEMO");
collection.Insert(range);
var restored = collection.FindAll().First();
}
}
97 changes: 97 additions & 0 deletions LiteDB.Tests/Issues/Issue2471_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using FluentAssertions;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2471_Test
{
[Fact]
public void TestFragmentDB_FindByIDException()
{
using var db = new LiteDatabase(":memory:");
var collection = db.GetCollection<object>("fragtest");

var fragment = new object { };
var id = collection.Insert(fragment);

id.Should().BeGreaterThan(0);

var frag2 = collection.FindById(id);
frag2.Should().NotBeNull();

Action act = () => db.Checkpoint();

act.Should().NotThrow();
}

[Fact]
public void MultipleReadCleansUpTransaction()
{
using var database = new LiteDatabase(":memory:");

var collection = database.GetCollection("test");
collection.Insert(new BsonDocument { ["_id"] = 1 });

for (int i = 0; i < 500; i++)
{
collection.FindById(1);
}
}

#region Model

public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int[] Phones { get; set; }
public List<Address> Addresses { get; set; }
}

public class Address
{
public string Street { get; set; }
}

#endregion Model

// Copied from IndexMultiKeyIndex, but this time we ensure that the lock is released by calling db.Checkpoint()
[Fact]
public void Ensure_Query_GetPlan_Releases_Lock()
{
using var db = new LiteDatabase(new MemoryStream());
var col = db.GetCollection<User>();

col.Insert(new User { Name = "John Doe", Phones = new int[] { 1, 3, 5 }, Addresses = new List<Address> { new Address { Street = "Av.1" }, new Address { Street = "Av.3" } } });
col.Insert(new User { Name = "Joana Mark", Phones = new int[] { 1, 4 }, Addresses = new List<Address> { new Address { Street = "Av.3" } } });

// create indexes
col.EnsureIndex(x => x.Phones);
col.EnsureIndex(x => x.Addresses.Select(z => z.Street));

// testing indexes expressions
var indexes = db.GetCollection("$indexes").FindAll().ToArray();

indexes[1]["expression"].AsString.Should().Be("$.Phones[*]");
indexes[2]["expression"].AsString.Should().Be("MAP($.Addresses[*]=>@.Street)");

// doing Phone query
var queryPhone = col.Query()
.Where(x => x.Phones.Contains(3));

var planPhone = queryPhone.GetPlan();

Action act = () => db.Checkpoint();

act.Should().NotThrow();
}
}
Loading

0 comments on commit 1517f66

Please sign in to comment.