Exemplo n.º 1
0
        public void ShouldPersistAndReadEnum()
        {
            var db = new TestDb(TestPath.GetTempFileName());

            var obj1 = new TestObj()
            {
                Id = 1, Value = TestEnum.Value2
            };
            var obj2 = new TestObj()
            {
                Id = 2, Value = TestEnum.Value3
            };

            var numIn1 = db.Insert(obj1);
            var numIn2 = db.Insert(obj2);

            Assert.AreEqual(1, numIn1);
            Assert.AreEqual(1, numIn2);

            var result = db.Query <TestObj>("select * from TestObj").ToList();

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(obj1.Value, result[0].Value);
            Assert.AreEqual(obj2.Value, result[1].Value);

            Assert.AreEqual(obj1.Id, result[0].Id);
            Assert.AreEqual(obj2.Id, result[1].Id);

            db.Close();
        }
Exemplo n.º 2
0
        public void ShouldPersistAndReadGuid()
        {
            var db = new TestDb(TestPath.GetTempFileName());

            var obj1 = new TestObj()
            {
                Id = new Guid("36473164-C9E4-4CDF-B266-A0B287C85623"), Text = "First Guid Object"
            };
            var obj2 = new TestObj()
            {
                Id = new Guid("BC5C4C4A-CA57-4B61-8B53-9FD4673528B6"), Text = "Second Guid Object"
            };

            var numIn1 = db.Insert(obj1);
            var numIn2 = db.Insert(obj2);

            Assert.AreEqual(1, numIn1);
            Assert.AreEqual(1, numIn2);

            var result = db.Query <TestObj>("select * from TestObj").ToList();

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(obj1.Text, result[0].Text);
            Assert.AreEqual(obj2.Text, result[1].Text);

            Assert.AreEqual(obj1.Id, result[0].Id);
            Assert.AreEqual(obj2.Id, result[1].Id);

            db.Close();
        }
Exemplo n.º 3
0
 public void ListAndCustomTypeTest()
 {
     _db.Insert(Cruze);
     _db.Insert(S600);
     _db.Insert(C600);
     Assert.AreEqual(3, _db.Table <Car>().Count());
     Assert.AreEqual(210, _db.Table <Car>().Where(c => c.Name.Equals("Cruze")).FirstOrDefault().Quality.Speed);
 }
Exemplo n.º 4
0
        public void SuccessfulSavepointTransaction()
        {
            db.RunInTransaction(() => {
                db.Delete(testObjects[0]);
                db.Delete(testObjects[1]);
                db.Insert(new TestObj());
            });

            Assert.AreEqual(testObjects.Count - 1, db.Table <TestObj>().Count());
        }
Exemplo n.º 5
0
        public void AddColumns()
        {
            //
            // Init the DB
            //
            var path = "";

            using (var db = new TestDb(true)
            {
                Trace = true
            }) {
                path = db.DatabasePath;

                db.CreateTable <TestAddBefore> ();

                var cols = db.GetTableInfo("TestAdd");
                Assert.AreEqual(2, cols.Count);

                var o = new TestAddBefore {
                    Name = "Foo",
                };

                db.Insert(o);

                var oo = db.Table <TestAddBefore> ().First();

                Assert.AreEqual("Foo", oo.Name);
            }

            //
            // Migrate and use it
            //
            using (var db = new SQLiteConnection(path, true)
            {
                Trace = true
            }) {
                db.CreateTable <TestAddAfter> ();

                var cols = db.GetTableInfo("TestAdd");
                Assert.AreEqual(4, cols.Count);

                var oo = db.Table <TestAddAfter> ().First();

                Assert.AreEqual("Foo", oo.Name);
                Assert.AreEqual(0, oo.IntValue);
                Assert.AreEqual(null, oo.StringValue);

                var o = new TestAddAfter {
                    Name        = "Bar",
                    IntValue    = 42,
                    StringValue = "Hello",
                };
                db.Insert(o);

                var ooo = db.Get <TestAddAfter> (o.Id);
                Assert.AreEqual("Bar", ooo.Name);
                Assert.AreEqual(42, ooo.IntValue);
                Assert.AreEqual("Hello", ooo.StringValue);
            }
        }
