コード例 #1
0
ファイル: SqlCeDataStoreTest.cs プロジェクト: lordofffarm/orm
        private void TestCascadingUpdates(SqlCeDataStore store)
        {
            var author = new Author
            {
                Name = "Theodore Geisel"
            };

            store.Insert(author);

            var book = new Book
            {
                BookType = BookType.Fiction,
                Title    = "Fox in Sox"
            };

            author.Books = new Book[] { book };

            store.Update(author);

            var existing = store.Select <Author>(author.AuthorID, true);

            Assert.AreEqual(1, existing.Books.Length);
            Assert.AreEqual("Fox in Sox", existing.Books[0].Title);

            // replace the book title in the author's collection
            existing.Books[0].Title = "Green Eggs and Ham";

            // now Update the Author - this should cascade update the book title
            store.Update(existing);

            existing = store.Select <Author>(author.AuthorID, true);
            Assert.AreEqual("Green Eggs and Ham", existing.Books[0].Title);
        }
コード例 #2
0
        public void SimpleCRUDTest()
        {
            var store = new SqlCeDataStore("test.sdf");

            store.AddType <TestItem>();
            store.CreateStore();

            var itemA = new TestItem("ItemA");
            var itemB = new TestItem("ItemB");
            var itemC = new TestItem("ItemC");

            // INSERT
            store.Insert(itemA);
            store.Insert(itemB);
            store.Insert(itemC);

            // COUNT
            var count = store.Count <TestItem>();

            Assert.AreEqual(3, count);

            // SELECT
            var item = store.Select <TestItem>("Name", itemB.Name).FirstOrDefault();

            Assert.IsTrue(item.Equals(itemB));

            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Equals(itemC));

            // FETCH

            // UPDATE
            itemC.Name    = "NewItem";
            itemC.Address = "Changed Address";
            itemC.TS      = new TimeSpan(8, 23, 30);
            store.Update(itemC);

            item = store.Select <TestItem>("Name", "ItemC").FirstOrDefault();
            Assert.IsNull(item);
            item = store.Select <TestItem>("Name", itemC.Name).FirstOrDefault();
            Assert.IsTrue(item.Equals(itemC));

            // CONTAINS
            var exists = store.Contains(itemA);

            Assert.IsTrue(exists);

            // DELETE
            store.Delete(itemA, false);
            item = store.Select <TestItem>("Name", itemA.Name).FirstOrDefault();
            Assert.IsNull(item);

            // CONTAINS
            exists = store.Contains(itemA);
            Assert.IsFalse(exists);

            // COUNT
            count = store.Count <TestItem>();
            Assert.AreEqual(2, count);
        }
コード例 #3
0
        public void AddOrderToCustomer(Customer customer, ProductOrder order)
        {
            var list = new List <ProductOrder>();

            if (customer.Orders != null)
            {
                list.AddRange(customer.Orders);
            }
            if (order.OrderID == 0)
            {
                // insert the order?
            }
            list.Add(order);
            customer.Orders = list.ToArray();
            m_store.Update(customer);
        }
コード例 #4
0
ファイル: SqlCeDataStoreTest.cs プロジェクト: lordofffarm/orm
        public void DateTimeTest()
        {
            m_store.TruncateTable("TestItem");

            var itemA = new TestItem("ItemA");

            m_store.Insert(itemA);

            var checkItem = m_store.Select <TestItem>().FirstOrDefault();

            Assert.AreEqual(itemA.TestDate, checkItem.TestDate);

            var now = DateTime.Now;

            itemA.TestDate = now;

            m_store.Update(itemA);
            checkItem = m_store.Select <TestItem>().FirstOrDefault();
            Assert.AreEqual(itemA.TestDate.Second, checkItem.TestDate.Second);
        }
コード例 #5
0
ファイル: SqlCeDataStoreTest.cs プロジェクト: lordofffarm/orm
        public void TestEnumCRUD(SqlCeDataStore store)
        {
            // truncate the table for this test
            store.Delete <TestTable>();

            var testRow = new TestTable
            {
                EnumField = TestEnum.ValueB
            };

            store.Insert(testRow);

            var existing = store.Select <TestTable>().First();

            Assert.AreEqual(existing.EnumField, testRow.EnumField);

            existing.EnumField = TestEnum.ValueC;
            store.Update(existing);
            var secondPull = store.Select <TestTable>().First();

            Assert.AreEqual(existing.EnumField, secondPull.EnumField);
        }
