예제 #1
0
        public void SetUp()
        {
            SQLite.SQLiteConnectionPool.Shared.Reset();

            _connectionString = TestPath.GetTempFileName();
            _path             = _connectionString;
            System.IO.File.Delete(_path);
        }
예제 #2
0
        public IEnumerator UnicodePathsAsync() => UniTask.ToCoroutine(async() =>
        {
            var path = TestPath.GetTempFileName() + UnicodeText;

            var db = new SQLiteAsyncConnection(path, true);
            await db.CreateTableAsync <OrderLine>();

            Assert.IsTrue(new FileInfo(path).Length > 0, path);
        });
예제 #3
0
            private static string GetTempFileName()
            {
#if NETFX_CORE
                var name = Guid.NewGuid() + ".sqlite";
                return(Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, name));
#else
                return(TestPath.GetTempFileName());
#endif
            }
예제 #4
0
        public void UnicodePaths()
        {
            var path = TestPath.GetTempFileName() + UnicodeText;

            using (var db = new SQLiteConnection(path, true)) {
                db.CreateTable <OrderLine> ();
            }

            Assert.IsTrue(new FileInfo(path).Length > 0, path);
        }
예제 #5
0
        public void ByteArrays()
        {
            //Byte Arrays for comparisson
            ByteArrayClass[] byteArrays = new ByteArrayClass[] {
                new ByteArrayClass()
                {
                    bytes = new byte[] { 1, 2, 3, 4, 250, 252, 253, 254, 255 }
                },                                                                                                   //Range check
                new ByteArrayClass()
                {
                    bytes = new byte[] { 0 }
                },                                                                 //null bytes need to be handled correctly
                new ByteArrayClass()
                {
                    bytes = new byte[] { 0, 0 }
                },
                new ByteArrayClass()
                {
                    bytes = new byte[] { 0, 1, 0 }
                },
                new ByteArrayClass()
                {
                    bytes = new byte[] { 1, 0, 1 }
                },
                new ByteArrayClass()
                {
                    bytes = new byte[] { }
                },                                                               //Empty byte array should stay empty (and not become null)
                new ByteArrayClass()
                {
                    bytes = null
                }                                                     //Null should be supported
            };

            SQLiteConnection database = new SQLiteConnection(TestPath.GetTempFileName());

            database.CreateTable <ByteArrayClass>();

            //Insert all of the ByteArrayClass
            foreach (ByteArrayClass b in byteArrays)
            {
                database.Insert(b);
            }

            //Get them back out
            ByteArrayClass[] fetchedByteArrays = database.Table <ByteArrayClass>().OrderBy(x => x.ID).ToArray();

            Assert.AreEqual(fetchedByteArrays.Length, byteArrays.Length);
            //Check they are the same
            for (int i = 0; i < byteArrays.Length; i++)
            {
                byteArrays[i].AssertEquals(fetchedByteArrays[i]);
            }
        }
예제 #6
0
        public void QueryGenericObject()
        {
            var path = TestPath.GetTempFileName();
            var db   = new SQLiteConnection(path, true);

            db.Execute("create table G(Value integer not null)");
            db.Execute("insert into G(Value) values (?)", 42);
            var r = db.Query <GenericObject> ("select * from G");

            Assert.AreEqual(1, r.Count);
            Assert.AreEqual(42, r[0].Value);
        }
예제 #7
0
        public void SetPreKeyAction()
        {
            var path = TestPath.GetTempFileName();
            var key  = "SecretKey";

            using (var db = new SQLiteConnection(path, true, key,
                                                 preKeyAction:  conn => conn.Execute("PRAGMA cipher_default_use_hmac = OFF;"))) {
                db.CreateTable <TestTable> ();
                db.Insert(new TestTable {
                    Value = "Secret Value"
                });
                Assert.AreEqual("0", db.ExecuteScalar <string> ("PRAGMA cipher_default_use_hmac;"));
            }
        }
예제 #8
0
        public void SetPostKeyAction()
        {
            var path = TestPath.GetTempFileName();
            var key  = "SecretKey";

            using (var db = new SQLiteConnection(new SQLiteConnectionString(path, true, key,
                                                                            postKeyAction: conn => conn.Execute("PRAGMA page_size = 512;")))) {
                db.CreateTable <TestTable> ();
                db.Insert(new TestTable {
                    Value = "Secret Value"
                });
                Assert.AreEqual("512", db.ExecuteScalar <string> ("PRAGMA page_size;"));
            }
        }
예제 #9
0
        public void Cipher()
        {
            var databasePath = TestPath.GetTempFileName();

            File.Delete(databasePath);

            var options     = new SQLiteConnectionString(databasePath, true, key: "password");
            var encryptedDb = new SQLiteAsyncConnection(options);

            var options2 = new SQLiteConnectionString(databasePath, true,
                                                      key: "password",
                                                      preKeyAction: db => db.Execute("PRAGMA cipher_default_use_hmac = OFF;"),
                                                      postKeyAction: db => db.Execute("PRAGMA kdf_iter = 128000;"));
            var encryptedDb2 = new SQLiteAsyncConnection(options2);
        }
