Beispiel #1
0
        public void SetUp()
        {
            _db = new TestDb();
            _db.CreateTable <Product> ();
            _db.CreateTable <Order> ();
            _db.CreateTable <OrderLine> ();

            var p1 = new Product {
                Name = "One",
            };
            var p2 = new Product {
                Name = "Two",
            };
            var p3 = new Product {
                Name = "Three",
            };

            _db.InsertAll(new [] { p1, p2, p3 });

            var o1 = new Order {
                PlacedTime = DateTime.Now,
            };
            var o2 = new Order {
                PlacedTime = DateTime.Now,
            };

            _db.InsertAll(new [] { o1, o2 });

            _db.InsertAll(new [] {
                new OrderLine {
                    OrderId   = o1.Id,
                    ProductId = p1.Id,
                    Quantity  = 1,
                },
                new OrderLine {
                    OrderId   = o1.Id,
                    ProductId = p2.Id,
                    Quantity  = 2,
                },
                new OrderLine {
                    OrderId   = o2.Id,
                    ProductId = p3.Id,
                    Quantity  = 3,
                },
            });
        }
Beispiel #2
0
        public void Issue115_MissingPrimaryKey()
        {
            using (var conn = new TestDb()) {
                conn.CreateTable <Issue115_MyObject> ();
                conn.InsertAll(from i in Enumerable.Range(0, 10) select new Issue115_MyObject {
                    UniqueId   = i.ToString(),
                    OtherValue = (byte)(i * 10),
                });

                var query = conn.Table <Issue115_MyObject> ();
                foreach (var itm in query)
                {
                    itm.OtherValue++;
                    Assert.AreEqual(1, conn.Update(itm, typeof(Issue115_MyObject)));
                }
            }
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public void WithoutImplicitMapping()
        {
            var db = new TestDb();

            db.CreateTable <NoAttributes>();

            var mapping = db.GetMapping <NoAttributes>();

            Assert.IsNull(mapping.PK);

            var column = mapping.Columns[2];

            Assert.AreEqual("IndexedId", column.Name);
            Assert.IsFalse(column.Indices.Any());

            CheckPK(db);
        }
Beispiel #5
0
        static void CheckIndex(TestDb db, List <IndexInfo> indexes, string iname, bool unique, params string [] columns)
        {
            if (columns == null)
            {
                throw new Exception("Don't!");
            }
            var idx = indexes.SingleOrDefault(i => i.name == iname);

            Assert.IsNotNull(idx, String.Format("Index {0} not found", iname));
            Assert.AreEqual(idx.unique, unique, String.Format("Index {0} unique expected {1} but got {2}", iname, unique, idx.unique));
            var idx_columns = db.Query <IndexColumns> (String.Format("PRAGMA INDEX_INFO (\"{0}\")", iname));

            Assert.AreEqual(columns.Length, idx_columns.Count, String.Format("# of columns: expected {0}, got {1}", columns.Length, idx_columns.Count));
            foreach (var col in columns)
            {
                Assert.IsNotNull(idx_columns.SingleOrDefault(c => c.name == col), String.Format("Column {0} not in index {1}", col, idx.name));
            }
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }
Beispiel #9
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());
        }
        public void SetUp()
        {
            db = new TestDb();
            db.CreateTable <Product> ();

            var prods = new[] {
                new Product {
                    Name = "Foo"
                },
                new Product {
                    Name = "Bar"
                },
                new Product {
                    Name = "Foobar"
                },
            };

            db.InsertAll(prods);
        }
        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.");
        }
Beispiel #12
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);
        }
        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.");
            }
        }
Beispiel #14
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);
            }
        }
Beispiel #15
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);
        }
        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.");
        }
Beispiel #17
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);
            }
        }
Beispiel #18
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);
            }
        }
Beispiel #19
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);
        }
        public void SetUp()
        {
            db       = new TestDb();
            db.Trace = true;
            db.CreateTable <Product>();
            db.CreateTable <Order>();
            db.InsertAll(from i in Enumerable.Range(0, 22)
                         select new Product {
                Name = "Thing" + i, Price = (decimal)Math.Pow(2, i)
            });

            changeCount = 0;

            db.TableChanged += (sender, e) => {
                if (e.Table.TableName == "Product")
                {
                    changeCount++;
                    if (!db.IsInTransaction)
                    {
                        Console.WriteLine("this changed called by the end of InsertAll");
                    }
                }
            };
        }
Beispiel #21
0
 public void Setup()
 {
     _db = new TestDb(TestPath.GetTempFileName());
 }
Beispiel #22
0
 static T GetEntity <T> (TestDb db, int id) where T : IEntity, new ()
 {
     return(db.Table <T> ().FirstOrDefault(x => x.Id == id));
 }
Beispiel #23
0
        public void CreateTypeWithNoProps()
        {
            var db = new TestDb();

            db.CreateTable <NoPropObject> ();
        }
Beispiel #24
0
        public void CreateTableSucceeds()
        {
            var db = new TestDb();

            db.CreateTable <TestObj> ();
        }
Beispiel #25
0
 public void Setup()
 {
     _db = new TestDb(TestPath.GetTempFileName(), new DefalutConvert());
 }
        public void AsTicks()
        {
            var db = new TestDb();

            TestDateTimeOffset(db);
        }
Beispiel #27
0
        public void AsTicks()
        {
            var db = new TestDb(storeDateTimeAsTicks: true);

            TestDateTime(db);
        }
Beispiel #28
0
 public void SetEmptyStringKey()
 {
     using (var db = new TestDb(key: "")) {
     }
 }
Beispiel #29
0
        public void AsStrings()
        {
            var db = new TestDb(storeDateTimeAsTicks: false);

            TestDateTime(db);
        }