예제 #1
0
        public void ChangeCaseDoesNotThrowDuplicateErrorTest()
        {
            var db = new OrmTestSession();

            db.CreateTable <InitialTable>();
            db.CreateTable <RenamedTable>();
        }
예제 #2
0
        public void CreateMultiReferencingTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <MultiReferencedTable>();
            db.CreateTable <MultiReferencingTable>();

            TableMapping refingMap = db.GetMapping <MultiReferencingTable>();

            var sql     = refingMap.GetCreateSql();
            var correct =
                @"CREATE TABLE [MultiReferencingTable] (
[RefId] integer NOT NULL,
[Indexed] integer NOT NULL,
CONSTRAINT [FK_Foreign_Key]
FOREIGN KEY ([Indexed], [RefId])
REFERENCES [MultiReferencedTable] ([Id2], [Id])
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual(1, refingMap.ForeignKeys.Count);
            Assert.AreEqual("FK_Foreign_Key", refingMap.ForeignKeys.First().Name);
            TableMapping.ForeignKey[] fk = refingMap.ForeignKeys.Where(f => f.ChildTable == "MultiReferencedTable").ToArray();
            Assert.AreEqual(1, fk.Count());

            Assert.AreEqual("RefId", fk[0].Keys.Skip(1).First().Key);
            Assert.AreEqual("Id", fk[0].Keys.Skip(1).First().Value);
            Assert.AreEqual("Indexed", fk[0].Keys.First().Key);
            Assert.AreEqual("Id2", fk[0].Keys.First().Value);
        }
예제 #3
0
        public void CreateReferencingTable()
        {
            var db = new OrmTestSession();

            db.CreateTable <ReferencedTable>();
            db.CreateTable <ReferencingTable>();

            TableMapping refingMap = db.GetMapping <ReferencingTable>();

            var sql     = refingMap.GetCreateSql();
            var correct =
                @"CREATE TABLE [ReferencingTable] (
[RefId] integer NOT NULL,
[RandomName] integer NOT NULL,
CONSTRAINT [FK_Foreign_Key]
FOREIGN KEY ([RefId])
REFERENCES [ReferencedTable] ([Id]),
FOREIGN KEY ([RandomName])
REFERENCES [ReferencedTable] ([Id2])
ON UPDATE CASCADE
);";

            Assert.AreEqual(correct, sql);

            Assert.AreEqual(2, refingMap.ForeignKeys.Count);
            Assert.AreEqual(1, refingMap.ForeignKeys.Count(f => f.Name == "FK_Foreign_Key"));
            TableMapping.ForeignKey[] fk = refingMap.ForeignKeys.Where(f => f.ChildTable == "ReferencedTable").ToArray();
            Assert.AreEqual(2, fk.Count());
            Assert.AreEqual("Id", fk.Where(f => f.Keys.First().Key == "RefId").First().Keys.First().Value);
            Assert.AreEqual("Id2", fk.Where(f => f.Keys.First().Key == "RandomName").First().Keys.First().Value);
        }
예제 #4
0
        public void InsertIntoTwoTables()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();
            db.CreateTable <TestObj2>();

            var obj1 = new TestObj {
                Text = "GLaDOS loves testing!"
            };
            var obj2 = new TestObj2 {
                Text = "Keep testing, just keep testing"
            };

            int numIn1 = db.Insert(obj1);

            Assert.AreEqual(1, numIn1);
            int numIn2 = db.Insert(obj2);

            Assert.AreEqual(1, numIn2);

            List <TestObj> result1 = db.Table <TestObj>().ToList();

            Assert.AreEqual(numIn1, result1.Count);
            Assert.AreEqual(obj1.Text, result1.First().Text);

            List <TestObj> result2 = db.Query <TestObj>("select * from TestObj2").ToList();

            Assert.AreEqual(numIn2, result2.Count);

            db.Close();
        }
예제 #5
0
        public void CreateThem()
        {
            var db = new OrmTestSession();

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

            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.OrderLines;

            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");
        }
