Пример #1
0
        public void Setup()
        {
            testObjects = Enumerable.Range(1, 20).Select(i => new TestObj()).ToList();

            db = new TestDb(TestPath.GetTempFileName());
            db.InsertAll(testObjects);
        }
Пример #2
0
        public void Skip()
        {
            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.GetTempFileName());

            int numIn = db.InsertAll(objs);
            Assert.AreEqual(numIn, n, "Num inserted must = num objects");

            TableQuery<TestObj> q = from o in db.Table<TestObj>()
                orderby o.Order
                select o;

            TableQuery<TestObj> qs1 = q.Skip(1);
            List<TestObj> s1 = qs1.ToList();
            Assert.AreEqual(n - 1, s1.Count);
            Assert.AreEqual(2, s1[0].Order);

            TableQuery<TestObj> qs5 = q.Skip(5);
            List<TestObj> s5 = qs5.ToList();
            Assert.AreEqual(n - 5, s5.Count);
            Assert.AreEqual(6, s5[0].Order);
        }
Пример #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;
                }
                
                Assert.True(string.IsNullOrWhiteSpace(failed), failed);

            }
        }
Пример #4
0
        private SQLiteConnection CreateDb()
        {
            var db = new TestDb();

            db.CreateTable<TestTable>();
            db.CreateTable<TestTableCompositeKey>();

            var items =
                from i in Enumerable.Range(0, Count)
                select new TestTable
                {
                    Datum = 1000 + i,
                    Test = "Hello World"
                }
                ;
            db.InsertAll(items);

            var itemsCompositeKey =
                from i in Enumerable.Range(0, Count)
                select new TestTableCompositeKey
                {
                    Datum = 1000 + i,
                    Test = "Hello World",
                    Id = i,
                    TestIndex = i + 1
                }
                ;
            db.InsertAll(itemsCompositeKey);
            Assert.AreEqual(Count, db.Table<TestTableCompositeKey>().Count());

            return db;
        }
Пример #5
0
        public void ContainsQueriedData()
        {
            int n = 20;
            IEnumerable<TestObj> cq = from i in Enumerable.Range(1, n)
                select new TestObj
                {
                    Name = i.ToString()
                };

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

            db.InsertAll(cq);

            db.Trace = true;

            var tensq = new[] {"0", "10", "20"};
            List<TestObj> tens = (from o in db.Table<TestObj>() where tensq.Contains(o.Name) select o).ToList();
            Assert.AreEqual(2, tens.Count);

            var moreq = new[] {"0", "x", "99", "10", "20", "234324"};
            List<TestObj> more = (from o in db.Table<TestObj>() where moreq.Contains(o.Name) select o).ToList();
            Assert.AreEqual(2, more.Count);

            // https://github.com/praeclarum/SQLite.Net/issues/28
            List<string> moreq2 = moreq.ToList();
            List<TestObj> more2 = (from o in db.Table<TestObj>() where moreq2.Contains(o.Name) select o).ToList();
            Assert.AreEqual(2, more2.Count);
        }
Пример #6
0
        public void Setup()
        {
            testObjects = Enumerable.Range(1, 20).Select(i => new TestObj()).ToList();

            db = new TestDb(TestPath.CreateTemporaryDatabase());
            db.InsertAll(testObjects);
        }
Пример #7
0
        public void Collate()
        {
            var obj = new TestObj
            {
                CollateDefault = "Alpha ",
                CollateBinary = "Alpha ",
                CollateRTrim = "Alpha ",
                CollateNoCase = "Alpha ",
            };

            var db = new TestDb(new SQLitePlatformTest(), 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());
        }
Пример #8
0
 private TestDb CreateDb()
 {
     var db = new TestDb();
     db.CreateTable<Product>();
     db.CreateTable<Order>();
     db.CreateTable<OrderLine>();
     db.CreateTable<OrderHistory>();
     return db;
 }
Пример #9
0
        public void InheritanceWorks()
        {
            var db = new TestDb();

            TableMapping mapping = db.GetMapping<Derived>();

            Assert.AreEqual(3, mapping.Columns.Length);
            Assert.AreEqual("Id", mapping.PK.Name);
        }
