public async Task BasicCrud()
        {
            using (var trans = DataSource.BeginTransaction())
            {
                var repo = new AsyncRepository<Employee, int>(trans, EmployeeTableName);

                var emp1 = new Employee() { FirstName = "Tom", LastName = "Jones", Title = "President" };
                var echo1 = await repo.InsertAsync(emp1);

                Assert.AreNotEqual(0, echo1.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp1.FirstName, echo1.FirstName, "FirstName");
                Assert.AreEqual(emp1.LastName, echo1.LastName, "LastName");
                Assert.AreEqual(emp1.Title, echo1.Title, "Title");

                echo1.MiddleName = "G";
                await repo.UpdateAsync(echo1);

                var emp2 = new Employee() { FirstName = "Lisa", LastName = "Green", Title = "VP Transportation", ManagerKey = echo1.EmployeeKey };
                var echo2 = await repo.InsertAsync(emp2);
                Assert.AreNotEqual(0, echo2.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp2.FirstName, echo2.FirstName, "FirstName");
                Assert.AreEqual(emp2.LastName, echo2.LastName, "LastName");
                Assert.AreEqual(emp2.Title, echo2.Title, "Title");
                Assert.AreEqual(emp2.ManagerKey, echo2.ManagerKey, "ManagerKey");

                var list = await repo.GetAllAsync();
                Assert.IsTrue(list.Any(e => e.EmployeeKey == echo1.EmployeeKey), "Employee 1 is missing");
                Assert.IsTrue(list.Any(e => e.EmployeeKey == echo2.EmployeeKey), "Employee 2 is missing");

                var get1 = await repo.GetAsync(echo1.EmployeeKey.Value);
                Assert.AreEqual(echo1.EmployeeKey, get1.EmployeeKey);



                var whereSearch1 = await repo.QueryAsync("FirstName = @FN", new { FN = "Tom" });
                Assert.IsTrue(whereSearch1.Any(x => x.EmployeeKey == echo1.EmployeeKey), "Emp1 should have been returned");
                Assert.IsTrue(whereSearch1.All(x => x.FirstName == "Tom"), "Checking for incorrect return values");

                var whereSearch2 = await repo.QueryAsync(new { FirstName = "Tom" });
                Assert.IsTrue(whereSearch2.Any(x => x.EmployeeKey == echo1.EmployeeKey), "Emp1 should have been returned");
                Assert.IsTrue(whereSearch2.All(x => x.FirstName == "Tom"), "Checking for incorrect return values");


                await repo.DeleteAsync(echo2.EmployeeKey.Value);
                await repo.DeleteAsync(echo1.EmployeeKey.Value);

                var list2 = await repo.GetAllAsync();
                Assert.AreEqual(list.Count - 2, list2.Count);

            }
        }
Ejemplo n.º 2
0
        public async Task InsertWithDictionary()
        {
            var repo = new AsyncRepository<Employee, int>(DataSource, EmployeeTableName);

            var emp1 = new Dictionary<string, object>() { { "FirstName", "Tom" }, { "LastName", "Jones" }, { "Title", "President" } };
            var echo1 = await repo.InsertAsync(emp1);

            Assert.AreNotEqual(0, echo1.EmployeeKey, "EmployeeKey was not set");
            Assert.AreEqual(emp1["FirstName"], echo1.FirstName, "FirstName");
            Assert.AreEqual(emp1["LastName"], echo1.LastName, "LastName");
            Assert.AreEqual(emp1["Title"], echo1.Title, "Title");

            await repo.DeleteAsync(echo1.EmployeeKey.Value);

        }
Ejemplo n.º 3
0
            public void GivenRemovingEntityNotInStore_ItShouldThrowWhenAwaiting()
            {
                var options = new DbContextOptionsBuilder <TestContext>()
                              .UseInMemoryDatabase(Helper.GetCallerName())
                              .Options;

                using (var context = new TestContext(options))
                {
                    var repository = new AsyncRepository <TestObject>(context);
                    repository.DeleteAsync(new TestObject())
                    .GetAwaiter()
                    .Invoking(awaiter => awaiter.GetResult())
                    .Should().Throw <DbUpdateConcurrencyException>()
                    .WithMessage("Attempted to update or delete an entity that does not exist in the store.");
                }
            }
Ejemplo n.º 4
0
            public async void GivenDeleteZero_ItShouldRemoveFirstObjectAsync()
            {
                var options = new DbContextOptionsBuilder <TestContext>()
                              .UseInMemoryDatabase(Helper.GetCallerName())
                              .Options;
                var seedData = options.EnsureSeeded();

                using (var context = new TestContext(options))
                {
                    var repository = new AsyncRepository <TestObject>(context);
                    await repository.DeleteAsync(seedData[0]);
                }

                using (var context = new TestContext(options))
                {
                    var foundObject = context.TestObjects.Find(seedData[0].Id);
                    foundObject.Should().BeNull();
                }
            }
        public async Task UpdateWithDictionary()
        {
            using (var trans = DataSource.BeginTransaction())
            {
                var repo = new AsyncRepository<Employee, int>(trans, EmployeeTableName);

                var emp1 = new Dictionary<string, object>() { { "FirstName", "Tom" }, { "LastName", "Jones" }, { "Title", "President" } };
                var echo1 = await repo.InsertAsync(emp1);

                Assert.AreNotEqual(0, echo1.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp1["FirstName"], echo1.FirstName, "FirstName");
                Assert.AreEqual(emp1["LastName"], echo1.LastName, "LastName");
                Assert.AreEqual(emp1["Title"], echo1.Title, "Title");

                var emp2 = new Dictionary<string, object>() { { "EmployeeKey", echo1.EmployeeKey }, { "LastName", "Brown" } };
                await repo.UpdateAsync(emp2);
                var echo2 = await repo.GetAsync(echo1.EmployeeKey.Value);

                //these were changed
                Assert.AreEqual(echo1.EmployeeKey, echo2.EmployeeKey, "EmployeeKey was not set");
                Assert.AreEqual(emp2["LastName"], echo2.LastName, "LastName");

                //these should be unchanged
                Assert.AreEqual(emp1["FirstName"], echo2.FirstName, "FirstName");
                Assert.AreEqual(emp1["Title"], echo2.Title, "Title");


                await repo.DeleteAsync(echo1.EmployeeKey.Value);

            }
        }