예제 #6
0
        public void CreateTableWithForeignKeyColumnThatDoesNotExist()
        {
            var db = new OrmTestSession();

            db.CreateTable <Category>();
            ExceptionAssert.Throws <ArgumentException>(() => db.CreateTable <Book>());
        }
        private OrmTestSession CreateDb()
        {
            var db = new OrmTestSession();

            db.CreateTable <Product>();
            return(db);
        }
        public void CreateInsertDrop()
        {
            var db = new OrmTestSession();

            db.CreateTable<Product>();

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

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

            Assert.AreEqual(1, n);

            db.DropTable<Product>();

            try
            {
                // Should throw SqliteException
                db.Table<Product>().Count();

                Assert.Fail("Expeced 'table does not exist' error.");
            }
            catch (SqliteException)
            {
            }
            catch
            {
                Assert.Fail();
            }
        }
예제 #9
0
        public void Skip()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();
            const int n = 100;

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

            TestObj[] objs = cq.ToArray();

            int numIn = db.InsertAll(objs);

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

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

            var            qs1 = q.Skip(1);
            List <TestObj> s1  = qs1.ToList();

            Assert.AreEqual(n - 1, s1.Count);
            Assert.AreEqual(2, s1[0].Order);

            var            qs5 = q.Skip(5);
            List <TestObj> s5  = qs5.ToList();

            Assert.AreEqual(n - 5, s5.Count);
            Assert.AreEqual(6, s5[0].Order);
        }
예제 #10
0
        public void ConstantStringArrayContainsTableColumnTest()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();

            const int             n  = 20;
            IEnumerable <TestObj> cq = from i in Enumerable.Range(1, n)
                                       select new TestObj {
                Name = i.ToString(CultureInfo.InvariantCulture)
            };

            db.InsertAll(cq);

            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);
        }
예제 #11
0
        public void QueriedStringArrayContainsTableColumnTest()
        {
            int n = 20;
            IEnumerable <TestObj> cq = from i in Enumerable.Range(1, n) select new TestObj {
                Name = i.ToString()
            };

            var db = new OrmTestSession();

            // temp stop trace for inputs
            SqliteSession.Trace = false;
            db.CreateTable <TestObj>();
            db.InsertAll(cq);
            SqliteSession.Trace = true;

            var tensArray  = new[] { "0", "10", "20" };
            var tensResult = from tens in db.Table <TestObj>()
                             where tensArray.Contains(tens.Name)
                             select tens.Name;
            List <TestObj> more2 = (from all in db.Table <TestObj>()
                                    where tensResult.Contains(all.Name)
                                    select all).ToList();

            Assert.AreEqual(2, more2.Count);
        }
예제 #12
0
        // [Description("Create a table with a nullable int column then insert and select against it")]
        public void NullableFloat()
        {
            var db = new OrmTestSession();

            db.CreateTable <NullableFloatClass>();

            var withNull = new NullableFloatClass {
                NullableFloat = null
            };
            var with0 = new NullableFloatClass {
                NullableFloat = 0
            };
            var with1 = new NullableFloatClass {
                NullableFloat = 1
            };
            var withMinus1 = new NullableFloatClass {
                NullableFloat = -1
            };

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

            NullableFloatClass[] results = db.Table <NullableFloatClass>().OrderBy(x => x.Id).ToArray();

            Assert.AreEqual(4, results.Length);

            Assert.AreEqual(withNull, results[0]);
            Assert.AreEqual(with0, results[1]);
            Assert.AreEqual(with1, results[2]);
            Assert.AreEqual(withMinus1, results[3]);
        }
예제 #13
0
        public void WhereNull()
        {
            var db = new OrmTestSession();

            db.CreateTable <NullableIntClass>();

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

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

            NullableIntClass[] results = db.Table <NullableIntClass>().Where(x => x.NullableInt == null).OrderBy(x => x.Id).ToArray();

            Assert.AreEqual(1, results.Length);
            Assert.AreEqual(withNull, results[0]);
        }
