9 namespace Deveel.Data.Control {
12 const string testDbName =
"testdb";
13 const string testDbAdmin =
"SA";
14 const string testDbPass =
"1234";
20 Config = DbConfig.Default;
21 Config.StorageSystem(ConfigDefaultValues.FileStorageSystem);
24 using (var controller = DbController.Create(Config)) {
25 if (controller.DatabaseExists(testDbName))
26 controller.DeleteDatabase(testDbName, testDbAdmin, testDbPass);
34 using (var controller = DbController.Create(Config)) {
35 Console.Out.WriteLine(
"A new system controller was created at path {0}", controller.Config.BasePath());
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());
43 Assert.IsTrue(dbSystem.Database.Exists,
"The database was created but was not not physically found.");
46 using (var connection = dbSystem.GetConnection(testDbAdmin, testDbPass)) {
47 Assert.IsTrue(connection.State == ConnectionState.Open,
"The connection is not open");
49 using (var transaction = connection.BeginTransaction()) {
51 var command = connection.CreateCommand();
52 command.CommandText =
"CREATE TABLE people (first_name VARCHAR(255), last_name VARCHAR(255), age INT)";
53 command.ExecuteNonQuery();
55 Console.Out.WriteLine(
"The table 'people' was created in the database");
58 command = connection.CreateCommand();
59 command.CommandText =
"INSERT INTO people (first_name, last_name, age) VALUES ('Antonello', 'Provenzano', 33)";
60 command.ExecuteNonQuery();
62 Console.Out.WriteLine(
"An entry was inserted into the table 'people'.");
65 command = connection.CreateCommand();
66 command.CommandText =
"SELECT COUNT(*) FROM people";
68 var count = (BigNumber) command.ExecuteScalar();
69 Assert.AreEqual(1, count.ToInt32(),
"The number of entries in the table is not coherent.");
79 using (var controller = DbController.Create(Config)) {
81 Assert.IsTrue(controller.DatabaseExists(testDbName),
"The database {0} was not physically found at the path {1}", testDbName, Config.BasePath());
85 using (var controller = DbController.Create(Config)) {
88 using (var dbSystem = controller.StartDatabase(testDbName)) {
90 using (var connection = dbSystem.GetConnection(testDbAdmin, testDbPass)) {
92 Assert.IsTrue(connection.State == ConnectionState.Open,
"The connection is not open");
96 var command = connection.CreateCommand();
97 command.CommandText =
"SELECT COUNT(*) FROM people";
100 var count = (BigNumber) command.ExecuteScalar();
101 Assert.AreEqual(1, count.ToInt32(),
"An incorrect number of items was found in the table.");
104 command = connection.CreateCommand();
105 command.CommandText =
"SELECT * FROM people";
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'");
116 Assert.IsTrue(reader.Read(),
"It was not possible to read from the result");
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");