Exemple #1
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();
        }
Exemple #2
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);
        }
        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);
        }
Exemple #4
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);
        }
        public void ContainsQueriedData()
        {
            int n = 20;
            IEnumerable <Product> cq = from i in Enumerable.Range(1, n)
                                       select new Product
            {
                Name = i.ToString()
            };

            OrmTestSession db = CreateDb();

            db.InsertAll(cq);

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

            Assert.AreEqual(2, tens.Count);

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

            Assert.AreEqual(2, more.Count);

            List <string>  moreq2 = moreq.ToList();
            List <Product> more2  = (from o in db.Table <Product>() where moreq2.Contains(o.Name) select o).ToList();

            Assert.AreEqual(2, more2.Count);
        }
Exemple #6
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();
        }
Exemple #7
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);
        }
Exemple #8
0
        public void NestedTransactionsTest()
        {
            int oneTrans;
            int nestedTrans;

            // one transaction
            using (var db = new OrmTestSession())
            {
                SqliteSession.Trace = false;
                db.CreateTable <TestObj>();

                var start = Environment.TickCount;

                const int             n = 500 * 500;
                IEnumerable <TestObj> q = Enumerable.Range(1, n).Select(x => new TestObj {
                    Text = "I am"
                });
                TestObj[] objs  = q.ToArray();
                int       numIn = db.InsertAll(objs);

                oneTrans = Environment.TickCount - start;
            }

            // nested transactions
            using (var db = new OrmTestSession())
            {
                SqliteSession.Trace = false;
                db.CreateTable <TestObj>();

                var start = Environment.TickCount;

                using (var trans = db.BeginTransaction())
                {
                    for (var i = 0; i < 500; ++i)
                    {
                        const int             n = 500;
                        IEnumerable <TestObj> q = Enumerable.Range(1, n).Select(x => new TestObj {
                            Text = "I am"
                        });
                        TestObj[] objs  = q.ToArray();
                        int       numIn = db.InsertAll(objs);
                    }

                    trans.Commit();
                }

                nestedTrans = Environment.TickCount - start;
            }

            System.Diagnostics.Debug.WriteLine("Single tranasction: " + oneTrans);
            System.Diagnostics.Debug.WriteLine("Nested tranasction: " + nestedTrans);
            System.Diagnostics.Debug.WriteLine("Difference: " + Math.Abs(nestedTrans - oneTrans));

            // this is a really dodgy test to use the execution time...
            Assert.IsTrue(Math.Abs(oneTrans - nestedTrans) <= 100);
        }
        public void Contains()
		{
			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);
		}
        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);
        }
Exemple #11
0
        public void InsertAllWithinATransactionSucceeds()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();

            var first = new TestObj {
                Text = "First"
            };
            var second = new TestObj {
                Text = "Second"
            };
            var third = new TestObj {
                Text = "Third"
            };

            using (var trans = db.BeginTransaction())
            {
                var inObjs = db.InsertAll(new[] { first, second, third }, false);
                trans.Commit();
            }

            Assert.AreEqual(3, db.Table <TestObj>().Count());
        }
        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();
        }
        public void MultiplePkOperations()
		{
			var db = new OrmTestSession();
			db.CreateTable<TestObj>();
			
			// insert
			const int n = 10 , m= 5;
			var objs = new TestObj[n * m];
			for (int j = 0; j != n; ++j)
			{
				for (int i = 0; i != m; ++i)
				{
					objs[j * m + i] = new TestObj {Id = j, SubId = i, Text = "I am (" + j + "," + i + ")"};
				}
			}

			int numIn = db.InsertAll(objs);

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

			var obj = db.Get<TestObj>(5, 3);
			Assert.AreEqual(5, obj.Id);
			Assert.AreEqual(3, obj.SubId);
			Assert.AreEqual("I am (5,3)", obj.Text);
			
			try
			{
				db.Insert(obj);
			}
			catch (SqliteException ex)
			{
                Assert.AreEqual(SQLiteErrorCode.Constraint, ex.ErrorCode);
			}

			// update
			obj.Text = "I've been changed";
			db.Update(obj);
			db.Update<TestObj>("Text", "I've been changed also", 8, 2);

			obj = db.Get<TestObj>(5, 3);
			Assert.AreEqual("I've been changed", obj.Text);

			obj = db.Get<TestObj>(8, 2);
			Assert.AreEqual("I've been changed also", obj.Text);

			db.UpdateAll<TestObj>("Text", "All changed");
			IEnumerable<TestObj> q1 = from o in db.Table<TestObj>() select o;
			foreach (TestObj o in q1)
				Assert.AreEqual("All changed", o.Text);

			TestObj[] q2 = (from o in db.Table<TestObj>() where o.SubId == 3 select o).ToArray();
			Assert.AreEqual(10, q2.Length);
			for (int i = 0; i != 10; ++i)
			{
				Assert.AreEqual(i, q2[i].Id);
			}

            object numCount = db.Table<TestObj>().Count();
			Assert.AreEqual(numCount, objs.Length, "Num counted must = num objects");

			// delete
			obj = db.Get<TestObj>(8, 2);
			db.Delete(obj);
			
			try
			{
				var item = db.Get<TestObj>(8, 2);

				Assert.Fail();
			}
			catch (InvalidOperationException)
			{
			}
			catch (Exception ex)
			{
				Assert.Fail(ex.Message);
			}

            db.Execute("delete from TestObj where SubId=2");
            numCount = db.ExecuteScalar<int>("select count(*) from TestObj");
			Assert.AreEqual(numCount, objs.Length - 10);
			foreach (TestObj o in (from o in db.Table<TestObj>() select o))
				Assert.AreNotEqual(2, o.SubId);
		}