Пример #10
0
        public void CreateThem()
        {
            var db = new TestDb();

            db.CreateTable<Product>();
            db.CreateTable<Order>();
            db.CreateTable<OrderLine>();
            db.CreateTable<OrderHistory>();

            VerifyCreations(db);
        }
        public void ImplicitIndex()
        {
            var db = new TestDb();

            db.CreateTable<NoAttributes>(CreateFlags.ImplicitIndex);

            TableMapping mapping = db.GetMapping<NoAttributes>();
            TableMapping.Column column = mapping.Columns[2];
            Assert.AreEqual("IndexedId", column.Name);
            Assert.IsTrue(column.Indices.Any());
        }
Пример #12
0
        public void CreateAsPassedInTypes()
        {
            var db = new TestDb();

            db.CreateTable(typeof (Product));
            db.CreateTable(typeof (Order));
            db.CreateTable(typeof (OrderLine));
            db.CreateTable(typeof (OrderHistory));

            VerifyCreations(db);
        }
Пример #13
0
        private async Task TestAsyncDateTime(SQLiteAsyncConnection db)
        {
            await db.CreateTableAsync<TestDb>();

            var val1 = new TestDb
            {
                Time = new TimeSpan(1000),
            };
            await db.InsertAsync(val1);
            TestDb val2 = await db.GetAsync<TestDb>(val1.Id);
            Assert.AreEqual(val1.Time, val2.Time);
        }
Пример #14
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,
                }
            });
        }
Пример #15
0
        public void HasGoodNames()
        {
            var db = new TestDb();

            db.CreateTable<AFunnyTableName>();

            TableMapping mapping = db.GetMapping<AFunnyTableName>();

            Assert.AreEqual("AGoodTableName", mapping.TableName);

            Assert.AreEqual("Id", mapping.Columns[0].Name);
            Assert.AreEqual("AGoodColumnName", mapping.Columns[1].Name);
        }
Пример #16
0
 private SQLiteConnection CreateDb()
 {
     var db = new TestDb();
     db.CreateTable<TestTable>();
     IEnumerable<TestTable> items = from i in Enumerable.Range(0, Count)
         select new TestTable
         {
             Two = 2
         };
     db.InsertAll(items);
     Assert.AreEqual(Count, db.Table<TestTable>().Count());
     return db;
 }
Пример #17
0
        private static void VerifyCreations(TestDb db)
        {
            TableMapping orderLine = db.GetMapping(typeof (OrderLine));
            Assert.AreEqual(6, orderLine.Columns.Length);

            var l = new OrderLine
            {
                Status = OrderLineStatus.Shipped
            };
            db.Insert(l);
            OrderLine lo = db.Table<OrderLine>().First(x => x.Status == OrderLineStatus.Shipped);
            Assert.AreEqual(lo.Id, l.Id);
        }
Пример #18
0
 public void CreateUniqueIndexes()
 {
     using (var db = new TestDb())
     {
         db.CreateTable<TheOne>();
         List<IndexInfo> indexes = db.Query<IndexInfo>("PRAGMA INDEX_LIST (\"TheOne\")");
         Assert.AreEqual(4, indexes.Count, "# of indexes");
         CheckIndex(db, indexes, "UX_Uno", true, "Uno");
         CheckIndex(db, indexes, "UX_Dos", true, "Dos", "Tres");
         CheckIndex(db, indexes, "UX_Uno_bool", true, "Cuatro");
         CheckIndex(db, indexes, "UX_Dos_bool", true, "Cinco", "Seis");
     }
 }
        public void ImplicitAutoIncAsPassedInTypes()
        {
            var db = new TestDb();

            db.CreateTable(typeof (PkAttribute), CreateFlags.AutoIncPK);

            TableMapping mapping = db.GetMapping<PkAttribute>();

            Assert.IsNotNull(mapping.PK);
            Assert.AreEqual("Id", mapping.PK.Name);
            Assert.IsTrue(mapping.PK.IsPK);
            Assert.IsTrue(mapping.PK.IsAutoInc);
        }
