forked from litedb-org/LiteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update_Tests.cs
99 lines (72 loc) · 2.69 KB
/
Update_Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using FluentAssertions;
using LiteDB.Engine;
using Xunit;
namespace LiteDB.Tests.Engine
{
public class Update_Tests
{
[Fact]
public void Update_IndexNodes()
{
using (var db = new LiteEngine())
{
var doc = new BsonDocument {["_id"] = 1, ["name"] = "Mauricio", ["phones"] = new BsonArray() {"51", "11"}};
db.Insert("col1", doc);
db.EnsureIndex("col1", "idx_name", "name", false);
db.EnsureIndex("col1", "idx_phones", "phones[*]", false);
doc["name"] = "David";
doc["phones"] = new BsonArray() {"11", "25"};
db.Update("col1", doc);
doc["name"] = "John";
db.Update("col1", doc);
}
}
[Fact]
public void Update_ExtendBlocks()
{
using (var db = new LiteEngine())
{
var doc = new BsonDocument {["_id"] = 1, ["d"] = new byte[1000]};
db.Insert("col1", doc);
// small (same page)
doc["d"] = new byte[300];
db.Update("col1", doc);
var page3 = db.GetPageLog(3);
page3["freeBytes"].AsInt32.Should().Be(7828);
// big (same page)
doc["d"] = new byte[2000];
db.Update("col1", doc);
page3 = db.GetPageLog(3);
page3["freeBytes"].AsInt32.Should().Be(6128);
// big (extend page)
doc["d"] = new byte[20000];
db.Update("col1", doc);
page3 = db.GetPageLog(3);
var page4 = db.GetPageLog(4);
var page5 = db.GetPageLog(5);
page3["freeBytes"].AsInt32.Should().Be(0);
page4["freeBytes"].AsInt32.Should().Be(0);
page5["freeBytes"].AsInt32.Should().Be(4428);
// small (shrink page)
doc["d"] = new byte[10000];
db.Update("col1", doc);
page3 = db.GetPageLog(3);
page4 = db.GetPageLog(4);
page5 = db.GetPageLog(5);
page3["freeBytes"].AsInt32.Should().Be(0);
page4["freeBytes"].AsInt32.Should().Be(6278);
page5["pageType"].AsString.Should().Be("Empty");
}
}
[Fact]
public void Update_Empty_Collection()
{
using(var e = new LiteEngine())
{
var d = new BsonDocument { ["_id"] = 1, ["a"] = "demo" };
var r = e.Update("col1", new BsonDocument[] { d });
r.Should().Be(0);
}
}
}
}