Exemplo n.º 1
0
        public void LimitOffset_ShouldLimitAndOffsetResults()
        {
            ITestObjectRepository repo    = GetTestObjectRepository();
            string            description = "Limit and Offset Test";
            List <TestObject> expected    = GetLimitTestObject(description, 10);

            expected.ForEach(x => repo.Add(x));

            List <TestObject> actual1 = repo.GetByDescriptionWithLimitAndOffset(description, 4, 0);

            Assert.AreEqual(4, actual1.Count);


            List <TestObject> actual2 = repo.GetByDescriptionWithLimitAndOffset(description, 4, 4);

            Assert.AreEqual(4, actual2.Count);

            foreach (TestObject testObject1 in actual1)
            {
                foreach (TestObject testObject2 in actual2)
                {
                    Assert.AreNotEqual(testObject1.ID, testObject2.ID);
                }
            }
        }
Exemplo n.º 2
0
        public void Add_MultipleTestObjects_ShouldAdd()
        {
            ITestObjectRepository repo    = GetTestObjectRepository();
            List <TestObject>     objects = new List <TestObject>();

            for (int i = 0; i < 5000; i++)
            {
                objects.Add(new TestObject {
                    Title       = "Add Multiple Test " + i,
                    Description = "Multiple Insert Test",
                    StatusID    = Status.Active,
                    InputOn     = DateTime.Now,
                    InputByID   = 1,
                    IsActive    = true
                });
            }

            repo.Add(objects);

            List <TestObject> testObjects = repo.GetByDescription("Multiple Insert Test");

            Assert.AreEqual(objects.Count, testObjects.Count); // Make sure the same number of objects are returns
            foreach (TestObject testObject in testObjects)
            {
                Assert.AreEqual("Multiple Insert Test", testObject.Description);
            }
        }
        public async Task Xml_Data_Saved_In_DataBase_Should_Be_Properly_Restored()
        {
            ServiceCollection services =
                new ServiceCollectionBuilder().PrepareServiceCollection(s =>
            {
                s.ResetDapperCustomTypeHandlers();
                s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly());
            });

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>();

                TestXmlObject testObject = CreateFullTestObject();

                // Act
                await testObjectRepository.SaveTestXmlObject(testObject);

                TestXmlObject retrievedTestObject = await testObjectRepository.GetTestXmlObject(testObject.Id);

                // Assert
                retrievedTestObject.Should().NotBeNull();
                retrievedTestObject.Should().BeEquivalentTo(testObject);
                retrievedTestObject.Content.Should().BeEquivalentTo(testObject.Content);
            }
        }
        public async Task Using_JsonCustomOptions_Json_Data_Saved_In_DataBase_Should_Be_Properly_Restored()
        {
            ServiceCollection services =
                new ServiceCollectionBuilder().PrepareServiceCollection(s =>
            {
                s.ResetDapperCustomTypeHandlers();
                s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly(), options =>
                {
                    options.JsonSerializerOptions = new JsonSerializerOptions
                    {
                        IgnoreNullValues     = false,
                        PropertyNamingPolicy = null
                    };
                });
            });

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>();

                TestJsonObject testObject = new TestJsonObject
                {
                    FirstName = "John",
                    LastName  = "Doe",
                    StartWork = new DateTime(2018, 06, 01),
                    Content   = new TestJsonContentObject
                    {
                        Nick                  = "JD",
                        DateOfBirth           = new DateTime(1990, 10, 11),
                        Siblings              = 2,
                        FavoriteDaysOfTheWeek = new List <string>
                        {
                            "Friday",
                            "Saturday",
                            "Sunday"
                        },
                        FavoriteNumbers = new List <int> {
                            10, 15, 1332, 5555
                        }
                    }
                };

                // Act
                await testObjectRepository.SaveTestJsonObject(testObject);

                TestJsonObject retrievedTestObject = await testObjectRepository.GetTestJsonObject(testObject.Id);

                // Assert
                retrievedTestObject.Should().NotBeNull();
                retrievedTestObject.Should().BeEquivalentTo(testObject);
                retrievedTestObject.Content.Should().BeEquivalentTo(testObject.Content);
            }
        }
Exemplo n.º 5
0
        public void Limit_ShouldLimitResults()
        {
            ITestObjectRepository repo = GetTestObjectRepository();
            string description         = "Limit Test";

            List <TestObject> expected = GetLimitTestObject(description, 10);

            expected.ForEach(x => repo.Add(x));

            List <TestObject> actual1 = repo.GetByDescriptionWithLimit(description, 4);

            Assert.AreEqual(4, actual1.Count);
        }
Exemplo n.º 6
0
        public void OrderBy_ShouldOrderDescending()
        {
            ITestObjectRepository repo    = GetTestObjectRepository();
            string            description = "OrderByDescending Test";
            List <TestObject> expected    = GetLimitTestObject(description, 5);

            repo.Add(expected);

            List <TestObject> actual = repo.GetByDescriptionWithInputOnOrderDesc(description);

            Assert.AreEqual(expected.Count, actual.Count);

            expected = expected.OrderByDescending(x => x.InputOn).ToList();

            for (int i = 0; i < actual.Count; i++)
            {
                Assert.AreEqual(actual[i].InputOn, expected[i].InputOn);
            }
        }