コード例 #6
0
ファイル: SqlCeDataStoreTest.cs プロジェクト: lordofffarm/orm
        public void SimpleCRUDTest()
        {
            bool beforeInsert = false;
            bool afterInsert  = false;
            bool beforeUpdate = false;
            bool afterUpdate  = false;
            bool beforeDelete = false;
            bool afterDelete  = false;

            var store = new SqlCeDataStore("simpleCRUDTest.sdf");

            store.AddType <TestItem>();
            store.CreateStore();

            store.BeforeInsert += delegate
            {
                beforeInsert = true;
            };
            store.AfterInsert += delegate
            {
                afterInsert = true;
            };
            store.BeforeUpdate += delegate
            {
                beforeUpdate = true;
            };
            store.AfterUpdate += delegate
            {
                afterUpdate = true;
            };
            store.BeforeDelete += delegate
            {
                beforeDelete = true;
            };
            store.AfterDelete += delegate
            {
                afterDelete = true;
            };

            var itemA = new TestItem("ItemA");

            itemA.UUID   = Guid.NewGuid();
            itemA.ITest  = 5;
            itemA.FTest  = 3.14F;
            itemA.DBTest = 1.4D;
            itemA.DETest = 2.678M;

            var itemB = new TestItem("ItemB");
            var itemC = new TestItem("ItemC");

            // INSERT
            store.Insert(itemA);
            Assert.IsTrue(beforeInsert, "BeforeInsert never fired");
            Assert.IsTrue(afterInsert, "AfterInsert never fired");

            store.Insert(itemB);
            store.Insert(itemC);

            // COUNT
            var count = store.Count <TestItem>();

            Assert.AreEqual(3, count);

            // SELECT
            var items = store.Select <TestItem>();

            Assert.AreEqual(3, items.Count());

            var item = store.Select <TestItem>("Name", itemB.Name).FirstOrDefault();

            Assert.IsTrue(item.Equals(itemB));

            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Equals(itemC));

            // FETCH

            // UPDATE
            itemC.Name      = "NewItem";
            itemC.Address   = "Changed Address";
            itemC.TS        = new TimeSpan(8, 23, 30);
            itemC.BigString = "little string";

            // test rollback
            store.BeginTransaction();
            store.Update(itemC);
            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Name == itemC.Name);
            store.Rollback();

            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Name != itemC.Name);

            // test commit
            store.BeginTransaction(System.Data.IsolationLevel.Unspecified);
            store.Update(itemC);
            store.Commit();

            Assert.IsTrue(beforeUpdate, "BeforeUpdate never fired");
            Assert.IsTrue(afterUpdate, "AfterUpdate never fired");

            item = store.Select <TestItem>("Name", "ItemC").FirstOrDefault();
            Assert.IsNull(item);
            item = store.Select <TestItem>("Name", itemC.Name).FirstOrDefault();
            Assert.IsTrue(item.Equals(itemC));

            // CONTAINS
            var exists = store.Contains(itemA);

            Assert.IsTrue(exists);

            // DELETE
            store.Delete(itemA);
            Assert.IsTrue(beforeDelete, "BeforeDelete never fired");
            Assert.IsTrue(afterDelete, "AfterDelete never fired");
            item = store.Select <TestItem>("Name", itemA.Name).FirstOrDefault();
            Assert.IsNull(item);

            // CONTAINS
            exists = store.Contains(itemA);
            Assert.IsFalse(exists);

            // COUNT
            count = store.Count <TestItem>();
            Assert.AreEqual(2, count);

            // this will create the table in newer versions of ORM
            store.AddType <LateAddItem>();

            var newitems = store.Select <LateAddItem>(false);

            Assert.IsNotNull(newitems);
        }
コード例 #7
0
ファイル: DynamicEntityTest.cs プロジェクト: lordofffarm/orm
        public void BasicDynamicCRUDTest()
        {
            var fieldList = new List <FieldAttribute>();

            fieldList.Add(new FieldAttribute()
            {
                FieldName    = "ID",
                IsPrimaryKey = true,
                DataType     = System.Data.DbType.Int32
            });

            fieldList.Add(new FieldAttribute()
            {
                FieldName = "FirstName",
                DataType  = System.Data.DbType.String
            });

            fieldList.Add(new FieldAttribute()
            {
                FieldName   = "LastName",
                DataType    = System.Data.DbType.String,
                AllowsNulls = false
            });

            var definition = new DynamicEntityDefinition("People", fieldList, KeyScheme.Identity);

            var store = new SqlCeDataStore(Path.Combine(TestContext.TestDir, "test.sdf"));

            store.CreateStore();

            Assert.IsFalse(store.TableExists(definition.EntityName));

            store.RegisterDynamicEntity(definition);

            Assert.IsTrue(store.TableExists(definition.EntityName));

            var entity = new DynamicEntity("People");

            entity.Fields["FirstName"] = "John";
            entity.Fields["LastName"]  = "Doe";
            store.Insert(entity);

            entity = new DynamicEntity("People");
            entity.Fields["FirstName"] = "Jim";
            entity.Fields["LastName"]  = "Smith";
            store.Insert(entity);

            entity = new DynamicEntity("People");
            entity.Fields["FirstName"] = "Sam";
            entity.Fields["LastName"]  = "Adams";
            store.Insert(entity);

            var items = store.Select("People");

            DumpData(items);

            store.Delete("People", items.First().Fields["ID"]);

            items = store.Select("People");
            DumpData(items);

            store.Delete("People", "LastName", "Smith");

            items = store.Select("People");
            DumpData(items);

            var person = items.First();

            person.Fields["FirstName"] = "Joe";
            person.Fields["LastName"]  = "Satriani";
            store.Update(person);

            items = store.Select("People");
            DumpData(items);

            // now let's change the structure and see what happens
            fieldList.Add(new FieldAttribute()
            {
                FieldName   = "Middle_Name",
                DataType    = System.Data.DbType.Double,
                AllowsNulls = true // this has to be true to add a column
            });

            var newDefinition = new DynamicEntityDefinition("People", fieldList, KeyScheme.Identity);

            store.RegisterDynamicEntity(newDefinition, true);

            items = store.Select("People");
        }