예제 #14
0
        public void InsertUsingInterweavingSavePoints()
        {
            var obj = new TestObj {
                Text = "Matthew"
            };

            using (var db = new OrmTestSession())
            {
                db.CreateTable <TestObj>();

                using (var trans = db.BeginTransaction())
                {
                    db.Insert(obj);
                    Assert.AreEqual(db.Table <TestObj>().Count(), 1);

                    trans.CreateSavepoint("First");
                    db.Insert(obj);
                    Assert.AreEqual(db.Table <TestObj>().Count(), 2);

                    trans.CreateSavepoint("Second");
                    db.Insert(obj);
                    Assert.AreEqual(db.Table <TestObj>().Count(), 3);

                    trans.RollbackSavepoint("First");

                    trans.Commit();
                }

                Assert.AreEqual(db.Table <TestObj>().Count(), 1);
            }
        }
예제 #15
0
        public void NestedTransactionsTest()
        {
            var obj = new TestObj {
                Text = "Matthew"
            };

            using (var db = new OrmTestSession())
            {
                db.CreateTable <TestObj>();

                using (var trans1 = db.BeginTransaction())
                {
                    trans1.CreateSavepoint("First");
                    db.Insert(obj);

                    using (var trans2 = db.BeginTransaction())
                    {
                        // rollback on the second trans affects the first as
                        // it is the same transaction
                        trans2.RollbackSavepoint("First");
                    }

                    Assert.AreEqual(db.Table <TestObj>().Count(), 0);
                }
            }
        }
예제 #16
0
        public void InsertUsingNestedSystemTransactions()
        {
            var options = new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            };

            using (var db = new OrmTestSession())
            {
                using (var trans = new TransactionScope(TransactionScopeOption.Required, options))
                {
                    db.CreateTable <TestObj>();
                    db.Insert(new TestObj {
                        Text = "My Text"
                    });

                    using (var trans2 = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        db.Insert(new TestObj {
                            Text = "My Text"
                        });
                        trans2.Complete();
                    }

                    trans.Complete();
                }

                Assert.AreEqual(2, db.Table <TestObj>().Count());
            }
        }
예제 #17
0
        public void StringWhereNotNull()
        {
            var db = new OrmTestSession();

            db.CreateTable <StringClass>();

            var withNull = new StringClass {
                StringData = null
            };
            var withEmpty = new StringClass {
                StringData = ""
            };
            var withData = new StringClass {
                StringData = "data"
            };

            db.Insert(withNull);
            db.Insert(withEmpty);
            db.Insert(withData);

            StringClass[] results = db.Table <StringClass>().Where(x => x.StringData != null).OrderBy(x => x.Id).ToArray();
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual(withEmpty, results[0]);
            Assert.AreEqual(withData, results[1]);
        }
예제 #18
0
        public void TableQueryOnQueryableTable()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable <QueryableTable>();
                db.Insert(new QueryableTable {
                    Name = "sqlite"
                });
                db.Insert(new QueryableTable {
                    Name = "sqlevy"
                });
                db.Insert(new QueryableTable {
                    Name = "cars"
                });
                db.Insert(new QueryableTable {
                    Name = "we know sqlite is cool"
                });
                db.Insert(new QueryableTable {
                    Name = "we think sqlites"
                });

                var count = db.Table <QueryableTable>().Where(x => x.Name.Matches("sqlite")).Count();

                Assert.AreEqual(3, count);
            }
        }
예제 #19
0
        public void OrderByCast()
        {
            var db = new OrmTestSession();

            db.CreateTable <CoolTable>();

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

            var nocast = (from p in db.Table <CoolTable>()
                          orderby p.Price descending
                          select p).ToList();

            Assert.AreEqual(2, nocast.Count);
            Assert.AreEqual("B", nocast[0].Name);

            var cast = (from p in db.Table <CoolTable>()
                        orderby(int) p.Price descending
                        select p).ToList();

            Assert.AreEqual(2, cast.Count);
            Assert.AreEqual("B", cast[0].Name);
        }
예제 #20
0
        public void InsertTwoTimes()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();

            var obj1 = new TestObj {
                Text = "GLaDOS loves testing!"
            };
            var obj2 = new TestObj {
                Text = "Keep testing, just keep testing"
            };

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

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

            List <TestObj> 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);

            db.Close();
        }
