Exemplo n.º 1
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);
        }
        public void CreateTableWithNotNullConstraints()
        {
            using (var db = new TestDb())
            {
                db.CreateTable <NotNullNoPK>();
                var cols = db.GetTableInfo("NotNullNoPK");

                var joined = from expected in GetExpectedColumnInfos(typeof(NotNullNoPK))
                             join actual in cols on expected.Name equals actual.Name
                             where actual.notnull != expected.notnull
                             select actual.Name;

                Assert.AreNotEqual(0, cols.Count(), "Failed to get table info");
                Assert.IsTrue(joined.Count() == 0, string.Format("not null constraint was not created for the following properties: {0}"
                                                                 , string.Join(", ", joined.ToArray())));
            }
        }
Exemplo n.º 3
0
        public void TestColumnValues()
        {
            using (TestDb db = new TestDb())
            {
                db.CreateTable <WithDefaultValue>();


                string failed = string.Empty;
                foreach (var col in db.GetMapping <WithDefaultValue>().Columns)
                {
                    if (col.PropertyName == "TestInt" && !col.DefaultValue.Equals(WithDefaultValue.IntVal))
                    {
                        failed += " , TestInt does not equal " + WithDefaultValue.IntVal;
                    }


                    if (col.PropertyName == "TestDecimal" && !col.DefaultValue.Equals(WithDefaultValue.DecimalVal))
                    {
                        failed += "TestDecimal does not equal " + WithDefaultValue.DecimalVal;
                    }

                    if (col.PropertyName == "TestDateTime" && !col.DefaultValue.Equals(WithDefaultValue.DateTimegVal))
                    {
                        failed += "TestDateTime does not equal " + WithDefaultValue.DateTimegVal;
                    }

                    if (col.PropertyName == "TestString" && !col.DefaultValue.Equals(WithDefaultValue.StringVal))
                    {
                        failed += "TestString does not equal " + WithDefaultValue.StringVal;
                    }

                    if (col.PropertyName == "DefaultValueInAttributeTestInt" && !col.DefaultValue.Equals(WithDefaultValue.IntVal))
                    {
                        failed += " , DefaultValueInAttributeTestInt does not equal " + WithDefaultValue.IntVal;
                    }

                    if (col.PropertyName == "TestIntWithSubtypeOfDefault" && !col.DefaultValue.Equals(WithDefaultValue.IntVal))
                    {
                        failed += " , TestIntWithSubtypeOfDefault does not equal " + WithDefaultValue.IntVal;
                    }
                }

                Assert.True(string.IsNullOrWhiteSpace(failed), failed);
            }
        }
Exemplo n.º 4
0
        public void Query()
        {
            var db = new TestDb();

            db.CreateTable <UnicodeProduct>();

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

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

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

            Assert.AreEqual(1, ps.Count);
            Assert.AreEqual(testString, ps[0].Name);
        }
Exemplo n.º 5
0
 public void SelectWorks()
 {
     using (var db = new TestDb(TestPath.CreateTemporaryDatabase()))
     {
         db.Insert(new TestObj()
         {
             Order = 5
         });
         try
         {
             Assert.That(db.Table <TestObj>().Select(obj => obj.Order * 2).First(), Is.EqualTo(10));
         }
         catch (NotImplementedException)
         {
             //Allow Not implemented exceptions as the selection may be too complex.
         }
     }
 }
Exemplo n.º 6
0
        public void AddForStringsMeansConcatenate()
        {
            int n = 20;
            IEnumerable <TestObjString> cq = from i in Enumerable.Range(1, n)
                                             select new TestObjString()
            {
                Data = i.ToString(),
            };

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

            db.InsertAll(cq);

            TableQuery <TestObjString> results = db.Table <TestObjString>().Where(o => o.Data + "1" == "11");

            Assert.AreEqual(1, results.Count());
            Assert.AreEqual("1", results.OrderBy(o => o.Data).FirstOrDefault().Data);
        }
Exemplo n.º 7
0
        private static TestDb GetTestDBWith100Elements()
        {
            int n = 100;

            IEnumerable <TestObj> cq = from i in Enumerable.Range(1, n)
                                       select new TestObj
            {
                Order = i
            };

            TestObj[] objs = cq.ToArray();
            var       db   = new TestDb(TestPath.CreateTemporaryDatabase());

            int numIn = db.InsertAll(objs);

            Assert.AreEqual(numIn, n, "Num inserted must = num objects");
            return(db);
        }
Exemplo n.º 8
0
        public void CanHaveSubtractInWhereClause()
        {
            int n = 20;
            IEnumerable <TestObjInt> cq = from i in Enumerable.Range(1, n)
                                          select new TestObjInt()
            {
                Data = i,
            };

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

            db.InsertAll(cq);

            TableQuery <TestObjInt> results = db.Table <TestObjInt>().Where(o => o.Data - 10 >= 0);

            Assert.AreEqual(results.Count(), 11);
            Assert.AreEqual(results.OrderBy(o => o.Data).FirstOrDefault().Data, 10);
        }
Exemplo n.º 9
0
        public void CreateInsertDrop()
        {
            var db = new TestDb();

            db.CreateTable <Product>();

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

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

            Assert.AreEqual(1, n);

            db.DropTable <Product>();

            Assert.Throws <SQLiteException>(() => db.Table <Product>().Count());
        }
