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

+7-38
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

-58
This file was deleted.
+95
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+

LiteDB.Tests/Resources/Loop-copy

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

LiteDB.Tests/Resources/Loop-copy-temp

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

LiteDB.Tests/Resources/v4-copy-temp

-8 KB
Binary file not shown.
-8 KB
Binary file not shown.
-8 KB
Binary file not shown.
-8 KB
Binary file not shown.

LiteDB.Tests/Utils/TempFile.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,30 @@ public class TempFile : IDisposable
1010
public TempFile()
1111
{
1212
var path = Path.GetTempPath();
13-
var name = "test-" + Guid.NewGuid().ToString("d").Substring(0, 5) + ".db";
13+
var name = "litedb-" + Guid.NewGuid().ToString("d").Substring(0, 5) + ".db";
1414

1515
this.Filename = Path.Combine(path, name);
1616
}
1717

18+
public TempFile(string original)
19+
{
20+
var rnd = "-" + Guid.NewGuid().ToString("d").Substring(0, 5);
21+
var path = Path.GetTempPath();
22+
var name = "litedb-" + Path.GetFileName(FileHelper.GetSuffixFile(original, rnd));
23+
var filename = Path.Combine(path, name);
24+
25+
File.Copy(original, filename, true);
26+
27+
this.Filename = filename;
28+
}
29+
1830
#region Dispose
1931

32+
public static implicit operator String(TempFile value)
33+
{
34+
return value.Filename;
35+
}
36+
2037
private bool _disposed;
2138

2239
public void Dispose()
@@ -43,7 +60,7 @@ protected virtual void Dispose(bool disposing)
4360

4461
// check file integrity
4562

46-
File.Delete(this.Filename);
63+
FileHelper.TryExec(5, () => File.Delete(this.Filename));
4764

4865
_disposed = true;
4966
}
@@ -53,5 +70,7 @@ protected virtual void Dispose(bool disposing)
5370
public long Size => new FileInfo(this.Filename).Length;
5471

5572
public string ReadAsText() => File.ReadAllText(this.Filename);
73+
74+
public override string ToString() => this.Filename;
5675
}
5776
}

0 commit comments

Comments
 (0)