Skip to content

Commit f23f14c

Browse files
committed
Fix unit tests to 5.0.19
1 parent 663f749 commit f23f14c

16 files changed

+123
-98
lines changed

LiteDB.Tests/Database/Upgrade_Tests.cs

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using LiteDB;
55
using FluentAssertions;
66
using Xunit;
7+
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
78

89
namespace LiteDB.Tests.Database
910
{
@@ -13,80 +14,48 @@ public class Upgrade_Tests
1314
public void Migrage_From_V4()
1415
{
1516
// v5 upgrades only from v4!
16-
17-
var original = "../../../Resources/v4.db";
18-
var copy = original.Replace(".db", "-copy.db");
19-
20-
File.Copy(original, copy, true);
21-
22-
try
17+
using(var tempFile = new TempFile("../../../Resources/v4.db"))
2318
{
24-
25-
using(var db = new LiteDatabase($"filename={copy};upgrade=true"))
19+
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
2620
{
2721
// convert and open database
2822
var col1 = db.GetCollection("col1");
2923

3024
col1.Count().Should().Be(3);
3125
}
3226

33-
using (var db = new LiteDatabase($"filename={copy};upgrade=true"))
27+
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
3428
{
3529
// database already converted
3630
var col1 = db.GetCollection("col1");
3731

3832
col1.Count().Should().Be(3);
3933
}
4034
}
41-
finally
42-
{
43-
File.Delete(copy);
44-
45-
foreach(var backups in Directory.GetFiles(Path.GetDirectoryName(copy), "*-backup*.db"))
46-
{
47-
File.Delete(backups);
48-
}
49-
}
5035
}
5136

5237
[Fact]
5338
public void Migrage_From_V4_No_FileExtension()
5439
{
5540
// v5 upgrades only from v4!
56-
57-
var original = "../../../Resources/v4.db";
58-
var copy = original.Replace(".db", "-copy");
59-
60-
File.Copy(original, copy, true);
61-
62-
try
41+
using (var tempFile = new TempFile("../../../Resources/v4.db"))
6342
{
64-
65-
using (var db = new LiteDatabase($"filename={copy};upgrade=true"))
43+
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
6644
{
6745
// convert and open database
6846
var col1 = db.GetCollection("col1");
6947

7048
col1.Count().Should().Be(3);
7149
}
7250

73-
using (var db = new LiteDatabase($"filename={copy};upgrade=true"))
51+
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
7452
{
7553
// database already converted
7654
var col1 = db.GetCollection("col1");
7755

7856
col1.Count().Should().Be(3);
7957
}
8058
}
81-
finally
82-
{
83-
File.Delete(copy);
84-
85-
foreach (var backups in Directory.GetFiles(Path.GetDirectoryName(copy), "*-backup*"))
86-
{
87-
File.Delete(backups);
88-
}
89-
}
9059
}
9160
}
9261
}

LiteDB.Tests/Engine/Rebuild_Loop_Tests.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using FluentAssertions;
2+
using LiteDB.Engine;
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
7+
using Xunit;
8+
9+
namespace LiteDB.Tests.Issues
10+
{
11+
public class Issue2417_Tests
12+
{
13+
[Fact]
14+
public void Rebuild_Detected_Infinite_Loop()
15+
{
16+
var original = "../../../Resources/Issue2417_MyData.db";
17+
18+
using (var filename = new TempFile(original))
19+
{
20+
var settings = new EngineSettings
21+
{
22+
Filename = filename,
23+
AutoRebuild = true,
24+
};
25+
26+
try
27+
{
28+
using (var db = new LiteEngine(settings))
29+
{
30+
// infinite loop here
31+
var col = db.Query("customers", Query.All()).ToList();
32+
33+
// never run here
34+
Assert.Fail("not expected");
35+
}
36+
}
37+
catch (Exception ex)
38+
{
39+
Assert.True(ex is LiteException lex && lex.ErrorCode == 999);
40+
}
41+
42+
using (var db = new LiteEngine(settings))
43+
{
44+
var col = db.Query("customers", Query.All()).ToList().Count;
45+
var errors = db.Query("_rebuild_errors", Query.All()).ToList().Count;
46+
47+
col.Should().Be(4);
48+
errors.Should().Be(0);
49+
}
50+
}
51+
}
52+
53+
[Fact]
54+
public void Rebuild_Detected_Infinite_Loop_With_Password()
55+
{
56+
var original = "../../../Resources/Issue2417_TestCacheDb.db";
57+
58+
using (var filename = new TempFile(original))
59+
{
60+
var settings = new EngineSettings
61+
{
62+
Filename = filename,
63+
Password = "bzj2NplCbVH/bB8fxtjEC7u0unYdKHJVSmdmPgArRBwmmGw0+Wd2tE+b2zRMFcHAzoG71YIn/2Nq1EMqa5JKcQ==",
64+
AutoRebuild = true,
65+
};
66+
67+
try
68+
{
69+
using (var db = new LiteEngine(settings))
70+
{
71+
// infinite loop here
72+
var col = db.Query("hubData$AppOperations", Query.All()).ToList();
73+
74+
// never run here
75+
Assert.Fail("not expected");
76+
}
77+
}
78+
catch (Exception ex)
79+
{
80+
Assert.True(ex is LiteException lex && lex.ErrorCode == 999);
81+
}
82+
83+
using (var db = new LiteEngine(settings))
84+
{
85+
var col = db.Query("hubData$AppOperations", Query.All()).ToList().Count;
86+
var errors = db.Query("_rebuild_errors", Query.All()).ToList().Count;
87+
88+
col.Should().Be(408);
89+
errors.Should().Be(0);
90+
}
91+
}
92+
}
93+
}
94+
}
95+
File renamed without changes.
640 KB
Binary file not shown.

LiteDB.Tests/Resources/Loop-copy

-272 KB
Binary file not shown.
-640 KB
Binary file not shown.
-16 KB
Binary file not shown.
-16 KB
Binary file not shown.
-16 KB
Binary file not shown.

0 commit comments

Comments
 (0)