Exemplo n.º 6
0
        public void SetBytesKey()
        {
            string path;

            var rand = new Random();
            var key  = new byte[32];

            rand.NextBytes(key);

            using (var db = new TestDb(key: key)) {
                path = db.DatabasePath;

                db.CreateTable <TestTable> ();
                db.Insert(new TestTable {
                    Value = "Hello"
                });
            }

            using (var db = new TestDb(path, key: key)) {
                path = db.DatabasePath;

                var r = db.Table <TestTable> ().First();

                Assert.AreEqual("Hello", r.Value);
            }
        }
Exemplo n.º 7
0
        public void OnlyKey()
        {
            var db = new TestDb();

            db.CreateTable <OnlyKeyModel> ();

            db.InsertOrReplace(new OnlyKeyModel {
                MyModelId = "Foo"
            });
            var foo = db.Get <OnlyKeyModel> ("Foo");

            Assert.AreEqual(foo.MyModelId, "Foo");

            db.Insert(new OnlyKeyModel {
                MyModelId = "Bar"
            });
            var bar = db.Get <OnlyKeyModel> ("Bar");

            Assert.AreEqual(bar.MyModelId, "Bar");

            db.Update(new OnlyKeyModel {
                MyModelId = "Foo"
            });
            var foo2 = db.Get <OnlyKeyModel> ("Foo");

            Assert.AreEqual(foo2.MyModelId, "Foo");
        }
Exemplo n.º 8
0
        public void Collate()
        {
            var obj = new TestObj()
            {
                CollateDefault = "Alpha ",
                CollateBinary  = "Alpha ",
                CollateRTrim   = "Alpha ",
                CollateNoCase  = "Alpha ",
            };

            var db = new TestDb(TestPath.GetTempFileName());

            db.Insert(obj);

            Assert.AreEqual(1, (from o in db.Table <TestObj>() where o.CollateDefault == "Alpha " select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateDefault == "ALPHA " select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateDefault == "Alpha" select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateDefault == "ALPHA" select o).Count());

            Assert.AreEqual(1, (from o in db.Table <TestObj>() where o.CollateBinary == "Alpha " select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateBinary == "ALPHA " select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateBinary == "Alpha" select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateBinary == "ALPHA" select o).Count());

            Assert.AreEqual(1, (from o in db.Table <TestObj>() where o.CollateRTrim == "Alpha " select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateRTrim == "ALPHA " select o).Count());
            Assert.AreEqual(1, (from o in db.Table <TestObj>() where o.CollateRTrim == "Alpha" select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateRTrim == "ALPHA" select o).Count());

            Assert.AreEqual(1, (from o in db.Table <TestObj>() where o.CollateNoCase == "Alpha " select o).Count());
            Assert.AreEqual(1, (from o in db.Table <TestObj>() where o.CollateNoCase == "ALPHA " select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateNoCase == "Alpha" select o).Count());
            Assert.AreEqual(0, (from o in db.Table <TestObj>() where o.CollateNoCase == "ALPHA" select o).Count());
        }
Exemplo n.º 9
0
        public void NotNullConstraintExceptionListsOffendingColumnsOnInsert()
        {
            using (TestDb db = new TestDb()) {
                db.CreateTable <NotNullNoPK> ();

                try {
                    NotNullNoPK obj = new NotNullNoPK()
                    {
                        RequiredStringProp = "Some value"
                    };
                    db.Insert(obj);
                }
                catch (NotNullConstraintViolationException ex) {
                    string expected = "AnotherRequiredStringProp, RequiredIntProp";
                    string actual   = string.Join(", ", ex.Columns.Where(c => !c.IsPK).OrderBy(p => p.PropertyName).Select(c => c.PropertyName));

                    Assert.AreEqual(expected, actual, "NotNullConstraintViolationException did not correctly list the columns that violated the constraint");
                    return;
                }
                catch (SQLiteException ex) {
                    if (SQLite3.LibVersionNumber() < 3007017 && ex.Result == SQLite3.Result.Constraint)
                    {
                        Inconclusive();
                        return;
                    }
                }
                Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. No exception was thrown.");
            }
        }