예제 #21
0
        public void InsertWithExtra()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj2>();

            var obj1 = new TestObj2 {
                Id = 1, Text = "GLaDOS loves testing!"
            };
            var obj2 = new TestObj2 {
                Id = 1, Text = "Keep testing, just keep testing"
            };
            var obj3 = new TestObj2 {
                Id = 1, Text = "Done testing"
            };

            db.Insert(obj1);

            // "Expected unique constraint violation"
            ExceptionAssert.Throws <SqliteException>(() => db.Insert(obj2));

            db.Insert(obj2, ConflictResolution.Replace);

            // "Expected unique constraint violation"
            ExceptionAssert.Throws <SqliteException>(() => db.Insert(obj3));

            db.Insert(obj3, ConflictResolution.Ignore);

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

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(obj2.Text, result.First().Text);

            db.Close();
        }
        public void Skip()
        {
            var db = new OrmTestSession();
			db.CreateTable<TestObj>();
            const int n = 100;

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

            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);
        }
        public void ClearTableTest()
        {
            // setup
            var db = new OrmTestSession();
            db.CreateTable<Product>();

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

            // confirm
            Assert.AreEqual(2, db.Table<Product>().Count());
            db.Get<Product>(1);

            // clear
            Assert.AreEqual(2, db.ClearTable<Product>());

            // confirm
            Assert.AreEqual(0, db.Table<Product>().Count());

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

            // confirm that the Ids have not reset
            Assert.AreEqual(1, db.Table<Product>().Count());
            db.Get<Product>(3);
        }
예제 #24
0
        public void InsertALotPlain()
        {
            var db = new OrmTestSession();

            SqliteSession.Trace = false;

            db.CreateTable <TestObjPlain>();

            const int n = 10000;
            IEnumerable <TestObjPlain> q = Enumerable.Range(1, n).Select(i => new TestObjPlain {
                Text = "I am"
            });

            TestObjPlain[] objs  = q.ToArray();
            int            numIn = db.InsertAll(objs);

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

            TestObjPlain[] inObjs = db.Table <TestObjPlain>().ToArray();

            foreach (TestObjPlain t in inObjs)
            {
                Assert.AreEqual("I am", t.Text);
            }

            int numCount = db.Table <TestObjPlain>().Count();

            Assert.AreEqual(numCount, n, "Num counted must = num objects");

            db.Close();
        }
        public void CreateInsertDrop()
        {
            var db = new OrmTestSession();

            db.CreateTable <Product>();

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

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

            Assert.AreEqual(1, n);

            db.DropTable <Product>();

            try
            {
                // Should throw SqliteException
                db.Table <Product>().Count();

                Assert.Fail("Expeced 'table does not exist' error.");
            }
            catch (SqliteException)
            {
            }
            catch
            {
                Assert.Fail();
            }
        }
예제 #26
0
        public void ClearTableTest()
        {
            // setup
            var db = new OrmTestSession();

            db.CreateTable <Product>();

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

            // confirm
            Assert.AreEqual(2, db.Table <Product>().Count());
            db.Get <Product>(1);

            // clear
            Assert.AreEqual(2, db.ClearTable <Product>());

            // confirm
            Assert.AreEqual(0, db.Table <Product>().Count());

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

            // confirm that the Ids have not reset
            Assert.AreEqual(1, db.Table <Product>().Count());
            db.Get <Product>(3);
        }
 public void CreateUnknownColumnType()
 {
     using (var db = new OrmTestSession())
     {
         ExceptionAssert.Throws<NotSupportedException>(() => db.CreateTable<UnknownColumnType>());
     }
 }
예제 #28
0
        public void TestBoolean()
        {
            var db = new OrmTestSession();

            db.CreateTable <Vo>();

            for (int i = 0; i < 10; i++)
            {
                db.Insert(new Vo {
                    Flag = (i % 3 == 0), Text = String.Format("VO{0}", i)
                });
            }

            // count vo which flag is true
            Assert.AreEqual(4, CountWithFlag(db, true));
            Assert.AreEqual(6, CountWithFlag(db, false));

            Debug.WriteLine("VO with true flag:");
            foreach (Vo vo in db.Query <Vo>("SELECT * FROM VO Where Flag = ?", true))
            {
                Debug.WriteLine(vo.ToString());
            }

            Debug.WriteLine("VO with false flag:");
            foreach (Vo vo in db.Query <Vo>("SELECT * FROM VO Where Flag = ?", false))
            {
                Debug.WriteLine(vo.ToString());
            }
        }
        public void Collate()
        {
            var obj = new TestObj
            {
                CollateDefault = "Alpha ",
                CollateBinary  = "Alpha ",
                CollateRTrim   = "Alpha ",
                CollateNoCase  = "Alpha ",
            };

            var db = new OrmTestSession();

            db.CreateTable <TestObj>();
            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());
        }
