forked from litedb-org/LiteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rebuild_Tests.cs
122 lines (92 loc) · 3.56 KB
/
Rebuild_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using FluentAssertions;
using LiteDB.Engine;
using System;
using System.IO;
using System.Linq;
using Xunit;
namespace LiteDB.Tests.Engine
{
public class Rebuild_Tests
{
[Fact]
public void Rebuild_After_DropCollection()
{
using (var file = new TempFile())
using (var db = new LiteDatabase(file.Filename))
{
var col = db.GetCollection<Zip>("zip");
col.Insert(DataGen.Zip());
db.DropCollection("zip");
db.Checkpoint();
// full disk usage
var size = file.Size;
var r = db.Rebuild();
// only header page
Assert.Equal(8192, size - r);
}
}
[Fact]
public void Rebuild_Large_Files()
{
// do some tests
void DoTest(ILiteDatabase db, ILiteCollection<Zip> col)
{
Assert.Equal(1, col.Count());
Assert.Equal(99, db.UserVersion);
};
using (var file = new TempFile())
{
using (var db = new LiteDatabase(file.Filename))
{
var col = db.GetCollection<Zip>();
db.UserVersion = 99;
col.EnsureIndex("city", false);
var inserted = col.Insert(DataGen.Zip()); // 29.353 docs
var deleted = col.DeleteMany(x => x.Id != "01001"); // delete 29.352 docs
Assert.Equal(29353, inserted);
Assert.Equal(29352, deleted);
Assert.Equal(1, col.Count());
// must checkpoint
db.Checkpoint();
// file still large than 5mb (even with only 1 document)
Assert.True(file.Size > 5 * 1024 * 1024);
// reduce datafile
var reduced = db.Rebuild();
// now file are small than 50kb
Assert.True(file.Size < 50 * 1024);
DoTest(db, col);
}
// re-open and rebuild again
using (var db = new LiteDatabase(file.Filename))
{
var col = db.GetCollection<Zip>();
DoTest(db, col);
db.Rebuild();
DoTest(db, col);
}
}
}
[Fact (Skip = "Not supported yet")]
public void Rebuild_Change_Culture_Error()
{
using (var file = new TempFile())
using (var db = new LiteDatabase(file.Filename))
{
// remove string comparer ignore case
db.Rebuild(new RebuildOptions { Collation = new Collation("en-US/None") });
// insert 2 documents with different ID in case sensitive
db.GetCollection("col1").Insert(new BsonDocument[]
{
new BsonDocument { ["_id"] = "ana" },
new BsonDocument { ["_id"] = "ANA" }
});
// migrate to ignorecase
db.Rebuild(new RebuildOptions { Collation = new Collation("en-US/IgnoreCase"), IncludeErrorReport = true });
// check for rebuild errors
db.GetCollection("_rebuild_errors").Count().Should().BeGreaterThan(0);
// test if current pragma still with collation none
db.Pragma(Pragmas.COLLATION).AsString.Should().Be("en-US/None");
}
}
}
}