Example #1
0
        public void InsertOrUpdateTest()
        {
            var conn = new SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=Budget;Integrated Security=Yes;");
            var rep = new SqlRepository(conn);
            var list = new List<DebitEntry>
                {
                    new DebitEntry { LogTime = new DateTime(2015,1,1), Amount = 50.05m, Description = "Bla bla" }
                  , new DebitEntry { LogTime = new DateTime(2015,1,2), Amount = 150.05m, Description = "11 11 11" }
                };

            int userId = -1;
            try
            {
                // Create a test user
                userId = rep.InsertUser(new User { Name = "Test", Email = "*****@*****.**" });

                var count = rep.CountDebitEntries(userId);
                Assert.AreEqual(count, 0, "There are unexpected DebitEntry records after creating a new user");

                // Insert a banch of debit entries
                count = rep.BatchInsertOrUpdate(userId, list, true);
                Assert.AreEqual(count, list.Count, "Number of inserted records is different from the amount to be inserted");

                count = rep.CountDebitEntries(userId);
                Assert.AreEqual(count, 2, "Total number of records is incorrect");

                // Insert/Update a slightly modified list
                list[1].Description = "11 11 22";
                count = rep.BatchInsertOrUpdate(userId, list, false);
                Assert.AreEqual(count, 1, "Number of inserted records is different from the amount to be inserted");

                count = rep.BatchInsertOrUpdate(userId, list, true);
                Assert.AreEqual(count, list.Count, "Number of inserted records is different from the amount to be inserted");

                count = rep.CountDebitEntries(userId);
                Assert.AreEqual(count, 3, "Total number of records is incorrect");
            }
            finally
            {
                rep.DeleteUser(userId);
            }
        }