Skip to content

Commit

Permalink
Fix unit tests to 5.0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Feb 21, 2024
1 parent 663f749 commit f23f14c
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 98 deletions.
45 changes: 7 additions & 38 deletions LiteDB.Tests/Database/Upgrade_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LiteDB;
using FluentAssertions;
using Xunit;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;

namespace LiteDB.Tests.Database
{
Expand All @@ -13,80 +14,48 @@ public class Upgrade_Tests
public void Migrage_From_V4()
{
// v5 upgrades only from v4!

var original = "../../../Resources/v4.db";
var copy = original.Replace(".db", "-copy.db");

File.Copy(original, copy, true);

try
using(var tempFile = new TempFile("../../../Resources/v4.db"))
{

using(var db = new LiteDatabase($"filename={copy};upgrade=true"))
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
{
// convert and open database
var col1 = db.GetCollection("col1");

col1.Count().Should().Be(3);
}

using (var db = new LiteDatabase($"filename={copy};upgrade=true"))
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
{
// database already converted
var col1 = db.GetCollection("col1");

col1.Count().Should().Be(3);
}
}
finally
{
File.Delete(copy);

foreach(var backups in Directory.GetFiles(Path.GetDirectoryName(copy), "*-backup*.db"))
{
File.Delete(backups);
}
}
}

[Fact]
public void Migrage_From_V4_No_FileExtension()
{
// v5 upgrades only from v4!

var original = "../../../Resources/v4.db";
var copy = original.Replace(".db", "-copy");

File.Copy(original, copy, true);

try
using (var tempFile = new TempFile("../../../Resources/v4.db"))
{

using (var db = new LiteDatabase($"filename={copy};upgrade=true"))
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
{
// convert and open database
var col1 = db.GetCollection("col1");

col1.Count().Should().Be(3);
}

using (var db = new LiteDatabase($"filename={copy};upgrade=true"))
using (var db = new LiteDatabase($"filename={tempFile};upgrade=true"))
{
// database already converted
var col1 = db.GetCollection("col1");

col1.Count().Should().Be(3);
}
}
finally
{
File.Delete(copy);

foreach (var backups in Directory.GetFiles(Path.GetDirectoryName(copy), "*-backup*"))
{
File.Delete(backups);
}
}
}
}
}
58 changes: 0 additions & 58 deletions LiteDB.Tests/Engine/Rebuild_Loop_Tests.cs

This file was deleted.

95 changes: 95 additions & 0 deletions LiteDB.Tests/Issues/Issue2417_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using FluentAssertions;
using LiteDB.Engine;
using System;
using System.IO;
using System.Linq;

using Xunit;

namespace LiteDB.Tests.Issues
{
public class Issue2417_Tests
{
[Fact]
public void Rebuild_Detected_Infinite_Loop()
{
var original = "../../../Resources/Issue2417_MyData.db";

using (var filename = new TempFile(original))
{
var settings = new EngineSettings
{
Filename = filename,
AutoRebuild = true,
};

try
{
using (var db = new LiteEngine(settings))
{
// infinite loop here
var col = db.Query("customers", Query.All()).ToList();

// never run here
Assert.Fail("not expected");
}
}
catch (Exception ex)
{
Assert.True(ex is LiteException lex && lex.ErrorCode == 999);
}

using (var db = new LiteEngine(settings))
{
var col = db.Query("customers", Query.All()).ToList().Count;
var errors = db.Query("_rebuild_errors", Query.All()).ToList().Count;

col.Should().Be(4);
errors.Should().Be(0);
}
}
}

[Fact]
public void Rebuild_Detected_Infinite_Loop_With_Password()
{
var original = "../../../Resources/Issue2417_TestCacheDb.db";

using (var filename = new TempFile(original))
{
var settings = new EngineSettings
{
Filename = filename,
Password = "bzj2NplCbVH/bB8fxtjEC7u0unYdKHJVSmdmPgArRBwmmGw0+Wd2tE+b2zRMFcHAzoG71YIn/2Nq1EMqa5JKcQ==",
AutoRebuild = true,
};

try
{
using (var db = new LiteEngine(settings))
{
// infinite loop here
var col = db.Query("hubData$AppOperations", Query.All()).ToList();

// never run here
Assert.Fail("not expected");
}
}
catch (Exception ex)
{
Assert.True(ex is LiteException lex && lex.ErrorCode == 999);
}

using (var db = new LiteEngine(settings))
{
var col = db.Query("hubData$AppOperations", Query.All()).ToList().Count;
var errors = db.Query("_rebuild_errors", Query.All()).ToList().Count;

col.Should().Be(408);
errors.Should().Be(0);
}
}
}
}
}

File renamed without changes.
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/Loop-copy
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/Loop-copy-backup
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/Loop-copy-temp
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/Loop-copy-temp-1
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/Loop-copy-temp-1-log
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/Loop-copy-temp-log
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/v4-copy-temp
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/v4-copy-temp-log
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/v4-copy-temp-log.db
Binary file not shown.
Binary file removed LiteDB.Tests/Resources/v4-copy-temp.db
Binary file not shown.
23 changes: 21 additions & 2 deletions LiteDB.Tests/Utils/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,30 @@ public class TempFile : IDisposable
public TempFile()
{
var path = Path.GetTempPath();
var name = "test-" + Guid.NewGuid().ToString("d").Substring(0, 5) + ".db";
var name = "litedb-" + Guid.NewGuid().ToString("d").Substring(0, 5) + ".db";

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

public TempFile(string original)
{
var rnd = "-" + Guid.NewGuid().ToString("d").Substring(0, 5);
var path = Path.GetTempPath();
var name = "litedb-" + Path.GetFileName(FileHelper.GetSuffixFile(original, rnd));
var filename = Path.Combine(path, name);

File.Copy(original, filename, true);

this.Filename = filename;
}

#region Dispose

public static implicit operator String(TempFile value)
{
return value.Filename;
}

private bool _disposed;

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

// check file integrity

File.Delete(this.Filename);
FileHelper.TryExec(5, () => File.Delete(this.Filename));

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

public string ReadAsText() => File.ReadAllText(this.Filename);

public override string ToString() => this.Filename;
}
}

0 comments on commit f23f14c

Please sign in to comment.