Beispiel #1
0
        public void InsertObjectWithValueInPrimaryKey()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj2>();

            var obj2 = new TestObj2 {
                Id = 5, Text = "Old"
            };

            int numIn1 = db.Insert(obj2);

            Assert.AreEqual(1, numIn1);

            var result = db.Table <TestObj2>().Single(i => i.Id == 5);

            Assert.AreEqual("Old", result.Text);

            result.Text = "New";
            var upd = db.Update(result);

            result = db.Table <TestObj2>().Single(i => i.Id == 5);
            Assert.AreEqual("New", result.Text);

            db.Close();
        }
Beispiel #2
0
        public void CheckInertAndUpdateTwiceWorks()
        {
            var db = new OrmTestSession();

            db.CreateTable <TestObj>();

            var first = new TestObj {
                Text = "First"
            };

            db.Insert(first);

            first.Text = "Second";
            db.Update(first);

            first.Text = "Third";
            db.Update(first);

            Assert.AreEqual("Third", db.Table <TestObj>().Single().Text);
        }
            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;
            }
Beispiel #4
0
        public void StringPrimaryKeys()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable <StringKeyObject>();

                // insert 2 items
                db.Insert(new StringKeyObject {
                    Key = "Name", Value = "Matthew"
                });
                db.Insert(new StringKeyObject {
                    Key = "Age", Value = "19"
                });

                // see if they saved
                Assert.AreEqual(2, db.Table <StringKeyObject>().Count());

                // get the age
                var fromDb = db.Table <StringKeyObject>().Where(x => x.Key == "Age").Single();

                // make sure they are correct
                Assert.AreEqual("Age", fromDb.Key);
                Assert.AreEqual("19", fromDb.Value);

                // try updating
                db.Update(new StringKeyObject {
                    Key = "Name", Value = "Matthew Leibowitz"
                });

                // make sure it wasn't an add
                Assert.AreEqual(2, db.Table <StringKeyObject>().Count());

                // get the name
                fromDb = db.Table <StringKeyObject>().Where(x => x.Key == "Name").Single();

                // make sure the name has changed
                Assert.AreEqual("Matthew Leibowitz", fromDb.Value);

                // try a delete
                db.Delete(new StringKeyObject {
                    Key = "Age"
                });

                // ensure it was really deleted
                Assert.AreEqual(1, db.Table <StringKeyObject>().Count());
            }
        }
Beispiel #5
0
            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 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);
		}
        public void StringPrimaryKeys()
        {
            using (var db = new OrmTestSession())
            {
                db.CreateTable<StringKeyObject>();

                // insert 2 items
                db.Insert(new StringKeyObject { Key = "Name", Value = "Matthew" });
                db.Insert(new StringKeyObject { Key = "Age", Value = "19" });

                // see if they saved
                Assert.AreEqual(2, db.Table<StringKeyObject>().Count());

                // get the age
                var fromDb = db.Table<StringKeyObject>().Where(x => x.Key == "Age").Single();

                // make sure they are correct
                Assert.AreEqual("Age", fromDb.Key);
                Assert.AreEqual("19", fromDb.Value);

                // try updating
                db.Update(new StringKeyObject { Key = "Name", Value = "Matthew Leibowitz" });

                // make sure it wasn't an add
                Assert.AreEqual(2, db.Table<StringKeyObject>().Count());

                // get the name
                fromDb = db.Table<StringKeyObject>().Where(x => x.Key == "Name").Single();

                // make sure the name has changed
                Assert.AreEqual("Matthew Leibowitz", fromDb.Value);

                // try a delete
                db.Delete(new StringKeyObject { Key = "Age" });

                // ensure it was really deleted
                Assert.AreEqual(1, db.Table<StringKeyObject>().Count());
            }
        }
        public void CheckInertAndUpdateTwiceWorks()
        {
            var db = new OrmTestSession();
            db.CreateTable<TestObj>();

            var first = new TestObj { Text = "First" };

            db.Insert(first);
            
            first.Text = "Second";
            db.Update(first);

            first.Text = "Third";
            db.Update(first);

            Assert.AreEqual("Third", db.Table<TestObj>().Single().Text);
        }
        public void InsertObjectWithValueInPrimaryKey()
        {
            var db = new OrmTestSession();
            db.CreateTable<TestObj2>();

            var obj2 = new TestObj2 {Id = 5, Text = "Old"};

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

            var result = db.Table<TestObj2>().Single(i => i.Id == 5);
            Assert.AreEqual("Old", result.Text);

            result.Text = "New";
            var upd = db.Update(result);

            result = db.Table<TestObj2>().Single(i => i.Id == 5);
            Assert.AreEqual("New", result.Text);

            db.Close();
        }
Beispiel #10
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);
            }
        }