예제 #30
0
        public void StringQueryOnQueryableTable()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable <QueryableTable>();
                db.Insert(new QueryableTable {
                    Name = "sqlite"
                });
                db.Insert(new QueryableTable {
                    Name = "sqlevy"
                });
                db.Insert(new QueryableTable {
                    Name = "cars"
                });
                db.Insert(new QueryableTable {
                    Name = "we know sqlite is cool"
                });
                db.Insert(new QueryableTable {
                    Name = "we think sqlites"
                });

                var count = db.ExecuteScalar <int>(
                    "SELECT COUNT(*) FROM [QueryableTable] WHERE [Name] MATCH ?", "sqlite");

                Assert.AreEqual(3, count);
            }
        }
예제 #31
0
        // [Description("Create A large byte array and check it can be stored and retrieved correctly")]
        public void LargeByteArrayTest()
        {
            const int byteArraySize = 1024 * 1024;
            var       bytes         = new byte[byteArraySize];

            for (int i = 0; i < byteArraySize; i++)
            {
                bytes[i] = (byte)(i % 256);
            }

            var byteArray = new ByteArrayClass {
                Bytes = bytes
            };

            var database = new OrmTestSession();

            database.CreateTable <ByteArrayClass>();

            //Insert the ByteArrayClass
            database.Insert(byteArray);

            //Get it back out
            ByteArrayClass[] fetchedByteArrays = database.Table <ByteArrayClass>().ToArray();

            Assert.AreEqual(fetchedByteArrays.Length, 1);

            //Check they are the same
            byteArray.AssertEquals(fetchedByteArrays[0]);
        }
        public void TestBoolean()
        {
            var db = new OrmTestSession();
            db.CreateTable<Vo>();

            for (int i = 0; i < 10; i++)
            {
                db.Insert(new Vo {Flag = (i%3 == 0), Text = String.Format("VO{0}", i)});
            }

            // count vo which flag is true            
            Assert.AreEqual(4, CountWithFlag(db, true));
            Assert.AreEqual(6, CountWithFlag(db, false));

            Debug.WriteLine("VO with true flag:");
            foreach (Vo vo in db.Query<Vo>("SELECT * FROM VO Where Flag = ?", true))
            {
                Debug.WriteLine(vo.ToString());
            }

            Debug.WriteLine("VO with false flag:");
            foreach (Vo vo in db.Query<Vo>("SELECT * FROM VO Where Flag = ?", false))
            {
                Debug.WriteLine(vo.ToString());
            }
        }
예제 #33
0
        public void Collate()
        {
            var obj = new TestObj
                          {
                              CollateDefault = "Alpha ",
                              CollateBinary = "Alpha ",
                              CollateRTrim = "Alpha ",
                              CollateNoCase = "Alpha ",
                          };

            var db = new OrmTestSession();
            db.CreateTable<TestObj>();
            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());
        }
예제 #34
0
        public void InsertALot()
        {
            var db = new OrmTestSession();

            SqliteSession.Trace = false;

            db.CreateTable <TestObj>();

            const int             n = 10000;
            IEnumerable <TestObj> q = Enumerable.Range(1, n).Select(i => new TestObj {
                Text = "I am"
            });

            TestObj[] objs  = q.ToArray();
            int       numIn = db.InsertAll(objs);

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

            TestObj[] inObjs = db.Table <TestObj>().ToArray();

            for (int i = 0; i < inObjs.Length; i++)
            {
                Assert.AreEqual(i + 1, objs[i].Id);
                Assert.AreEqual(i + 1, inObjs[i].Id);
                Assert.AreEqual("I am", inObjs[i].Text);
            }

            int numCount = db.Table <TestObj>().Count();

            Assert.AreEqual(numCount, n, "Num counted must = num objects");

            db.Close();
        }
