Esempio n. 1
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);
        }
Esempio n. 2
0
        public void BasicLocalReplicationTest()
        {
            var source = new SqlCeDataStore("source.sdf");

            if (!source.StoreExists)
            {
                source.CreateStore();
            }
            source.AddType <TestItem>();

            var destination = new SqlCeDataStore("dest.sdf");

            if (!destination.StoreExists)
            {
                destination.CreateStore();
            }

            // build a replictor to send data to the destiantion store
            var replicator = new Replicator(destination, ReplicationBehavior.ReplicateAndDelete);

            // replication is opt-in, so tell it what type(s) we want to replicate
            replicator.RegisterEntity <TestItem>();

            // add the replicator to the source
            source.Replicators.Add(replicator);

            // watch an event for when data batches go out
            replicator.DataReplicated += delegate
            {
                // get a count
                Debug.WriteLine(string.Format("Sent {0} rows", replicator.GetCount <TestItem>()));
            };

            var rows = 200;

            // put some data in the source
            for (int i = 0; i < rows; i++)
            {
                var item = new TestItem(string.Format("Item {0}", i));
                source.Insert(item);
            }

            int remaining = 0;

            // loop until the source table is empty
            do
            {
                Thread.Sleep(500);
                remaining = source.Count <TestItem>();
            } while(remaining > 0);

            // make sure the destination has all rows
            Assert.AreEqual(rows, destination.Count <TestItem>());
        }
Esempio n. 3
0
        private void TestGetEntityCount(SqlCeDataStore store, int iterations)
        {
            Stopwatch sw = new Stopwatch();

            sw.Reset();

            for (int i = 0; i < iterations; i++)
            {
                sw.Start();

                var count = store.Count <Book>();
                sw.Stop();

                if (i == 0)
                {
                    Debug.WriteLine(string.Format("GetBookCount (pass 1):\t{0} s",
                                                  sw.Elapsed.TotalSeconds));
                    sw.Reset();
                }
            }
            Debug.WriteLine(string.Format("GetBookCount (mean):\t{0} s",
                                          sw.Elapsed.TotalSeconds / (iterations - 1)));
        }
Esempio n. 4
0
        private void TestCascadingInsert(SqlCeDataStore store)
        {
            var testBooks = new Book[]
            {
                new Book
                {
                    Title    = "CSS: The Missing Manual",
                    BookType = BookType.NonFiction
                },

                new Book
                {
                    Title    = "JavaScript: The Missing Manual",
                    BookType = BookType.NonFiction
                },

                new Book
                {
                    Title    = "Dreamweaver: The Missing Manual",
                    BookType = BookType.NonFiction
                },
            };

            // ensures that the entity *and its references* get inserted
            Author a = new Author
            {
                Name = "David McFarland",

                Books = testBooks
            };


            var initialCount = store.Count <Book>();

            // insert, telling ORM to insert references (cascade)
            store.Insert(a, true);

            // pull back to verify
            var author = store.Select <Author>(a.AuthorID, true);
            var count  = store.Count <Book>();

            // we should have inserted 3 new books
            var diff = count - initialCount;

            Assert.IsTrue(diff == 3);


            // create a new author with the same books - the books should *not* get re-inserted - plus one new book
            List <Book> newList = new List <Book>(testBooks);

            newList.Add(
                new Book
            {
                Title    = "My Coauthors Book",
                BookType = BookType.NonFiction
            }
                );

            Author a2 = new Author
            {
                Name = "Test CoAuthor",

                Books = newList.ToArray()
            };

            initialCount = store.Count <Book>();

            // insert, telling ORM to insert references (cascade)
            store.Insert(a2, true);

            author = store.Select <Author>(a.AuthorID, true);
            count  = store.Count <Book>();

            // we should have inserted 1 new book
            diff = count - initialCount;
            Assert.IsTrue(diff == 1);
        }
Esempio n. 5
0
        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);
        }