DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
PersistenceTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Data;
3 
5 using Deveel.Diagnostics;
6 
7 using NUnit.Framework;
8 
9 namespace Deveel.Data.Control {
10  [TestFixture]
11  public class PersistenceTests {
12  const string testDbName = "testdb";
13  const string testDbAdmin = "SA";
14  const string testDbPass = "1234";
15 
16  private IDbConfig Config;
17 
18  [SetUp]
19  public void SetUp() {
20  Config = DbConfig.Default;
21  Config.StorageSystem(ConfigDefaultValues.FileStorageSystem);
22  Config.LoggerType(typeof (ConsoleLogger));
23 
24  using (var controller = DbController.Create(Config)) {
25  if (controller.DatabaseExists(testDbName))
26  controller.DeleteDatabase(testDbName, testDbAdmin, testDbPass);
27  }
28  }
29 
30  [Test]
31  [Category("Create")]
32  public void FullCycle() {
33  // First create the controller for the system context
34  using (var controller = DbController.Create(Config)) {
35  Console.Out.WriteLine("A new system controller was created at path {0}", controller.Config.BasePath());
36 
37  // Then create the database 'testdb'
38  using (var dbSystem = controller.CreateDatabase(testDbName, testDbAdmin, testDbPass)) {
39  Console.Out.WriteLine("The database {0} was created at the path {1}",
40  dbSystem.Config.DatabaseName(),
41  dbSystem.Config.DatabaseFullPath());
42 
43  Assert.IsTrue(dbSystem.Database.Exists, "The database was created but was not not physically found.");
44 
45  // now open am ADO.NET connection to write data to the database
46  using (var connection = dbSystem.GetConnection(testDbAdmin, testDbPass)) {
47  Assert.IsTrue(connection.State == ConnectionState.Open, "The connection is not open");
48 
49  using (var transaction = connection.BeginTransaction()) {
50  // Create the table 'people'
51  var command = connection.CreateCommand();
52  command.CommandText = "CREATE TABLE people (first_name VARCHAR(255), last_name VARCHAR(255), age INT)";
53  command.ExecuteNonQuery();
54 
55  Console.Out.WriteLine("The table 'people' was created in the database");
56 
57  // insert an entry into the table
58  command = connection.CreateCommand();
59  command.CommandText = "INSERT INTO people (first_name, last_name, age) VALUES ('Antonello', 'Provenzano', 33)";
60  command.ExecuteNonQuery();
61 
62  Console.Out.WriteLine("An entry was inserted into the table 'people'.");
63 
64  // assert the entry exists within this context
65  command = connection.CreateCommand();
66  command.CommandText = "SELECT COUNT(*) FROM people";
67 
68  var count = (BigNumber) command.ExecuteScalar();
69  Assert.AreEqual(1, count.ToInt32(), "The number of entries in the table is not coherent.");
70 
71  transaction.Commit();
72  }
73  }
74  }
75  }
76 
77  // The previous system context was disposed, any reference to the databases within the
78  // context has been released. So create another system controller
79  using (var controller = DbController.Create(Config)) {
80  // Check the database physically exists in the system
81  Assert.IsTrue(controller.DatabaseExists(testDbName), "The database {0} was not physically found at the path {1}", testDbName, Config.BasePath());
82  }
83 
84  // Open another system context that is isolated from the previous ones.
85  using (var controller = DbController.Create(Config)) {
86  // Open an existing database within the system context. If the database doesn't exist
87  // this will throw an exception.
88  using (var dbSystem = controller.StartDatabase(testDbName)) {
89  // Open a connection to the database
90  using (var connection = dbSystem.GetConnection(testDbAdmin, testDbPass)) {
91  // Assert the connection state is open
92  Assert.IsTrue(connection.State == ConnectionState.Open, "The connection is not open");
93 
94  // Check the 'people' table and count the items. If the table doesn't physically
95  // exist this will throw an exception
96  var command = connection.CreateCommand();
97  command.CommandText = "SELECT COUNT(*) FROM people";
98 
99  // Assert there is exactly one element in the table
100  var count = (BigNumber) command.ExecuteScalar();
101  Assert.AreEqual(1, count.ToInt32(), "An incorrect number of items was found in the table.");
102 
103  // Now select the entry in the table
104  command = connection.CreateCommand();
105  command.CommandText = "SELECT * FROM people";
106 
107  // Assert the data structure is coherent with the one created in
108  // the previous passage
109  var reader = command.ExecuteReader();
110  Assert.AreEqual(3, reader.FieldCount, "An incorrect number of fields was found in the table.");
111  Assert.AreEqual(0, reader.GetOrdinal("first_name"), "The first field in the table is not 'first_name'");
112  Assert.AreEqual(1, reader.GetOrdinal("last_name"), "The second field in the table is not 'last_name'");
113  Assert.AreEqual(2, reader.GetOrdinal("age"), "The third field in the table is not 'age'");
114 
115  // Assert at least one entry can be read
116  Assert.IsTrue(reader.Read(), "It was not possible to read from the result");
117 
118  // Assert the entry read is exactly the one created in the previous stage
119  Assert.AreEqual("Antonello", reader.GetString(0), "The value of 'first_name' is not 'Antonello'");
120  Assert.AreEqual("Provenzano", reader.GetString(1), "The value of 'last_name' is not 'Provenzano'");
121  Assert.AreEqual(33, reader.GetInt32(2), "The value of 'age' is not 33");
122  }
123  }
124  }
125  }
126  }
127 }