예제 #10
0
        public IEnumerator AsyncWithWalCloses() => UniTask.ToCoroutine(async() =>
        {
            var path = TestPath.GetTempFileName();

            var db = new SQLiteAsyncConnection(path);
            await db.CreateTableAsync <OrderLine>();
            await db.InsertAsync(new OrderLine {
            });
            var lines = await db.Table <OrderLine>().ToListAsync();

            Assert.AreEqual(1, lines.Count);

            await db.CloseAsync();

            File.Delete(path);
        });
예제 #11
0
        public IEnumerator BackupOneTable() => UniTask.ToCoroutine(async() =>
        {
            var pathSrc  = TestPath.GetTempFileName();
            var pathDest = TestPath.GetTempFileName();

            var db = new SQLiteAsyncConnection(pathSrc);
            await db.CreateTableAsync <OrderLine>();
            await db.InsertAsync(new OrderLine {
            });
            var lines = await db.Table <OrderLine>().ToListAsync();
            Assert.AreEqual(1, lines.Count);

            await db.BackupAsync(pathDest);

            var destLen = new FileInfo(pathDest).Length;
            Assert.True(destLen >= 4096);
        });
예제 #12
0
        public void Issue604_RunInTransactionAsync()
        {
            var adb = new SQLiteAsyncConnection(TestPath.GetTempFileName());

            adb.CreateTableAsync <TestObj> ().Wait();
            var initialCount = adb.Table <TestObj> ().CountAsync().Result;

            //
            // Fail a commit
            //
            adb.Trace  = true;
            adb.Tracer = m => {
                //Console.WriteLine (m);
                if (m.Trim().EndsWith("commit"))
                {
                    throw SQLiteException.New(SQLite3.Result.Busy, "Make commit fail");
                }
            };

            try {
                adb.RunInTransactionAsync(db => {
                    db.Insert(new TestObj());
                }).Wait();
                Assert.Fail("Should have thrown");
            }
            catch (AggregateException aex)
                when(aex.InnerException is SQLiteException ex &&
                     ex.Result == SQLite3.Result.Busy)
                {
                    // Expected
                }

            //
            // Are we stuck?
            //
            adb.Tracer = null;
            adb.RunInTransactionAsync(db => {
                db.Insert(new TestObj());
            }).Wait();

            Assert.AreEqual(initialCount + 1, adb.Table <TestObj> ().CountAsync().Result);
        }
예제 #13
0
        public IEnumerator Asynchronous() => UniTask.ToCoroutine(async() =>
        {
            await UniTask.Delay(1);

            // Get an absolute path to the database file
            var databasePath = TestPath.GetTempFileName();
            File.Delete(databasePath);

            var db = new SQLiteAsyncConnection(databasePath);

            await db.CreateTableAsync <Stock>();

            Console.WriteLine("Table created!");

            var stock = new Stock()
            {
                Symbol = "AAPL"
            };

            await db.InsertAsync(stock);

            Console.WriteLine("New sti ID: {0}", stock.Id);

            var query = db.Table <Stock>().Where(s => s.Symbol.StartsWith("A"));

            var result = await query.ToListAsync();

            foreach (var s in result)
            {
                Console.WriteLine("Stock: " + s.Symbol);
            }

            Assert.AreEqual(1, result.Count);

            var count = await db.ExecuteScalarAsync <int>("select count(*) from Stock");

            Console.WriteLine(string.Format("Found '{0}' stock items.", count));

            Assert.AreEqual(1, count);
        });
예제 #14
0
        public IEnumerator Issue329_AsyncTransactionFailuresShouldRollback() => UniTask.ToCoroutine(async() =>
        {
            var adb = new SQLiteAsyncConnection(TestPath.GetTempFileName());
            await adb.CreateTableAsync <TestObj>();
            var initialCount = await adb.Table <TestObj>().CountAsync();
            var rollbacks    = 0;

            //
            // Fail a commit
            //
            adb.Trace  = true;
            adb.Tracer = m =>
            {
                Console.WriteLine(m);
                if (m == "Executing: rollback")
                {
                    rollbacks++;
                }
            };

            LogAssert.Expect(LogType.Exception, "Exception: User exception");
            try
            {
                await adb.RunInTransactionAsync(db =>
                {
                    db.Insert(new TestObj());
                    throw new Exception("User exception");
                });
                Assert.Fail("Should have thrown");
            }
            catch (AggregateException aex)
                when(aex.InnerException.Message == "User exception")
                {
                    // Expected
                }
            LogAssert.NoUnexpectedReceived();

            Assert.AreEqual(1, rollbacks);
        });
