Beispiel #1
0
        public void GetColumnNamesTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT boolTrue FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                var columnNames = cursor.GetColumnNames();
                Assert.AreEqual(1, columnNames.Count);
                Assert.AreEqual("boolTrue", columnNames[0]);
            }

            var expectedColumnNames = new List <string>
            {
                "boolTrue",
                "int",
                "string"
            };

            var sql = $"SELECT {expectedColumnNames[0]}, {expectedColumnNames[1]}, {expectedColumnNames[2]} FROM {TableName};";

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery(sql))
            {
                Assert.IsFalse(cursor.IsEmpty);
                var returnedColumnNames = cursor.GetColumnNames();
                Assert.AreEqual(expectedColumnNames.Count, returnedColumnNames.Count);

                foreach (var returnedColumnName in returnedColumnNames)
                {
                    Assert.IsTrue(expectedColumnNames.Contains(returnedColumnName));
                }
            }
        }
Beispiel #2
0
        public void GetValueTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT boolNullable FROM {TableName} WHERE boolNullable IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                try
                {
                    // ReSharper disable once UnusedVariable
                    var boolean = Convert.ToBoolean(cursor.GetValue(cursor.GetFirstFieldIndex()));
                }
                catch (Exception)
                {
                    Assert.Fail();
                }
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT string FROM {TableName} WHERE string IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                try
                {
                    // ReSharper disable once UnusedVariable
                    var s = cursor.GetString(cursor.GetFirstFieldIndex());
                }
                catch (Exception)
                {
                    Assert.Fail();
                }
            }
        }
Beispiel #3
0
        public void MoveToNextRow()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT boolNullable FROM {TableName} WHERE boolNullable IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsTrue(cursor.IsFirstRow());
                Assert.IsFalse(cursor.MoveToNextRow());
            }

            using (var cursor = DatabaseWrapper.ExecuteQuery($"SELECT boolNullable FROM {TableName} WHERE boolNullable IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsTrue(cursor.IsFirstRow());

                var lastRowPosition = cursor.GetRowPosition();
                while (cursor.MoveToNextRow())
                {
                    var currentRowPosition = cursor.GetRowPosition();
                    Assert.AreEqual(lastRowPosition + 1, currentRowPosition);

                    lastRowPosition = currentRowPosition;
                }

                cursor.MoveToNextRow();
                Assert.AreEqual(lastRowPosition, cursor.GetRowPosition());
            }
        }
Beispiel #4
0
        public void ExecuteQueryWithParametersTest()
        {
            var tableName  = "testingTable" + HelperObjectFactory.GetRandomInt(0);
            var parameters = new List <SQLiteParameter>();
            var aParameter = SqliteExtensions.Create("a", DbType.String, "string1");
            var bParameter = SqliteExtensions.Create("b", DbType.Int32, 32);

            parameters.Add(aParameter);
            parameters.Add(bParameter);

            DatabaseWrapper.ExecuteSql($"CREATE TABLE {tableName}(a, b);");
            using (var c = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT a FROM {tableName} WHERE a = 'nothing';"))
            {
                Assert.IsTrue(c.IsEmpty);
            }

            DatabaseWrapper.ExecuteSqlWithParameters($"INSERT INTO {tableName}(a, b) VALUES ({aParameter.ParameterName}, {bParameter.ParameterName});", parameters);
            using (var c = DatabaseWrapper.ExecuteQueryWithParameters($"SELECT a FROM {tableName} WHERE a = {aParameter.ParameterName};", parameters))
            {
                Assert.AreEqual(c.GetString(0), "string1");
            }

            aParameter = SqliteExtensions.Create("a", DbType.String, "string2");
            bParameter = SqliteExtensions.Create("b", DbType.Int32, 64);
            DatabaseWrapper.ExecuteSqlWithParameters($"INSERT INTO {tableName}(a, b) VALUES ({aParameter.ParameterName}, {bParameter.ParameterName});", parameters);
            using (var c = DatabaseWrapper.ExecuteQueryWithParameters($"SELECT b FROM {tableName} WHERE b = {bParameter.ParameterName};", parameters))
            {
                Assert.AreEqual(32, c.GetInt(0));
            }

            DatabaseWrapper.ExecuteSql($"DROP TABLE {tableName};");
        }
Beispiel #5
0
        public void GetBlobTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT blob FROM {TableName} WHERE blob NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                var blob = cursor.GetBlob(cursor.GetFirstFieldIndex());

                Assert.IsNotNull(blob);
                Assert.AreEqual(TestBlobSize, blob.Length);
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT blob FROM {TableName} WHERE blob NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                var blob = new byte[TestBlobSize];
                cursor.GetBlob(cursor.GetFirstFieldIndex(), 0, ref blob, 0, TestBlobSize);

                Assert.IsNotNull(blob);
                Assert.AreEqual(TestBlobSize, blob.Length);
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT blob FROM {TableName} WHERE blob NOT NULL;"))
            {
                using (var memoryStream = cursor.GetBlobMemoryStream(cursor.GetFirstFieldIndex()))
                {
                    Assert.IsFalse(cursor.IsEmpty);
                    var blob = memoryStream.ToArray();

                    Assert.IsNotNull(blob);
                    Assert.AreEqual(TestBlobSize, blob.Length);
                }
            }
        }