Exemplo n.º 7
0
        public void Add_TestObject_ShouldAdd()
        {
            ITestObjectRepository repo     = GetTestObjectRepository();
            TestObject            expected = new TestObject {
                Title       = "Add Test",
                Description = "Add Test Description",
                StatusID    = Status.Active,
                InputOn     = DateTime.Now,
                InputByID   = 1,
                IsActive    = true
            };

            repo.Add(expected);

            TestObject actual = repo.GetByTitle(expected.Title);

            Assert.IsNotNull(actual);      // Make sure object was returned
            Assert.AreNotEqual(0, actual); // Make sure autonumber was assigned
            Assert.IsTrue(expected.AllPropsEqual(actual), "The object returned from the database does not match the original");
        }
Exemplo n.º 8
0
        public void When_Custom_Xml_Handler_Is_Not_Registered_Exception_Should_Be_Thrown()
        {
            ServiceCollection services =
                new ServiceCollectionBuilder().PrepareServiceCollection(s =>
            {
                s.ResetDapperCustomTypeHandlers();
            });

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>();

                TestXmlObject testObject = CreateTestXmlObject();

                // Assert
                Assert.ThrowsAsync <NotSupportedException>(async() => await testObjectRepository.SaveTestXmlObject(testObject));
            }
        }
Exemplo n.º 9
0
        public void Remove_TestObject_ShouldRemove()
        {
            ITestObjectRepository repo   = GetTestObjectRepository();
            TestObject            newObj = new TestObject {
                Title       = "Remove Test",
                Description = "Remove Test Description",
                StatusID    = Status.Pending,
                InputOn     = DateTime.Now,
                InputByID   = 1,
                IsActive    = true
            };

            repo.Add(newObj);

            TestObject fromDbNotNull = repo.GetByID(newObj.ID);

            repo.Remove(newObj);
            TestObject fromDbNull = repo.GetByID(newObj.ID);

            Assert.IsNotNull(fromDbNotNull);
            Assert.IsNull(fromDbNull);
        }
Exemplo n.º 10
0
        public void Update_TestObject_ShouldUpdate()
        {
            ITestObjectRepository repo   = GetTestObjectRepository();
            TestObject            newObj = new TestObject {
                Title       = "Update Test",
                Description = "Update Test Description",
                StatusID    = Status.Active,
                InputOn     = DateTime.Now,
                InputByID   = 1,
                IsActive    = true
            };

            repo.Add(newObj);

            newObj.Title   += " - Updated";
            newObj.StatusID = Status.Closed;
            repo.Update(newObj);
            TestObject fromDB = repo.GetByID(newObj.ID);

            Assert.IsNotNull(fromDB); // Make sure object was returned
            Assert.IsTrue(newObj.AllPropsEqual(fromDB), "The object returned from the database does not match the updated original");
        }
Exemplo n.º 11
0
        public void When_Custom_Json_Handler_Is_Registered_Exception_Should_Not_Be_Thrown_V2()
        {
            ServiceCollection services =
                new ServiceCollectionBuilder().PrepareServiceCollection(s =>
            {
                s.ResetDapperCustomTypeHandlers();
                s.RegisterDapperCustomTypeHandlers(new[] { Assembly.GetExecutingAssembly() });
            });

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>();

                TestJsonObject testObject = CreateTestJsonObject();

                // Assert
                Assert.DoesNotThrowAsync(async() => await testObjectRepository.SaveTestJsonObject(testObject));
            }
        }
        public async Task Null_Json_Data_Saved_In_DataBase_Should_Be_Restored_As_Null_Object()
        {
            ServiceCollection services =
                new ServiceCollectionBuilder().PrepareServiceCollection(s =>
            {
                s.ResetDapperCustomTypeHandlers();
                s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly());
            });

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                ITestObjectRepository testObjectRepository = scopedServices.GetRequiredService <ITestObjectRepository>();

                TestJsonObject testObject = new TestJsonObject
                {
                    FirstName = "John",
                    LastName  = "Doe",
                    StartWork = new DateTime(2018, 06, 01),
                    Content   = null
                };

                // Act
                await testObjectRepository.SaveTestJsonObject(testObject);

                TestJsonObject retrievedTestObject = await testObjectRepository.GetTestJsonObject(testObject.Id);

                // Assert
                retrievedTestObject.Should().NotBeNull();
                retrievedTestObject.Should().BeEquivalentTo(testObject);
                retrievedTestObject.Content.Should().BeEquivalentTo(testObject.Content);
            }
        }
Exemplo n.º 13
0
        public void GetBetweenDates_ShouldReturnValidResults()
        {
            ITestObjectRepository repo    = GetTestObjectRepository();
            TestObject            newObj1 = new TestObject {
                Title       = "Date Range Test 1",
                Description = "Remove Test Description",
                StatusID    = Status.Active,
                InputOn     = new DateTime(1999, 1, 4, 12, 34, 20),
                InputByID   = 1,
                IsActive    = true
            };
            TestObject newObj2 = new TestObject {
                Title       = "Date Range Test 2",
                Description = "Remove Test Description",
                StatusID    = Status.Active,
                InputOn     = new DateTime(1999, 1, 6, 8, 4, 56),
                InputByID   = 1,
                IsActive    = true
            };
            TestObject newObj3 = new TestObject {
                Title       = "Date Range Test 3",
                Description = "Remove Test Description",
                StatusID    = Status.Active,
                InputOn     = new DateTime(1999, 1, 9, 18, 14, 0),
                InputByID   = 1,
                IsActive    = true
            };

            repo.Add(newObj1);
            repo.Add(newObj2);
            repo.Add(newObj3);

            List <TestObject> actual = repo.GetByInputOnDateRange(new DateTime(1999, 1, 2), new DateTime(1999, 1, 7));

            Assert.AreEqual(2, actual.Count);
        }