Exemple #14
0
        public void MultiplePkOperations()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();

            // insert
            const int n = 10, m = 5;
            var       objs = new TestObj[n * m];

            for (int j = 0; j != n; ++j)
            {
                for (int i = 0; i != m; ++i)
                {
                    objs[j * m + i] = new TestObj {
                        Id = j, SubId = i, Text = "I am (" + j + "," + i + ")"
                    };
                }
            }

            int numIn = db.InsertAll(objs);

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

            var obj = db.Get <TestObj>(5, 3);

            Assert.AreEqual(5, obj.Id);
            Assert.AreEqual(3, obj.SubId);
            Assert.AreEqual("I am (5,3)", obj.Text);

            try
            {
                db.Insert(obj);
            }
            catch (SqliteException ex)
            {
                Assert.AreEqual(SQLiteErrorCode.Constraint, ex.ErrorCode);
            }

            // update
            obj.Text = "I've been changed";
            db.Update(obj);
            db.Update <TestObj>("Text", "I've been changed also", 8, 2);

            obj = db.Get <TestObj>(5, 3);
            Assert.AreEqual("I've been changed", obj.Text);

            obj = db.Get <TestObj>(8, 2);
            Assert.AreEqual("I've been changed also", obj.Text);

            db.UpdateAll <TestObj>("Text", "All changed");
            IEnumerable <TestObj> q1 = from o in db.Table <TestObj>() select o;

            foreach (TestObj o in q1)
            {
                Assert.AreEqual("All changed", o.Text);
            }

            TestObj[] q2 = (from o in db.Table <TestObj>() where o.SubId == 3 select o).ToArray();
            Assert.AreEqual(10, q2.Length);
            for (int i = 0; i != 10; ++i)
            {
                Assert.AreEqual(i, q2[i].Id);
            }

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

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

            // delete
            obj = db.Get <TestObj>(8, 2);
            db.Delete(obj);

            ExceptionAssert.Throws <InvalidOperationException>(() => db.Get <TestObj>(8, 2));

            db.Execute("delete from TestObj where SubId=2");
            numCount = db.ExecuteScalar <int>("select count(*) from TestObj");
            Assert.AreEqual(numCount, objs.Length - 10);
            foreach (TestObj o in (from o in db.Table <TestObj>() select o))
            {
                Assert.AreNotEqual(2, o.SubId);
            }
        }
        public void NestedTransactionsTest()
        {
            int oneTrans;
            int nestedTrans;

            // one transaction
            using (var db = new OrmTestSession())
            {
                SqliteSession.Trace = false;
                db.CreateTable<TestObj>();

                var start = Environment.TickCount;

                const int n = 500*500;
                IEnumerable<TestObj> q = Enumerable.Range(1, n).Select(x => new TestObj {Text = "I am"});
                TestObj[] objs = q.ToArray();
                int numIn = db.InsertAll(objs);

                oneTrans = Environment.TickCount - start;
            }

            // nested transactions
            using (var db = new OrmTestSession())
            {
                SqliteSession.Trace = false;
                db.CreateTable<TestObj>();

                var start = Environment.TickCount;

                using (var trans = db.BeginTransaction())
                {
                    for (var i = 0; i < 500; ++i)
                    {
                        const int n = 500;
                        IEnumerable<TestObj> q = Enumerable.Range(1, n).Select(x => new TestObj {Text = "I am"});
                        TestObj[] objs = q.ToArray();
                        int numIn = db.InsertAll(objs);
                    }

                    trans.Commit();
                }

                nestedTrans = Environment.TickCount - start;
            }

            System.Diagnostics.Debug.WriteLine("Single tranasction: " + oneTrans);
            System.Diagnostics.Debug.WriteLine("Nested tranasction: " + nestedTrans);
            System.Diagnostics.Debug.WriteLine("Difference: " + Math.Abs(nestedTrans - oneTrans));

            // this is a really dodgy test to use the execution time...
            Assert.IsTrue(Math.Abs(oneTrans - nestedTrans) <= 100);
        }
        public void InsertAllWithinATransactionSucceeds()
        {
            var db = new OrmTestSession();
            db.CreateTable<TestObj>();

            var first = new TestObj { Text = "First" };
            var second = new TestObj { Text = "Second" };
            var third = new TestObj { Text = "Third" };

            using (var trans = db.BeginTransaction())
            {
                var inObjs = db.InsertAll(new[] { first, second, third }, false);
                trans.Commit();
            }

            Assert.AreEqual(3, db.Table<TestObj>().Count());
        }
        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();
        }