Beispiel #6
0
 public void GetFirstFieldIndexTest()
 {
     using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT * FROM {TableName};"))
     {
         Assert.AreEqual(0, cursor.GetFirstFieldIndex());
     }
 }
Beispiel #7
0
        public void TransactionsTest()
        {
            DatabaseWrapper.BeginTransaction();
            var tableName = "testingTable" + HelperObjectFactory.GetRandomInt(0);

            DatabaseWrapper.ExecuteSql($"CREATE TABLE {tableName}(a,b);");
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test0', 'test1');");
            DatabaseWrapper.CommitTransaction();

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT * FROM {tableName};"))
            {
                Assert.IsTrue(DatabaseWrapper.HasOneRecordAndClose(cursor));
            }

            DatabaseWrapper.BeginTransaction();
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test20', 'test21');");
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test20', 'test21');");
            DatabaseWrapper.RollBackTransaction();

            Assert.AreEqual(DatabaseWrapper.ExecuteCountQuery($"SELECT COUNT(a) FROM {tableName};"), 1);

            DatabaseWrapper.BeginTransaction();
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test20', 'test21');");
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test20', 'test21');");
            DatabaseWrapper.CommitTransaction();

            Assert.AreEqual(DatabaseWrapper.ExecuteCountQuery($"SELECT COUNT(a) FROM {tableName};"), 3);
        }
Beispiel #8
0
 public void GetStringTest()
 {
     using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT string FROM {TableName} WHERE string IS NOT NULL;"))
     {
         Assert.IsFalse(cursor.IsEmpty);
         cursor.GetString(cursor.GetFirstFieldIndex());
     }
 }
Beispiel #9
0
 public void IsClosed()
 {
     using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT string FROM {TableName} WHERE string IS NOT NULL;"))
     {
         Assert.IsFalse(cursor.IsClosed());
         cursor.Close();
         Assert.IsTrue(cursor.IsClosed());
     }
 }
Beispiel #10
0
 public void GetDateTimeFromUtcTicksToLocalTimeTest()
 {
     using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT dateTime FROM {TableName} WHERE dateTime IS NOT NULL;"))
     {
         Assert.IsFalse(cursor.IsEmpty);
         Assert.IsNotNull(cursor.GetDateTimeFromUtcTicksToLocalTime(cursor.GetFirstFieldIndex()));
         Assert.AreEqual(_now.ToLocalTime(), cursor.GetDateTimeFromUtcTicksToLocalTime(cursor.GetFirstFieldIndex()));
     }
 }
Beispiel #11
0
        public void IsEmptyTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT string FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT string FROM {TableName} WHERE string = 'notExistingValue';"))
            {
                Assert.IsTrue(cursor.IsEmpty);
            }
        }
Beispiel #12
0
 public void GetBlobMemoryStreamTest()
 {
     using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT blob FROM {TableName} WHERE blob IS NOT NULL;"))
     {
         Assert.IsFalse(cursor.IsEmpty);
         using (var memoryStream = cursor.GetBlobMemoryStream(cursor.GetFirstFieldIndex()))
         {
             Assert.IsNotNull(memoryStream);
             Assert.AreEqual(TestBlobSize, memoryStream.Length);
         }
     }
 }