Exemplo n.º 10
0
        public void UpdateWithNullThrowsException()
        {
            using (TestDb db = new TestDb()) {
                db.CreateTable <NotNullNoPK> ();

                try {
                    NotNullNoPK obj = new NotNullNoPK()
                    {
                        AnotherRequiredStringProp = "Another required string",
                        RequiredIntProp           = 123,
                        RequiredStringProp        = "Required string"
                    };
                    db.Insert(obj);
                    obj.RequiredStringProp = null;
                    db.Update(obj);
                }
                catch (NotNullConstraintViolationException) {
                    return;
                }
                catch (SQLiteException ex) {
                    if (SQLite3.LibVersionNumber() < 3007017 && ex.Result == SQLite3.Result.Constraint)
                    {
                        Inconclusive();
                        return;
                    }
                }
            }
            Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. No exception was thrown.");
        }
Exemplo n.º 11
0
        private void CheckPK(TestDb db)
        {
            for (int i = 1; i <= 10; i++)
            {
                var na = new NoAttributes {
                    Id = i, AColumn = i.ToString(), IndexedId = 0
                };
                db.Insert(na);
            }
            var item = db.Get <NoAttributes>(2);

            Assert.IsNotNull(item);
            Assert.AreEqual(2, item.Id);
        }
Exemplo n.º 12
0
        public void Issue86()
        {
            var db = new TestDb();

            db.CreateTable <Foo> ();

            db.Insert(new Foo {
                Bar = 42
            });
            db.Insert(new Foo {
                Bar = 69
            });

            var found42 = db.Table <Foo> ().Where(f => f.Bar == 42).FirstOrDefault();

            Assert.IsNotNull(found42);

            var ordered = new List <Foo> (db.Table <Foo> ().OrderByDescending(f => f.Bar));

            Assert.AreEqual(2, ordered.Count);
            Assert.AreEqual(69, ordered[0].Bar);
            Assert.AreEqual(42, ordered[1].Bar);
        }
Exemplo n.º 13
0
        public void InsertSucceeds()
        {
            var db = new TestDb();

            db.CreateTable <TestObj> ();

            var o = new TestObj {
                Text        = "Hello",
                IgnoredText = "World",
            };

            db.Insert(o);

            Assert.AreEqual(1, o.Id);
        }
Exemplo n.º 14
0
        public void Issue303_WhereNot_A()
        {
            using (var db = new TestDb())
            {
                db.CreateTable <Issue303_A>();
                db.Insert(new Issue303_A {
                    Id = 1, Name = "aa"
                });
                db.Insert(new Issue303_A {
                    Id = 2, Name = null
                });
                db.Insert(new Issue303_A {
                    Id = 3, Name = "test"
                });
                db.Insert(new Issue303_A {
                    Id = 4, Name = null
                });

                var r = (from p in db.Table <Issue303_A>() where !(p.Name == null) select p).ToList();
                Assert.AreEqual(2, r.Count);
                Assert.AreEqual(1, r[0].Id);
                Assert.AreEqual(3, r[1].Id);
            }
        }
Exemplo n.º 15
0
        public void Issue303_WhereNot_B()
        {
            using (var db = new TestDb())
            {
                db.CreateTable <Issue303_B>();
                db.Insert(new Issue303_B {
                    Id = 1, Flag = true
                });
                db.Insert(new Issue303_B {
                    Id = 2, Flag = false
                });
                db.Insert(new Issue303_B {
                    Id = 3, Flag = true
                });
                db.Insert(new Issue303_B {
                    Id = 4, Flag = false
                });

                var r = (from p in db.Table <Issue303_B>() where !p.Flag select p).ToList();
                Assert.AreEqual(2, r.Count);
                Assert.AreEqual(2, r[0].Id);
                Assert.AreEqual(4, r[1].Id);
            }
        }
Exemplo n.º 16
0
        public void Insert()
        {
            var query =
                from p in db.Table <Product>()
                select p;

            Assert.AreEqual(0, changeCount);
            Assert.AreEqual(22, query.Count());

            db.Insert(new Product {
                Name = "Hello", Price = 1001
            });

            Assert.AreEqual(1, changeCount);
            Assert.AreEqual(23, query.Count());
        }