예제 #35
0
        public void CanCompareAnyField()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObjString>();
            db.Insert(new TestObjString
            {
                Data = Convert.ToString(3),
                Date = new DateTime(2013, 1, 3)
            });

            var results = db.Table <TestObjString>().Where(o => o.Data.Equals("3"));

            Assert.AreEqual(1, results.Count());
            Assert.AreEqual("3", results.FirstOrDefault().Data);

            results = db.Table <TestObjString>().Where(o => o.Id.Equals(1));
            Assert.AreEqual(1, results.Count());
            Assert.AreEqual("3", results.FirstOrDefault().Data);

            var date = new DateTime(2013, 1, 3);

            results = db.Table <TestObjString>().Where(o => o.Date.Equals(date));
            Assert.AreEqual(1, results.Count());
            Assert.AreEqual("3", results.FirstOrDefault().Data);
        }
예제 #36
0
        public void BitwiseAndTest()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable <BitwiseTable>();

                db.Insert(new BitwiseTable {
                    Flags = 0
                });
                db.Insert(new BitwiseTable {
                    Flags = 1
                });
                db.Insert(new BitwiseTable {
                    Flags = 2
                });
                db.Insert(new BitwiseTable {
                    Flags = 2 | 1
                });

                var bit = db.Table <BitwiseTable>().Where(x => (x.Flags & 2) != 0).Select(x => x.Flags).ToArray();
                ArrayAssert.AreEqual(new[] { 2, 3 }, bit);

                bit = db.Table <BitwiseTable>().Where(x => (x.Flags & 1) != 0).Select(x => x.Flags).ToArray();
                ArrayAssert.AreEqual(new[] { 1, 3 }, bit);

                bit = db.Table <BitwiseTable>().Where(x => (x.Flags & 4) != 0).Select(x => x.Flags).ToArray();
                ArrayAssert.AreEqual(new int[] { }, bit);
            }
        }
 public void DropTableUsingSqlString()
 {
     var db = new OrmTestSession();
     
     db.CreateTable<Product>();
     
     var results = db.Execute("DROP TABLE IF EXISTS Product;");
 }
 public void EnsureSimpleTableExistsUsingTableExists()
 {
     using (var db = new OrmTestSession())
     {
         db.CreateTable<SimpleTable>();
         Assert.IsTrue(db.TableExists<SimpleTable>());
     }
 }
 public void EnsureSimpleTableExistsUsingQuery()
 {
     using (var db = new OrmTestSession())
     {
         db.CreateTable<SimpleTable>();
         Assert.AreEqual(0, db.ExecuteScalar<int>("SELECT COUNT(*) FROM [SimpleTable];"));
     }
 }
 public void IntegrityCheckTest()
 {
     using (var db = new OrmTestSession())
     {
         db.CreateTable<SimpleTable>();
         db.Insert(new SimpleTable { Name = RandomString() });
         db.IntegrityCheck<SimpleTable>();
     }
 }
예제 #41
0
 public void CreateInstanceHookTestNonGeneric()
 {
     var db = new OrmTestSession();
     db.InstanceCreated += InstanceCreated;
     db.CreateTable<HookTestTable>();
     db.Insert(new HookTestTable { Text = InsertedTest });
     var got = db.Get(typeof(HookTestTable), 1);
     Assert.AreEqual(ReplacedText, ((HookTestTable)got).Text);
 }
 public void MergeTest()
 {
     using (var db = new OrmTestSession())
     {
         db.CreateTable<SimpleTable>();
         db.Insert(new SimpleTable { Name = RandomString() });
         db.Merge<SimpleTable>();
     }
 }
예제 #43
0
 public void CreateInstanceHookTest()
 {
     var db = new OrmTestSession();
     db.InstanceCreated += InstanceCreated;
     db.CreateTable<HookTestTable>();
     db.Insert(new HookTestTable { Text = InsertedTest });
     var got = db.Get<HookTestTable>(1);
     Assert.AreEqual(ReplacedText, got.Text);
 }
 public void RunMergeUntilOptimalTest()
 {
     using (var db = new OrmTestSession())
     {
         db.CreateTable<SimpleTable>();
         db.Insert(new SimpleTable { Name = RandomString() });
         db.RunMergeUntilOptimal<SimpleTable>();
     }
 }
        public void CreateThem()
        {
            var db = new OrmTestSession();
            db.CreateTable<Product>();
            db.CreateTable<OrderLine>();

            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.OrderLines;

            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");
        }
