-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTest.cs
74 lines (58 loc) · 2.59 KB
/
CreateTest.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
using LinqToDB;
using LinqToDB.Data;
using Microsoft.Data.Sqlite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
namespace linq2dbsqliteschemaissue {
[TestClass]
public class CreateTest {
[ClassInitialize]
public static void ClassInitialize(TestContext context) {
//always use a new clean database in the TestDir..
DataConnection.DefaultSettings = new MockDBSettings(context.TestDir);
}
[TestMethod]
public void CreateTableMS() {
CreateTable("LocalStorage.MS");
}
[TestMethod]
public void CreateTableClassic() {
CreateTable("LocalStorage.Classic");
}
private void CreateTable(string connectionName) {
using (var db = new LocalDB(connectionName)) {
//Fetching the schema
var sp = db.DataProvider.GetSchemaProvider();
var dbSchema = sp.GetSchema(db);
//checing if a table with name TestTable exist
if (!dbSchema.Tables.Any(t => t.TableName == "TestTable")) {
//create a table named testtable
db.CreateTable<TestTable>();
}
//get the newly create table via sqlite_schema
var dr = db.ExecuteReader("SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%'; ");
var list = new List<string>();
while (dr.Reader is not null && dr.Reader.Read()) {
list.Add(dr.Reader.GetString(0));
}
//bail out if the table does not exist
Assert.IsTrue(list.Any(t => t == "TestTable"), "DB should contain a TestTable");
//Here we insert and read to be really sure the table exists
//insert data in testtable
db.Insert(new TestTable() { Name = "Test" });
//read data from testtable
Assert.IsTrue(db.Messages.Any(), "Table should contain data");
}
//Now we are really really sure the new table exists
//Just to be sure.. we start a new instance
using (var db = new LocalDB(connectionName)) {
//Get the schema
var sp = db.DataProvider.GetSchemaProvider();
var dbSchema = sp.GetSchema(db);
//The schema is not updated!!!
Assert.IsTrue(dbSchema.Tables.Any(t => t.TableName == "TestTable"), "Schema should contain TestTable");
}
}
}
}