예제 #15
0
        public void NullableEnum()
        {
            SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName());

            db.CreateTable <NullableEnumClass> ();

            var withNull = new NullableEnumClass {
                NullableIntEnum = null, NullableTextEnum = null
            };
            var with1 = new NullableEnumClass {
                NullableIntEnum = TestIntEnum.One, NullableTextEnum = null
            };
            var with2 = new NullableEnumClass {
                NullableIntEnum = TestIntEnum.Two, NullableTextEnum = null
            };
            var withNullA = new NullableEnumClass {
                NullableIntEnum = null, NullableTextEnum = TestTextEnum.Alpha
            };
            var with1B = new NullableEnumClass {
                NullableIntEnum = TestIntEnum.One, NullableTextEnum = TestTextEnum.Beta
            };

            db.Insert(withNull);
            db.Insert(with1);
            db.Insert(with2);
            db.Insert(withNullA);
            db.Insert(with1B);

            var results = db.Table <NullableEnumClass> ().OrderBy(x => x.ID).ToArray();

            Assert.AreEqual(5, results.Length);

            Assert.AreEqual(withNull, results[0]);
            Assert.AreEqual(with1, results[1]);
            Assert.AreEqual(with2, results[2]);
            Assert.AreEqual(withNullA, results[3]);
            Assert.AreEqual(with1B, results[4]);
        }
예제 #16
0
        public void NullableInt()
        {
            SQLiteConnection db = new SQLiteConnection(TestPath.GetTempFileName());

            db.CreateTable <NullableIntClass>();

            NullableIntClass withNull = new NullableIntClass()
            {
                NullableInt = null
            };
            NullableIntClass with0 = new NullableIntClass()
            {
                NullableInt = 0
            };
            NullableIntClass with1 = new NullableIntClass()
            {
                NullableInt = 1
            };
            NullableIntClass withMinus1 = new NullableIntClass()
            {
                NullableInt = -1
            };

            db.Insert(withNull);
            db.Insert(with0);
            db.Insert(with1);
            db.Insert(withMinus1);

            NullableIntClass[] results = db.Table <NullableIntClass>().OrderBy(x => x.ID).ToArray();

            Assert.AreEqual(4, results.Length);

            Assert.AreEqual(withNull, results[0]);
            Assert.AreEqual(with0, results[1]);
            Assert.AreEqual(with1, results[2]);
            Assert.AreEqual(withMinus1, results[3]);
        }
예제 #17
0
        public void AsyncAsTicks()
        {
            var db = new SQLiteAsyncConnection(TestPath.GetTempFileName());

            TestAsyncDateTimeOffset(db);
        }
예제 #18
0
 public TestDb(bool storeDateTimeAsTicks = true) : base(TestPath.GetTempFileName(), storeDateTimeAsTicks)
 {
     Trace = true;
 }
예제 #19
0
 public void Setup()
 {
     _db = new TestDb(TestPath.GetTempFileName());
 }
예제 #20
0
        public void AsyncAsTicks()
        {
            var db = new SQLiteConnectionAsync(TestPath.GetTempFileName(), true);

            TestAsyncDateTime(db);
        }
예제 #21
0
        public void AsyncAsString()
        {
            var db = new SQLiteAsyncConnection(TestPath.GetTempFileName(), false);

            TestAsyncDateTime(db);
        }
예제 #22
0
 public void Setup()
 {
     _db = new TestDb(TestPath.GetTempFileName());
     _db.SetForeignKeysPermissions(true);
 }
예제 #23
0
 SQLiteConnectionString CustomDateTimeString(string dateTimeFormat) => new SQLiteConnectionString(TestPath.GetTempFileName(), false, dateTimeFormat);
예제 #24
0
 SQLiteConnectionString TimeSpanAsTicks(bool asTicks = true) => new SQLiteConnectionString(TestPath.GetTempFileName(), SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite, true, storeTimeSpanAsTicks: asTicks);
예제 #25
0
 public void Setup()
 {
     this.database = new GetTestWithOne2OneRelationshipDb(TestPath.GetTempFileName());
     this.database.SetForeignKeysPermissions(true);
 }
예제 #26
0
 SQLiteConnectionString CustomDateTimeString(string dateTimeFormat) => new SQLiteConnectionString(TestPath.GetTempFileName(), SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite, false, dateTimeStringFormat: dateTimeFormat);
예제 #27
0
 public TestDb(bool storeDateTimeAsTicks = true, object key = null, bool wal = true) : base(new SQLiteConnectionString(TestPath.GetTempFileName(), storeDateTimeAsTicks, key: key))
 {
     Trace = true;
     if (wal)
     {
         EnableWriteAheadLogging();
     }
 }
예제 #28
0
 public IEnumerator AsyncAsString() => UniTask.ToCoroutine(async() =>
 {
     var db = new SQLiteAsyncConnection(TestPath.GetTempFileName(), false);
     await TestAsyncDateTime(db);
 });
예제 #29
0
 public IEnumerator AsyncAsTicks() => UniTask.ToCoroutine(async() =>
 {
     var db = new SQLiteAsyncConnection(TestPath.GetTempFileName());
     await TestAsyncDateTimeOffset(db);
 });
예제 #30
0
 public TestDb() : base(TestPath.GetTempFileName())
 {
     Trace = true;
 }