Skip to content

Commit 1517f66

Browse files
authored
Merge branch 'master' into ReadTransform
2 parents d417e10 + 610e530 commit 1517f66

File tree

23 files changed

+604
-101
lines changed

23 files changed

+604
-101
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using LiteDB;
2+
using FluentAssertions;
3+
using Xunit;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Collections.Generic;
7+
using System;
8+
9+
namespace LiteDB.Tests.Database
10+
{
11+
public class Contains_Tests
12+
{
13+
[Fact]
14+
public void ArrayContains_ShouldHaveCount1()
15+
{
16+
var random = new Random();
17+
var randomValue = random.Next();
18+
19+
using(var database = new LiteDatabase(new MemoryStream()))
20+
{
21+
var collection = database.GetCollection<ItemWithEnumerable>();
22+
collection.Insert(new ItemWithEnumerable
23+
{
24+
Array = new int[] { randomValue }
25+
});
26+
27+
var result = collection.Find(i => i.Array.Contains(randomValue)).ToList();
28+
result.Should().HaveCount(1);
29+
}
30+
}
31+
32+
[Fact]
33+
public void EnumerableAssignedArrayContains_ShouldHaveCount1()
34+
{
35+
var random = new Random();
36+
var randomValue = random.Next();
37+
38+
using(var database = new LiteDatabase(new MemoryStream()))
39+
{
40+
var collection = database.GetCollection<ItemWithEnumerable>();
41+
collection.Insert(new ItemWithEnumerable
42+
{
43+
Enumerable = new int[] { randomValue }
44+
});
45+
46+
var result = collection.Find(i => i.Enumerable.Contains(randomValue)).ToList();
47+
result.Should().HaveCount(1);
48+
}
49+
}
50+
51+
[Fact]
52+
public void EnumerableAssignedListContains_ShouldHaveCount1()
53+
{
54+
var random = new Random();
55+
var randomValue = random.Next();
56+
57+
using(var database = new LiteDatabase(new MemoryStream()))
58+
{
59+
var collection = database.GetCollection<ItemWithEnumerable>();
60+
collection.Insert(new ItemWithEnumerable
61+
{
62+
Enumerable = new List<int> { randomValue }
63+
});
64+
65+
var result = collection.Find(i => i.Enumerable.Contains(randomValue)).ToList();
66+
result.Should().HaveCount(1);
67+
}
68+
}
69+
70+
[Fact]
71+
public void ListContains_ShouldHaveCount1()
72+
{
73+
var random = new Random();
74+
var randomValue = random.Next();
75+
76+
using(var database = new LiteDatabase(new MemoryStream()))
77+
{
78+
var collection = database.GetCollection<ItemWithEnumerable>();
79+
collection.Insert(new ItemWithEnumerable
80+
{
81+
List = new List<int> { randomValue }
82+
});
83+
84+
var result = collection.Find(i => i.List.Contains(randomValue)).ToList();
85+
result.Should().HaveCount(1);
86+
}
87+
}
88+
89+
public class ItemWithEnumerable
90+
{
91+
public int[] Array { get; set; }
92+
public IEnumerable<int> Enumerable { get; set; }
93+
public IList<int> List { get; set; }
94+
}
95+
}
96+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using LiteDB.Utils.Extensions;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
using Xunit;
10+
11+
namespace LiteDB.Tests.Internals;
12+
13+
public class Extensions_Test
14+
{
15+
// Asserts that chained IEnumerable<T>.OnDispose(()=> { }) calls the action on dispose, even when chained
16+
[Fact]
17+
public void EnumerableExtensions_OnDispose()
18+
{
19+
var disposed = false;
20+
var disposed1 = false;
21+
var enumerable = new[] { 1, 2, 3 }.OnDispose(() => disposed = true).OnDispose(() => disposed1 = true);
22+
23+
foreach (var item in enumerable)
24+
{
25+
// do nothing
26+
}
27+
28+
Assert.True(disposed);
29+
Assert.True(disposed1);
30+
}
31+
32+
// tests IDisposable StartDisposable(this Stopwatch stopwatch)
33+
[Fact]
34+
public async Task StopWatchExtensions_StartDisposable()
35+
{
36+
var stopwatch = new System.Diagnostics.Stopwatch();
37+
using (stopwatch.StartDisposable())
38+
{
39+
await Task.Delay(100);
40+
}
41+
42+
Assert.True(stopwatch.ElapsedMilliseconds > 0);
43+
}
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
3+
using Xunit;
4+
5+
namespace LiteDB.Tests.Issues;
6+
7+
// issue 2265
8+
public class Issue2265_Tests
9+
{
10+
public class Weights
11+
{
12+
public int Id { get; set; } = 0;
13+
14+
// comment out [BsonRef] and the the test works
15+
[BsonRef("weights")]
16+
public Weights[] Parents { get; set; }
17+
18+
public Weights(int id, Weights[] parents)
19+
{
20+
Id = id;
21+
Parents = parents;
22+
}
23+
24+
public Weights()
25+
{
26+
Id = 0;
27+
Parents = Array.Empty<Weights>();
28+
}
29+
}
30+
31+
[Fact]
32+
public void Test()
33+
{
34+
using (var db = new LiteDatabase(":memory:"))
35+
{
36+
var c = db.GetCollection<Weights>("weights");
37+
Weights? w = c.FindOne(x => true);
38+
if (w == null)
39+
{
40+
w = new Weights();
41+
c.Insert(w);
42+
}
43+
44+
//return w;
45+
}
46+
}
47+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json;
6+
using System.Threading.Tasks;
7+
8+
using Xunit;
9+
10+
namespace LiteDB.Tests.Issues;
11+
12+
public class Issue2298_Tests
13+
{
14+
public struct Mass
15+
{
16+
public enum Units
17+
{ Pound, Kilogram }
18+
19+
public Mass(double value, Units unit)
20+
{ Value = value; Unit = unit; }
21+
22+
public double Value { get; init; }
23+
public Units Unit { get; init; }
24+
}
25+
26+
public class QuantityRange<T>
27+
{
28+
public QuantityRange(double min, double max, Enum unit)
29+
{ Min = min; Max = max; Unit = unit; }
30+
31+
public double Min { get; init; }
32+
public double Max { get; init; }
33+
public Enum Unit { get; init; }
34+
}
35+
36+
public static QuantityRange<Mass> MassRangeBuilder(BsonDocument document)
37+
{
38+
var doc = JsonDocument.Parse(document.ToString()).RootElement;
39+
var min = doc.GetProperty(nameof(QuantityRange<Mass>.Min)).GetDouble();
40+
var max = doc.GetProperty(nameof(QuantityRange<Mass>.Max)).GetDouble();
41+
var unit = Enum.Parse<Mass.Units>(doc.GetProperty(nameof(QuantityRange<Mass>.Unit)).GetString());
42+
43+
var restored = new QuantityRange<Mass>(min, max, unit);
44+
return restored;
45+
}
46+
47+
[Fact]
48+
public void We_Dont_Need_Ctor()
49+
{
50+
BsonMapper.Global.RegisterType<QuantityRange<Mass>>(
51+
serialize: (range) => new BsonDocument
52+
{
53+
{ nameof(QuantityRange<Mass>.Min), range.Min },
54+
{ nameof(QuantityRange<Mass>.Max), range.Max },
55+
{ nameof(QuantityRange<Mass>.Unit), range.Unit.ToString() }
56+
},
57+
deserialize: (document) => MassRangeBuilder(document as BsonDocument)
58+
);
59+
60+
var range = new QuantityRange<Mass>(100, 500, Mass.Units.Pound);
61+
var filename = "Demo.DB";
62+
var DB = new LiteDatabase(filename);
63+
var collection = DB.GetCollection<QuantityRange<Mass>>("DEMO");
64+
collection.Insert(range);
65+
var restored = collection.FindAll().First();
66+
}
67+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using FluentAssertions;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
using Xunit;
12+
13+
namespace LiteDB.Tests.Issues;
14+
15+
public class Issue2471_Test
16+
{
17+
[Fact]
18+
public void TestFragmentDB_FindByIDException()
19+
{
20+
using var db = new LiteDatabase(":memory:");
21+
var collection = db.GetCollection<object>("fragtest");
22+
23+
var fragment = new object { };
24+
var id = collection.Insert(fragment);
25+
26+
id.Should().BeGreaterThan(0);
27+
28+
var frag2 = collection.FindById(id);
29+
frag2.Should().NotBeNull();
30+
31+
Action act = () => db.Checkpoint();
32+
33+
act.Should().NotThrow();
34+
}
35+
36+
[Fact]
37+
public void MultipleReadCleansUpTransaction()
38+
{
39+
using var database = new LiteDatabase(":memory:");
40+
41+
var collection = database.GetCollection("test");
42+
collection.Insert(new BsonDocument { ["_id"] = 1 });
43+
44+
for (int i = 0; i < 500; i++)
45+
{
46+
collection.FindById(1);
47+
}
48+
}
49+
50+
#region Model
51+
52+
public class User
53+
{
54+
public int Id { get; set; }
55+
public string Name { get; set; }
56+
public int[] Phones { get; set; }
57+
public List<Address> Addresses { get; set; }
58+
}
59+
60+
public class Address
61+
{
62+
public string Street { get; set; }
63+
}
64+
65+
#endregion Model
66+
67+
// Copied from IndexMultiKeyIndex, but this time we ensure that the lock is released by calling db.Checkpoint()
68+
[Fact]
69+
public void Ensure_Query_GetPlan_Releases_Lock()
70+
{
71+
using var db = new LiteDatabase(new MemoryStream());
72+
var col = db.GetCollection<User>();
73+
74+
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" } } });
75+
col.Insert(new User { Name = "Joana Mark", Phones = new int[] { 1, 4 }, Addresses = new List<Address> { new Address { Street = "Av.3" } } });
76+
77+
// create indexes
78+
col.EnsureIndex(x => x.Phones);
79+
col.EnsureIndex(x => x.Addresses.Select(z => z.Street));
80+
81+
// testing indexes expressions
82+
var indexes = db.GetCollection("$indexes").FindAll().ToArray();
83+
84+
indexes[1]["expression"].AsString.Should().Be("$.Phones[*]");
85+
indexes[2]["expression"].AsString.Should().Be("MAP($.Addresses[*]=>@.Street)");
86+
87+
// doing Phone query
88+
var queryPhone = col.Query()
89+
.Where(x => x.Phones.Contains(3));
90+
91+
var planPhone = queryPhone.GetPlan();
92+
93+
Action act = () => db.Checkpoint();
94+
95+
act.Should().NotThrow();
96+
}
97+
}

0 commit comments

Comments
 (0)