Skip to content

Commit abb1d5c

Browse files
committed
InvalidFile_Tests
1 parent 409f6d5 commit abb1d5c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Collections.Generic;
5+
using FluentAssertions;
6+
using Xunit;
7+
8+
namespace LiteDB.Tests.Database
9+
{
10+
public class InvalidFile_Tests
11+
{
12+
[Fact]
13+
public void Test_AddDatabase_InvalidDatabase()
14+
{
15+
// Set the database name and file name
16+
string dbName = "invalidDb";
17+
string fileName = $"{dbName}.db";
18+
19+
// Create an invalid LiteDB database file for testing
20+
File.WriteAllText(fileName, "Invalid content");
21+
22+
// Verify the file exists and content is correct
23+
Assert.True(File.Exists(fileName));
24+
Assert.Equal("Invalid content", File.ReadAllText(fileName));
25+
26+
// Act & Assert: Try to open the invalid database and expect an exception
27+
Assert.Throws<LiteException>(() =>
28+
{
29+
using (var db = new LiteDatabase(fileName))
30+
{
31+
// Attempt to perform an operation to ensure the database file is read
32+
var col = db.GetCollection("test");
33+
col.Insert(new BsonDocument { ["name"] = "test" });
34+
}
35+
});
36+
37+
// Clean up: Remove the invalid database file after the test
38+
if (File.Exists(fileName))
39+
{
40+
File.Delete(fileName);
41+
}
42+
}
43+
44+
[Fact]
45+
public void Test_AddDatabase_InvalidDatabase_LargeFile()
46+
{
47+
// Set the database name and file name
48+
string dbName = "largeInvalidDb";
49+
string fileName = $"{dbName}.db";
50+
51+
// Create an invalid LiteDB database file with content larger than 16KB for testing
52+
string invalidContent = new string('a', 16 * 1024 + 1);
53+
File.WriteAllText(fileName, invalidContent);
54+
55+
// Verify the file exists and content is correct
56+
Assert.True(File.Exists(fileName));
57+
Assert.Equal(invalidContent, File.ReadAllText(fileName));
58+
59+
// Act & Assert: Try to open the invalid database and expect an exception
60+
Assert.Throws<LiteException>(() =>
61+
{
62+
using (var db = new LiteDatabase(fileName))
63+
{
64+
// Attempt to perform an operation to ensure the database file is read
65+
var col = db.GetCollection("test");
66+
col.Insert(new BsonDocument { ["name"] = "test" });
67+
}
68+
});
69+
70+
// Clean up: Remove the invalid database file after the test
71+
if (File.Exists(fileName))
72+
{
73+
File.Delete(fileName);
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)