Пример #20
0
        public void CanUseSubtypeOfTableAttribute()
        {
            var db = new TestDb();
            db.CreateTable<Cat>();

            db.Insert(new Cat()
            {
                Breed = "Siamese"
            });

            int numCats = db.ExecuteScalar<int>("select count(*) from Cats");

            Assert.That(numCats,Is.EqualTo(1), "The resulting num cats should be 1.");
        }
        public void ImplicitAutoInc()
        {
            var db = new TestDb();

            db.CreateTable<PkAttribute>(CreateFlags.AutoIncPK);

            TableMapping mapping = db.GetMapping<PkAttribute>();

            Assert.IsFalse(mapping.HasCompositePK);
            Assert.IsNotNull(mapping.PK);
            Assert.AreEqual("Id", mapping.PK.Name);
            Assert.IsTrue(mapping.PK.IsPK);
            Assert.IsTrue(mapping.PK.IsAutoInc);
        }
Пример #22
0
        public void UpperAndLowerColumnNames()
        {
            using (var db = new TestDb(true)
            {
                TraceListener = DebugTraceListener.Instance
            })
            {
                db.CreateTable<LowerId>();
                db.CreateTable<UpperId>();

                List<SQLiteConnection.ColumnInfo> cols = db.GetTableInfo("Test").ToList();
                Assert.That(cols.Count, Is.EqualTo(1));
                Assert.That(cols[0].Name, Is.EqualTo("Id"));
            }
        }
Пример #23
0
        public void ToUpper()
        {
            var db = new TestDb();

            db.CreateTable<TestTable>();
            var testTable = new TestTable()
            {
                Name = "test"
            };
            db.Insert(testTable);

            var x = db.Table<TestTable>().Where(t => t.Name.ToUpper() == "TEST");

            Assert.AreEqual(1, x.Count());
        }
Пример #24
0
 public void SelectWorks()
 {
     using (var db = new TestDb(TestPath.GetTempFileName()))
     {
         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.
         }
     }
    
 }
 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);
 }
        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);
        }
Пример #27
0
        public void CreateTableWithNotNullConstraints()
        {
            using (var db = new TestDb())
            {
                db.CreateTable <NotNullNoPK>();
                var cols = db.GetTableInfo("NotNullNoPK");

                var joined = from expected in GetExpectedColumnInfos(db.Platform.ReflectionService, 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())));
            }
        }
Пример #28
0
		public void Insert()
		{
			var db = new TestDb();

			db.CreateTable<UnicodeProduct>();

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

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

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

			Assert.AreEqual(testString, p.Name);
		}
Пример #29
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);
        }
        public void CreateTableWithNotNullConstraints()
        {
            using (var db = new TestDb())
            {
                db.CreateTable<NotNullNoPK>();
                var cols = db.GetTableInfo("NotNullNoPK");

                var joined = from expected in GetExpectedColumnInfos(db.Platform.ReflectionService, 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())));
            }
        }
Пример #31
0
        private void TestDateTime(TestDb db)
        {
            db.CreateTable <TestObj>();

            TestObj o, o2;

            //
            // Ticks
            //
            o = new TestObj
            {
                ModifiedTime = new DateTime(2012, 1, 14, 3, 2, 1),
            };
            db.Insert(o);
            o2 = db.Get <TestObj>(o.Id);
            Assert.AreEqual(o.ModifiedTime, o2.ModifiedTime);
        }
Пример #32
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);
        }
Пример #33
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);
            }
        }
Пример #34
0
        public void Query()
        {
            var db = new TestDb();

            db.CreateTable <Product>();

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

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

            List <Product> 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);
        }
Пример #35
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);
		}
Пример #36
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);
        }
Пример #37
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);
        }
Пример #38
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.GetTempFileName());

            int numIn = db.InsertAll(objs);

            Assert.AreEqual(numIn, n, "Num inserted must = num objects");
            return(db);
        }
Пример #39
0
 public void SelectWorks()
 {
     using (var db = new TestDb(TestPath.GetTempFileName()))
     {
         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.
         }
     }
 }
Пример #40
0
        private SQLiteConnection CreateDb()
        {
            var db = new TestDb();

            db.CreateTable <TestTable>();
            var items =
                from i in Enumerable.Range(0, Count)
                select new TestTable
            {
                Datum = 1000 + i,
                Test  = "Hello World"
            }
            ;

            db.InsertAll(items);
            Assert.AreEqual(Count, db.Table <TestTable>().Count());
            return(db);
        }
        public void CreateThem()
        {
            var db = new TestDb(new SQLitePlatformWin32());

            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();

            Assert.AreEqual(lines.Length, 2, "Has 2 order lines");
            Assert.AreEqual(foo.Connection, db, "foo.Connection was set");
            Assert.AreEqual(lines[0].Connection, db, "lines[0].Connection was set");
        }