Exemplo n.º 17
0
        private static void VerifyCreations(TestDb db)
        {
            var orderLine = db.GetMapping(typeof(OrderLine));

            Assert.AreEqual(6, orderLine.Columns.Length);

            var l = new OrderLine()
            {
                Status = OrderLineStatus.Shipped
            };

            db.Insert(l);
            var lo = db.Table <OrderLine>().First(x => x.Status == OrderLineStatus.Shipped);

            Assert.AreEqual(lo.Id, l.Id);
        }
Exemplo n.º 18
0
        void TestDateTimeOffset(TestDb db)
        {
            db.CreateTable <TestObj> ();

            TestObj o, o2;

            //
            // Ticks
            //
            o = new TestObj {
                ModifiedTime = new DateTimeOffset(2012, 1, 14, 3, 2, 1, TimeSpan.Zero),
            };
            db.Insert(o);
            o2 = db.Get <TestObj> (o.Id);
            Assert.AreEqual(o.ModifiedTime, o2.ModifiedTime);
        }
Exemplo n.º 19
0
        public void Insert()
        {
            var db = new TestDb();

            db.CreateTable <Product> ();

            string testString = "\u2329\u221E\u232A";

            db.Insert(new Product {
                Name = testString,
            });

            var p = db.Get <Product> (1);

            Assert.AreEqual(testString, p.Name);
        }
Exemplo n.º 20
0
        public void Query()
        {
            var db = new TestDb();

            db.CreateTable <Product> ();

            string testString = "\u2329\u221E\u232A";

            db.Insert(new Product {
                Name = testString,
            });

            var ps = (from p in db.Table <Product> () where p.Name == testString select p).ToList();

            Assert.AreEqual(1, ps.Count);
            Assert.AreEqual(testString, ps [0].Name);
        }
Exemplo n.º 21
0
        public void GetDoesntHaveIgnores()
        {
            var db = new TestDb();

            db.CreateTable <TestObj> ();

            var o = new TestObj {
                Text        = "Hello",
                IgnoredText = "World",
            };

            db.Insert(o);

            var oo = db.Get <TestObj> (o.Id);

            Assert.AreEqual("Hello", oo.Text);
            Assert.AreEqual(null, oo.IgnoredText);
        }
Exemplo n.º 22
0
        public void DerivedIgnore()
        {
            var db = new TestDb();

            db.CreateTable <DerivedIgnoreClass> ();

            var o = new DerivedIgnoreClass {
                Ignored    = "Hello",
                NotIgnored = "World",
            };

            db.Insert(o);

            var oo = db.Table <DerivedIgnoreClass> ().First();

            Assert.AreEqual(null, oo.Ignored);
            Assert.AreEqual("World", oo.NotIgnored);
        }
Exemplo n.º 23
0
        public void BaseIgnores()
        {
            var db = new TestDb();

            db.CreateTable <TableClass> ();

            var o = new TableClass {
                ToIgnore = "Hello",
                Name     = "World",
            };

            db.Insert(o);

            var oo = db.Table <TableClass> ().First();

            Assert.AreEqual(null, oo.ToIgnore);
            Assert.AreEqual("World", oo.Name);
        }
Exemplo n.º 24
0
        public void CreateInsertDrop()
        {
            var db = new TestDb();

            db.CreateTable <Product> ();

            db.Insert(new Product {
                Name  = "Hello",
                Price = 16,
            });

            var n = db.Table <Product> ().Count();

            Assert.AreEqual(1, n);

            db.DropTable <Product> ();

            ExceptionAssert.Throws <SQLiteException>(() => db.Table <Product> ().Count());
        }
Exemplo n.º 25
0
        public void InsertWithNullsThrowsException()
        {
            using (TestDb db = new TestDb()) {
                db.CreateTable <NotNullNoPK> ();

                try {
                    NotNullNoPK obj = new NotNullNoPK();
                    db.Insert(obj);
                }
                catch (NotNullConstraintViolationException) {
                    return;
                }
                catch (SQLiteException ex) {
                    if (SQLite3.LibVersionNumber() < 3007017 && ex.Result == SQLite3.Result.Constraint)
                    {
                        Inconclusive();
                        return;
                    }
                }
            }
            Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. No exception was thrown.");
        }