예제 #46
0
        public void Insert()
        {
            var db = new OrmTestSession();
            db.CreateTable<Product>();

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

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

            Assert.AreEqual(TestString, p.Name);
        }
예제 #47
0
        public void Query()
        {
            var db = new OrmTestSession();
            db.CreateTable<Product>();

            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);
        }
        public void InterfaceTest()
        {
            var db = new OrmTestSession();

            db.CreateTable<Product>();

            var obj1 = new Product { Name = "Some Cool Name" };

            int numIn1 = db.Insert(obj1);
            Assert.AreEqual(1, numIn1);

            db.Close();
        }
        public void ColumnsSaveLoadMaxCorrectly()
        {
            var db = new OrmTestSession();
            db.CreateTable<Test>();

            var test = new Test
                           {
                               Id = 0,
                               Int32 = Int32.MaxValue,
                               String = "A unicode string \u2022 <- bullet point",
                               Byte = Byte.MaxValue,
                               UInt16 = UInt16.MaxValue,
                               SByte = SByte.MaxValue,
                               Int16 = Int16.MaxValue,
                               Boolean = true,
                               UInt32 = UInt32.MaxValue,
                               Int64 = Int64.MaxValue,
                               Single = Single.MaxValue,
                               Double = Double.MaxValue,
                               Decimal = 79228162514264300000000000000M,
                               Enum1 = EEnum.EnumVal1,
                               Enum2 = EEnum.EnumVal2,
                               Timestamp = DateTime.Parse("2012-04-05 15:08:24.723"),
                               Blob = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
                               Guid = Guid.NewGuid(),
                               TimeSpan = TimeSpan.MaxValue
                           };

            db.Insert(test);
            var res = db.Get<Test>(test.Id);

            Assert.AreEqual(test.Id, res.Id);
            Assert.AreEqual(test.Int32, res.Int32);
            Assert.AreEqual(test.String, res.String);
            Assert.AreEqual(test.Byte, res.Byte);
            Assert.AreEqual(test.UInt16, res.UInt16);
            Assert.AreEqual(test.SByte, res.SByte);
            Assert.AreEqual(test.Int16, res.Int16);
            Assert.AreEqual(test.Boolean, res.Boolean);
            Assert.AreEqual(test.UInt32, res.UInt32);
            Assert.AreEqual(test.Int64, res.Int64);
            Assert.AreEqual(test.Single, res.Single);
            Assert.AreEqual(test.Double, res.Double);
            Assert.AreEqual(test.Decimal, res.Decimal);
            Assert.AreEqual(test.Enum1, res.Enum1);
            Assert.AreEqual(test.Enum2, res.Enum2);
            Assert.AreEqual(test.Timestamp, res.Timestamp);
            ArrayAssert.AreEqual(test.Blob, res.Blob);
            Assert.AreEqual(test.Guid, res.Guid);
            Assert.AreEqual(test.TimeSpan, res.TimeSpan);
        }
        public void StringContains()
        {
            var db = new OrmTestSession();
            db.CreateTable<TestObj>();

            var testObj = new TestObj { Name = "This is a Good name" };
            db.Insert(testObj);

            var stringContainsTest = (from n in db.Table<TestObj>() where n.Name.Contains("Good") select n).Single();
            Assert.AreEqual(testObj.Id, stringContainsTest.Id);
            var finder = "good";
            stringContainsTest = (from n in db.Table<TestObj>() where n.Name.Contains(finder) select n).Single();
            Assert.AreEqual(testObj.Id, stringContainsTest.Id);
        }
        public void StringStartsWith()
        {
            var db = new OrmTestSession();
            db.CreateTable<TestObj>();

            var testObj = new TestObj { Name = "This is a Good name" };
            db.Insert(testObj);

            var stringContainsTest = (from n in db.Table<TestObj>() where n.Name.StartsWith("This") select n).Single();
            Assert.AreEqual(testObj.Id, stringContainsTest.Id);
            var finder = "name";
            stringContainsTest = (from n in db.Table<TestObj>() where n.Name.StartsWith(finder) select n).SingleOrDefault();
            Assert.IsNull(stringContainsTest);
        }
            public CreateTest(OrmTestSession db)
            {
                db.CreateTable<OrderLine>();

                var orderLine = db.GetMapping<OrderLine>();
                Assert.AreEqual(4, orderLine.Columns.Count, "OrderLine has 4 columns");

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

                Id = lo.Id;
            }
        public void CreateUnknownColumnType()
        {
            using (var db = new OrmTestSession())
            {
                try
                {
                    db.CreateTable<UnknownColumnType>();

                    Assert.Fail();
                }
                catch (NotSupportedException)
                {
                }
            }
        }