Пример #42
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>();

            ExceptionAssert.Throws <SQLiteException>(() => db.Table <Product>().Count());
        }
Пример #43
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)));
                }
            }
        }
Пример #44
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>();

            ExceptionAssert.Throws<SQLiteException>(() => db.Table<Product>().Count());
        }
Пример #45
0
        private void TestDateTime(TestDb db)
        {
            db.CreateTable <TestObj>();

            TestObj o, o2;

            //
            // Ticks
            //
            o = new TestObj
            {
                ModifiedTime = DateTime.UtcNow,
            };
            db.Insert(o);
            o2 = db.Get <TestObj>(o.Id);
            Assert.AreEqual(o.ModifiedTime, o2.ModifiedTime.ToUniversalTime());

            var expectedTimeZone = db.StoreDateTimeAsTicks ? DateTimeKind.Utc : DateTimeKind.Local;

            Assert.AreEqual(o2.ModifiedTime.Kind, expectedTimeZone);
        }
Пример #46
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));
            }
        }
Пример #47
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());
        }
Пример #48
0
        public void ExecuteNonQueryWithNullThrowsException()
        {
            using (TestDb db = new TestDb())
            {
                TableMapping map;

                db.CreateTable <NotNullNoPK>();

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

                    map = db.GetMapping <NotNullNoPK>();
                    map.GetInsertCommand(db, "OR REPLACE").ExecuteNonQuery(new object[] { 1, null, 123, null, null, null });
                }
                catch (NotNullConstraintViolationException)
                {
                    return;
                }
                catch (SQLiteException ex)
                {
                    if (db.Platform.SQLiteApi.LibVersionNumber() < 3007017 && ex.Result == Result.Constraint)
                    {
                        Assert.Inconclusive("Detailed constraint information is only available in SQLite3 version 3.7.17 and above.");
                    }
                }
                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.");
        }
Пример #49
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);
        }
Пример #50
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);
        }
Пример #51
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);
        }
Пример #52
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(new SQLitePlatformTest(), 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);
        }
Пример #53
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);
            }
        }
Пример #54
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);
            }
        }
Пример #55
0
        public void CreateDatabase()
        {
            _testDb = new TestDb(new SQLitePlatformTest());

            _testDb.Insert(new Employee {
                Id = 1, Name = "Paul", Age = 32, Address = "California"
            });
            _testDb.Insert(new Employee {
                Id = 2, Name = "Allen", Age = 25, Address = "Texas"
            });
            _testDb.Insert(new Employee {
                Id = 3, Name = "Teddy", Age = 23, Address = "Norway"
            });
            _testDb.Insert(new Employee {
                Id = 4, Name = "Mark", Age = 25, Address = "Rich-Mond"
            });
            _testDb.Insert(new Employee {
                Id = 5, Name = "David", Age = 27, Address = "Texas"
            });
            _testDb.Insert(new Employee {
                Id = 6, Name = "Kim", Age = 22, Address = "South-Hall"
            });
            _testDb.Insert(new Employee {
                Id = 7, Name = "James", Age = 24, Address = "Houston"
            });

            _testDb.Insert(new Department {
                Id = 1, Name = "IT Billing", EmployeeId = 1
            });
            _testDb.Insert(new Department {
                Id = 2, Name = "Engineerin", EmployeeId = 2
            });
            _testDb.Insert(new Department {
                Id = 3, Name = "Finance", EmployeeId = 7
            });
        }
Пример #56
0
        public void AsTicks()
        {
            var db = new TestDb(true);

            TestDateTime(db);
        }
Пример #57
0
        public void AsTicks()
        {
            var db = new TestDb();

            TestDateTimeOffset(db);
        }
Пример #58
0
 public void SetUp()
 {
     _db = new TestDb();
     _db.CreateTable <ComplexType>();
 }
Пример #59
0
 public void Setup()
 {
     _db = new TestDb(TestPath.GetTempFileName());
 }
Пример #60
0
        public void AsStrings()
        {
            var db = new TestDb(storeDateTimeAsTicks: false);

            TestDateTime(db);
        }