Exemplo n.º 26
0
        public void NotNullConstraintExceptionListsOffendingColumnsOnUpdate()
        {
            // Skip this test if the Dll doesn't support the extended SQLITE_CONSTRAINT codes

            using (TestDb db = new TestDb()) {
                db.CreateTable <NotNullNoPK> ();

                try {
                    NotNullNoPK obj = new NotNullNoPK()
                    {
                        AnotherRequiredStringProp = "Another required string",
                        RequiredIntProp           = 123,
                        RequiredStringProp        = "Required string"
                    };
                    db.Insert(obj);
                    obj.RequiredStringProp = null;
                    db.Update(obj);
                }
                catch (NotNullConstraintViolationException ex) {
                    string expected = "RequiredStringProp";
                    string actual   = string.Join(", ", ex.Columns.Where(c => !c.IsPK).OrderBy(p => p.PropertyName).Select(c => c.PropertyName));

                    Assert.AreEqual(expected, actual, "NotNullConstraintViolationException did not correctly list the columns that violated the constraint");

                    return;
                }
                catch (SQLiteException ex) {
                    if (SQLite3.LibVersionNumber() < 3007017 && ex.Result == SQLite3.Result.Constraint)
                    {
                        Inconclusive();
                        return;
                    }
                }
                catch (Exception ex) {
                    Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. An exception of type {0} was thrown instead.", ex.GetType().Name);
                }
                Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. No exception was thrown.");
            }
        }
Exemplo n.º 27
0
        public void RedefinedIgnores()
        {
            var db = new TestDb();

            db.CreateTable <RedefinedClass> ();

            var o = new RedefinedClass {
                Name   = "Foo",
                Value  = "Bar",
                Values = new List <string> {
                    "hello", "world"
                },
            };

            db.Insert(o);

            var oo = db.Table <RedefinedClass> ().First();

            Assert.AreEqual("Foo", oo.Name);
            Assert.AreEqual("Bar", oo.Value);
            Assert.AreEqual(null, oo.Values);
        }
Exemplo n.º 28
0
        public void SetStringKey()
        {
            string path;

            var key = "SecretPassword";

            using (var db = new TestDb(key: key)) {
                path = db.DatabasePath;

                db.CreateTable <TestTable> ();
                db.Insert(new TestTable {
                    Value = "Hello"
                });
            }

            using (var db = new TestDb(path, key: key)) {
                path = db.DatabasePath;

                var r = db.Table <TestTable> ().First();

                Assert.AreEqual("Hello", r.Value);
            }
        }
Exemplo n.º 29
0
        public void ExecuteNonQueryWithNullThrowsException()
        {
            using (TestDb db = new TestDb()) {
                db.CreateTable <NotNullNoPK> ();

                try {
                    NotNullNoPK obj = new NotNullNoPK()
                    {
                        AnotherRequiredStringProp = "Another required prop",
                        RequiredIntProp           = 123,
                        RequiredStringProp        = "Required string prop"
                    };
                    db.Insert(obj);

                    NotNullNoPK obj2 = new NotNullNoPK()
                    {
                        objectId        = 1,
                        OptionalIntProp = 123,
                    };
                    db.InsertOrReplace(obj2);
                }
                catch (NotNullConstraintViolationException) {
                    return;
                }
                catch (SQLiteException ex) {
                    if (SQLite3.LibVersionNumber() < 3007017 && ex.Result == SQLite3.Result.Constraint)
                    {
                        Inconclusive();
                        return;
                    }
                }
                catch (Exception ex) {
                    Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. An exception of type {0} was thrown instead.", ex.GetType().Name);
                }
            }
            Assert.Fail("Expected an exception of type NotNullConstraintViolationException to be thrown. No exception was thrown.");
        }
Exemplo n.º 30
0
        public void OverrideNames()
        {
            var db = new TestDb();

            db.CreateTable <OverrideNamesClass> ();

            var cols = db.GetTableInfo("OverrideNamesClass");

            Assert.AreEqual(3, cols.Count);
            Assert.IsTrue(cols.Exists(x => x.Name == "n"));
            Assert.IsTrue(cols.Exists(x => x.Name == "v"));

            var o = new OverrideNamesClass {
                Name  = "Foo",
                Value = "Bar",
            };

            db.Insert(o);

            var oo = db.Table <OverrideNamesClass> ().First();

            Assert.AreEqual("Foo", oo.Name);
            Assert.AreEqual("Bar", oo.Value);
        }