예제 #54
0
        public void NamedCompositePrimaryKeyTableTest()
        {
            var db = new OrmTestSession();
            db.CreateTable<NamedCompositePrimaryKeyTable>();

            var sql = new TableMapping(typeof(NamedCompositePrimaryKeyTable)).GetCreateSql();
            var correct = @"CREATE TABLE [NamedCompositePrimaryKeyTable] (
[FirstId] integer NOT NULL,
[SecondId] integer NOT NULL,
CONSTRAINT PK_Alfred
PRIMARY KEY ([FirstId], [SecondId])
);";

            Assert.AreEqual(correct, sql);
        }
        public void EmptyByteArraySavedAndRetrievedCorrectlyTest()
        {
            //Empty byte array should stay empty (and not become null)
            var byteArray = new ByteArrayClass { Bytes = new byte[] { } };

            var database = new OrmTestSession();
            database.CreateTable<ByteArrayClass>();

            database.Insert(byteArray);

            //Get them back out
            var fetchedByteArray = database.Table<ByteArrayClass>().Single();

            //Check they are the same
            ArrayAssert.AreEqual(byteArray.Bytes, fetchedByteArray.Bytes);
        }
            public MigrateTest(OrmTestSession db)
            {
                db.CreateTable<OrderDetails>();

                var orderLine = db.GetMapping<OrderDetails>();
                Assert.AreEqual(6, orderLine.Columns.Count, "OrderDetails (OrderLine) has 6 columns");

                OrderDetails l = db.Table<OrderDetails>().First(x => x.ShippingStatus == OrderLineStatus.Shipped);
                l.Tax = 12;
                db.Update(l);

                OrderDetails lo = db.Table<OrderDetails>().First(x => x.Tax == 12);
                Assert.AreEqual(lo.Id, l.Id);

                Id = lo.Id;
            }
        public void CreateInsertDrop()
        {
            var db = new OrmTestSession();

            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());
        }
        public void CreateSimpleTable()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable<SimpleTable>();

                var sql = new TableMapping(typeof(SimpleTable)).GetCreateSql();
                var correct = @"CREATE TABLE [SimpleTable] (
[Id] integer NOT NULL,
[Name] text ,
[BigInt] bigint NOT NULL,
[IsWorking] integer NOT NULL
);";

                Assert.AreEqual(correct, sql);
            }
        }
        public void StringQueryOnQueryableTable()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable<QueryableTable>();
                db.Insert(new QueryableTable { Name = "sqlite" });
                db.Insert(new QueryableTable { Name = "sqlevy" });
                db.Insert(new QueryableTable { Name = "cars" });
                db.Insert(new QueryableTable { Name = "we know sqlite is cool" });
                db.Insert(new QueryableTable { Name = "we think sqlites" });

                var count = db.ExecuteScalar<int>(
                    "SELECT COUNT(*) FROM [QueryableTable] WHERE [Name] MATCH ?", 
                    "sqlite");

                Assert.AreEqual(3, count);
            }
        }
예제 #60
0
        public void SqlQuery()
        {
            var expected = "абвг";

            var db = new OrmTestSession();
            db.CreateTable<Product>();

            var product = new Product { Name = TestString };
            db.Insert(product);

            db.Execute("UPDATE Product SET Name = '" + expected + "' WHERE Id = " + product.Id);

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

            Assert.AreEqual(1, ps.Count);
            Assert.AreEqual(expected, ps[0].Name);
        }