Beispiel #13
0
        public void OpenCloseTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT int FROM {TableName} WHERE int IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsFalse(cursor.IsClosed());

                cursor.Close();
                Assert.IsTrue(cursor.IsClosed());

                cursor.Close();
                Assert.IsTrue(cursor.IsClosed());
            }
        }
Beispiel #14
0
        public void GetColumnIndexTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT boolFalse FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.AreEqual(cursor.GetFirstFieldIndex(), cursor.GetColumnIndex("boolFalse"));
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT boolTrue FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.AreEqual(-1, cursor.GetColumnIndex("boo"));
            }
        }
Beispiel #15
0
        public void IsNullTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT string FROM {TableName} WHERE string IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsFalse(cursor.IsNull(cursor.GetFirstFieldIndex()));
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT nullColumn FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsTrue(cursor.IsNull(cursor.GetFirstFieldIndex()));
            }
        }
Beispiel #16
0
        public void GetNullableShortTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT short FROM {TableName} WHERE short IS NOT NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsNotNull(cursor.GetNullableShort(cursor.GetFirstFieldIndex()));
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT short FROM {TableName} WHERE short IS NULL;"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.IsNull(cursor.GetNullableShort(cursor.GetFirstFieldIndex()));
            }
        }
Beispiel #17
0
 public void DisposeTest()
 {
     try
     {
         using (var c = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT * FROM {TableName};"))
         {
             Assert.IsNotNull(c);
             c.Dispose();
         }
     }
     catch (Exception e)
     {
         Assert.Fail($"Should not have thrown exception: {e}");
     }
 }
Beispiel #18
0
        public void ExecuteSingleResultQueryTest()
        {
            var tableName = "testingTable" + HelperObjectFactory.GetRandomInt(0);

            DatabaseWrapper.ExecuteSql($"CREATE TABLE {tableName}(a,b);");
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test0', 'test1');");
            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test6', 'test3');");

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT * FROM {tableName};"))
            {
                Assert.IsNotNull(cursor);
                Assert.IsFalse(cursor.MoveToNextRow());
            }

            DatabaseWrapper.ExecuteSql($"DROP TABLE {tableName};");
        }
Beispiel #19
0
        public void GetRowValues()
        {
            var expectedColumnNames = new List <string>
            {
                "boolTrue",
                "int",
                "string"
            };

            var sql = $"SELECT {expectedColumnNames[0]}, {expectedColumnNames[1]}, {expectedColumnNames[2]} FROM {TableName};";

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery(sql))
            {
                Assert.IsFalse(cursor.IsEmpty);
                var values = cursor.GetRowValues();
                Assert.AreEqual(expectedColumnNames.Count, values.Count);
            }
        }
Beispiel #20
0
        public void GetColumnCountTest()
        {
            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT boolTrue FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.AreEqual(1, cursor.GetColumnCount());
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT int, boolTrue FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.AreEqual(2, cursor.GetColumnCount());
            }

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT int, short, long, string, boolFalse FROM {TableName};"))
            {
                Assert.IsFalse(cursor.IsEmpty);
                Assert.AreEqual(5, cursor.GetColumnCount());
            }
        }
Beispiel #21
0
        public void HasOneRecordAndCloseTest()
        {
            var tableName = "testingTable" + HelperObjectFactory.GetRandomInt(0);

            DatabaseWrapper.ExecuteSql($"CREATE TABLE {tableName}(a,b);");

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT * FROM {tableName};"))
            {
                Assert.IsFalse(DatabaseWrapper.HasOneRecordAndClose(cursor));
                Assert.IsTrue(cursor.IsClosed());
            }

            DatabaseWrapper.ExecuteSql($"INSERT INTO {tableName}(a,b) VALUES ('test0', 'test1');");

            using (var cursor = DatabaseWrapper.ExecuteSingleResultQuery($"SELECT * FROM {tableName};"))
            {
                Assert.IsTrue(DatabaseWrapper.HasOneRecordAndClose(cursor));
                Assert.IsTrue(cursor.IsClosed());
            }

            DatabaseWrapper.ExecuteSql($"DROP TABLE {tableName};");
        }