Exemplo n.º 10
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),
                });

                TableQuery <Issue115_MyObject> query = conn.Table <Issue115_MyObject>();
                foreach (Issue115_MyObject itm in query)
                {
                    itm.OtherValue++;
                    Assert.AreEqual(1, conn.Update(itm, typeof(Issue115_MyObject)));
                }
            }
        }
Exemplo n.º 11
0
        public void CreateThem()
        {
            var db = new TestDb();

            var foo = new Product
            {
                Name  = "Foo",
                Price = 10.0m
            };
            var bar = new Product
            {
                Name  = "Bar",
                Price = 0.10m
            };

            db.Insert(foo);
            db.Insert(bar);
            db.Insert(new OrderLine
            {
                ProductId = foo.Id,
                Quantity  = 6,
                UnitPrice = 10.01m
            });
            db.Insert(new OrderLine
            {
                ProductId = foo.Id,
                Quantity  = 3,
                UnitPrice = 0.02m
            });
            db.Insert(new OrderLine
            {
                ProductId = bar.Id,
                Quantity  = 9,
                UnitPrice = 100.01m
            });

            OrderLine[] lines = foo.GetOrderLines(db);

            Assert.AreEqual(lines.Length, 2, "Has 2 order lines");
        }
Exemplo n.º 12
0
        private static void CheckIndex(TestDb db, List <IndexInfo> indexes, string iname, bool unique,
                                       params string[] columns)
        {
            if (columns == null)
            {
                throw new Exception("Don't!");
            }
            IndexInfo 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));
            List <IndexColumns> 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 (string col in columns)
            {
                Assert.IsNotNull(idx_columns.SingleOrDefault(c => c.name == col),
                                 String.Format("Column {0} not in index {1}", col, idx.name));
            }
        }
Exemplo n.º 13
0
        private void TestDateTime(TestDb db)
        {
            db.CreateTable <TestObj>();

            //
            // Ticks
            //
            var org = new TestObj
            {
                Time1 = DateTime.UtcNow,
                Time2 = DateTime.Now,
            };

            db.Insert(org);
            var fromDb = db.Get <TestObj>(org.Id);

            Assert.AreEqual(fromDb.Time1.ToUniversalTime(), org.Time1.ToUniversalTime());
            Assert.AreEqual(fromDb.Time2.ToUniversalTime(), org.Time2.ToUniversalTime());

            Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime());
            Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime());
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        public void WhereGreaterThan()
        {
            TestDb db = CreateDb();

            db.Insert(new Product
            {
                Name  = "A",
                Price = 20,
            });

            db.Insert(new Product
            {
                Name  = "B",
                Price = 10,
            });

            Assert.AreEqual(2, db.Table <Product>().Count());

            List <Product> r = (from p in db.Table <Product>() where p.Price > 15 select p).ToList();

            Assert.AreEqual(1, r.Count);
            Assert.AreEqual("A", r[0].Name);
        }
Exemplo n.º 16
0
        public void FunctionParameter()
        {
            TestDb db = CreateDb();

            db.Insert(new Product
            {
                Name  = "A",
                Price = 20,
            });

            db.Insert(new Product
            {
                Name  = "B",
                Price = 10,
            });

            Func <decimal, List <Product> > GetProductsWithPriceAtLeast =
                delegate(decimal val) { return((from p in db.Table <Product>() where p.Price > val select p).ToList()); };

            List <Product> r = GetProductsWithPriceAtLeast(15);

            Assert.AreEqual(1, r.Count);
            Assert.AreEqual("A", r[0].Name);
        }
Exemplo n.º 17
0
        public void ReplaceInWhere()
        {
            string testElement      = "Element";
            string alternateElement = "Alternate";
            string replacedElement  = "ReplacedElement";

            int n = 20;
            IEnumerable <TestObj> cq = from i in Enumerable.Range(1, n)
                                       select new TestObj
            {
                Name = (i % 2 == 0) ? testElement : alternateElement
            };

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

            db.InsertAll(cq);

            db.TraceListener = DebugTraceListener.Instance;


            List <TestObj> result = (from o in db.Table <TestObj>() where o.Name.Replace(testElement, replacedElement) == replacedElement select o).ToList();

            Assert.AreEqual(10, result.Count);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 19
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.º 20
0
 public OrderLine[] GetOrderLines(TestDb db)
 {
     return(db.Table <OrderLine>().Where(o => o.ProductId == Id).ToArray());
 }
Exemplo n.º 21
0
 public void Setup()
 {
     _db = new TestDb(TestPath.CreateTemporaryDatabase());
 }
Exemplo n.º 22
0
 public void SetUp()
 {
     _db = new TestDb();
     _db.CreateTable <ComplexType>();
 }
Exemplo n.º 23
0
        public void AsTicks()
        {
            var db = new TestDb(true);

            TestDateTime(db);
        }
Exemplo n.º 24
0
        public void AsStrings()
        {
            var db = new TestDb(storeDateTimeAsTicks: false);

            TestDateTime(db);
        }
Exemplo n.º 25
0
        public void AsTicks()
        {
            var db = new TestDb();

            TestDateTimeOffset(db);
        }