Esempio n. 1
0
        private IWidgetRepository CreateMockRepository(InMemoryDatabase db)
        {
            var widgetDbMock = new Mock <IDatabaseConnectionFactory>();

            widgetDbMock.Setup(c => c.GetConnection()).Returns(db.OpenConnection());
            return(new WidgetRepository(widgetDbMock.Object));
        }
Esempio n. 2
0
        public async Task test_get_widgets_ormlite()
        {
            //
            //    Mock data
            var table = new Widget
            {
                WidgetId   = 1,
                WidgetName = "test hell homer"
            };
            var table2 = new Widget
            {
                WidgetId   = 2,
                WidgetName = "test hell homer the second"
            };
            var db = new InMemoryDatabase();

            db.OpenConnection();
            db.Insert(new List <Widget> {
                table, table2
            });

            //
            //    Test
            var repository = CreateMockRepository(db);
            var result     = await repository.GetWidgets();

//            Assert.Single(result);
            Assert.Equal(2, result.Count());
        }
Esempio n. 3
0
        public async Task GetAllAsync_ShouldReturnElements()
        {
            // Arrange
            var sites = new List <Sites>
            {
                new Sites
                {
                    Id = 1
                },
                new Sites
                {
                    Id = 2
                }
            };
            var db = new InMemoryDatabase();

            db.Insert(sites);
            var connectionFactoryMock = new Mock <IDatabaseConnectionFactory>();

            connectionFactoryMock.Setup(c => c.GetConnection()).Returns(db.OpenConnection());

            // Act
            var result = await new SitesRepository(connectionFactoryMock.Object).GetAllAsync();

            // Assert
            Assert.Equal(sites, result.ToList());
        }
        public void should_retrieve_all_posts()
        {
            // Arrange
            var mockPosts = PostsHelper.GetDefaultMockData();
            var db        = new InMemoryDatabase();

            db.Insert <Post>(mockPosts);
            _connectionFactory.GetConnection().Returns(db.OpenConnection());

            // Act
            var posts     = _sut.GetAll();
            var postsList = posts.ToList();

            // Assert
            Assert.IsNotNull(posts);
            Assert.AreEqual(posts.Count(), mockPosts.Count);

            mockPosts = mockPosts.OrderBy(o => o.Id).ToList();
            postsList = postsList.OrderBy(o => o.Id).ToList();

            for (var i = 0; i < mockPosts.Count; i++)
            {
                var mockPost = mockPosts[0];
                var post     = postsList[0];
                Assert.IsTrue(mockPost.Id == post.Id);
                Assert.IsTrue(mockPost.Title == post.Title);
                Assert.IsTrue(mockPost.Text == post.Text);
            }
        }
Esempio n. 5
0
        public void ProductRepository_GetAllValues()
        {
            Mock <IDatabaseConnectionFactory> connectionFactoryMock = new Mock <IDatabaseConnectionFactory>();

            var products = new List <Product>
            {
                new Product {
                    BrandId = 1, Description = "açıklama", DiscountPrice = 10, Name = "test ürünü", Price = 100, Quantitiy = 1, Status = true, Id = 1
                }
            };

            var brands = new List <Brands>
            {
                new Brands {
                    Name = "Marka1", Status = true, Id = 1
                }
            };

            var db = new InMemoryDatabase();

            db.Insert(products);
            db.Insert(brands);

            connectionFactoryMock.Setup(c => c.GetConnection()).Returns(db.OpenConnection());
            var result = new ProductRepository(connectionFactoryMock.Object).GetAllValues();

            Assert.AreEqual(1, result.Count());
        }
Esempio n. 6
0
        protected RepositoryTests()
        {
            var fakeDatabase = new InMemoryDatabase();

            fakeDatabase.CreateTable <T>();

            this.Context = Substitute.For <IDatabaseContext>();
            this.Context.Connection.Returns(fakeDatabase.OpenConnection());
        }
        protected RepositoryTests(IEnumerable <T> objects)
        {
            var fakeDatabase = new InMemoryDatabase();

            fakeDatabase.Insert(objects);

            this.Context = Substitute.For <IDatabaseContext>();
            this.Context.Connection.Returns(fakeDatabase.OpenConnection());
        }
Esempio n. 8
0
        public PeopleRepositoryTests()
        {
            connectionService = new Mock <IConnection>();

            var db = new InMemoryDatabase();

            db.Insert(people);
            connectionService.Setup(c => c.GetConnection(configuration)).Returns(db.OpenConnection());

            peopleGateway = new PeopleRepository(connectionService.Object, configuration);
        }
Esempio n. 9
0
        protected async Task <IWidgetRepository> CreateWidgetRepository(
            Mock <IDatabaseConnectionFactory> connectionFactoryMock = null)
        {
            if (connectionFactoryMock == null)
            {
                var db = new InMemoryDatabase();
                db.OpenConnection();

                connectionFactoryMock = new Mock <IDatabaseConnectionFactory>();
                connectionFactoryMock.Setup(c => c.GetConnection()).Returns(db.OpenConnection());
            }

            IWidgetRepository widgetRepository = new WidgetRepository(connectionFactoryMock.Object);

            var projectRoot = AppContext.BaseDirectory.Substring(0,
                                                                 AppContext.BaseDirectory.LastIndexOf("bin", StringComparison.Ordinal));
            var script = File.ReadAllText(Path.Combine(projectRoot, XyzWidgetSchema));

            await widgetRepository.CreateSqliteSchema(script);

            return(widgetRepository);
        }
        public void should_retrieve_no_one_post()
        {
            // Arrange
            var mockPosts = PostsHelper.GetDefaultMockData();
            var db        = new InMemoryDatabase();

            db.Insert <Post>(mockPosts);
            _connectionFactory.GetConnection().Returns(db.OpenConnection());

            // Act
            var post = _sut.GetById(1000);

            // Assert
            Assert.IsNull(post);
        }
        public async void DatabaseHandler_NoSqlArgs_Managed_Execute()
        {
            var mockConnectionFactory = new Mock <IDatabaseConnectionFactory>();
            var db = new InMemoryDatabase();

            mockConnectionFactory.Setup(mcf => mcf.CreateConnection()).Returns(db.OpenConnection());
            SetupMockDatabase(db);

            var handler = new DatabaseHandler(mockConnectionFactory.Object);
            var result  = await handler.Execute("DELETE FROM Mock WHERE id = 2");

            Assert.True(result.Success);
            Assert.Equal(ConnectionState.Closed, mockConnectionFactory.Object.CreateConnection().State);
            Assert.Equal(1, result.RowsAffected);
        }
        public async void DatabaseHandler_SqlArgs_Managed_Query()
        {
            var mockConnectionFactory = new Mock <IDatabaseConnectionFactory>();
            var db = new InMemoryDatabase();

            mockConnectionFactory.Setup(mcf => mcf.CreateConnection()).Returns(db.OpenConnection());
            SetupMockDatabase(db);

            var handler = new DatabaseHandler(mockConnectionFactory.Object);
            var result  = await handler.Query <dynamic>("SELECT * FROM Mock WHERE id = @id", new { id = 1 });

            Assert.True(result.Success);
            Assert.Equal(ConnectionState.Closed, mockConnectionFactory.Object.CreateConnection().State);
            Assert.Collection(result.Data, data => Assert.Contains(MOCK_FIRST_ROW, data.name));
            Assert.Collection(result.Data, data => Assert.DoesNotContain(MOCK_SECOND_ROW, data.name));
        }
        public async void DatabaseHandler_SqlArgs_Managed_Execute()
        {
            var mockUpdatedName       = "New Name";
            var mockConnectionFactory = new Mock <IDatabaseConnectionFactory>();
            var db = new InMemoryDatabase();

            mockConnectionFactory.Setup(mcf => mcf.CreateConnection()).Returns(db.OpenConnection());
            SetupMockDatabase(db);

            var handler = new DatabaseHandler(mockConnectionFactory.Object);
            var result  = await handler.Execute("UPDATE Mock SET name = @UpdatedName WHERE id = @Id", new { UpdatedName = mockUpdatedName, Id = 2 });

            Assert.True(result.Success);
            Assert.Equal(ConnectionState.Closed, mockConnectionFactory.Object.CreateConnection().State);
            Assert.Equal(1, result.RowsAffected);
        }
        public async void DatabaseHandler_DapperMapping_Managed_Query()
        {
            var mockConnectionFactory = new Mock <IDatabaseConnectionFactory>();
            var db = new InMemoryDatabase();

            mockConnectionFactory.Setup(mcf => mcf.CreateConnection()).Returns(db.OpenConnection());
            SetupMockDatabase(db);

            var handler = new DatabaseHandler(mockConnectionFactory.Object);
            var result  = await handler.Query <MockTable>("SELECT * FROM Mock");

            Assert.True(result.Success);
            Assert.Equal(ConnectionState.Closed, mockConnectionFactory.Object.CreateConnection().State);
            Assert.IsType <List <MockTable> >(result.Data);
            Assert.Collection(result.Data, data => Assert.Contains(MOCK_FIRST_ROW, data.Name),
                              data => Assert.Contains(MOCK_SECOND_ROW, data.Name));
        }
        public async void DatabaseHandler_NoSqlArgs_Unmanaged_Query()
        {
            var mockConnectionFactory = new Mock <IDatabaseConnectionFactory>();
            var db = new InMemoryDatabase();

            mockConnectionFactory.Setup(mcf => mcf.CreateConnection()).Returns(db.OpenConnection());
            SetupMockDatabase(db);

            var handler = new DatabaseHandler(mockConnectionFactory.Object);

            using (var connection = handler.ConnectionFactory.CreateConnection())
            {
                var result = await handler.Query <dynamic>(connection, "SELECT * FROM Mock");

                Assert.True(result.Success);
                Assert.Equal(ConnectionState.Open, mockConnectionFactory.Object.CreateConnection().State);
                Assert.Collection(result.Data, data => Assert.Contains(MOCK_FIRST_ROW, data.name),
                                  data => Assert.Contains(MOCK_SECOND_ROW, data.name));
            }
        }
        public void should_retrieve_post_by_id(int id)
        {
            // Arrange
            var mockPosts = PostsHelper.GetDefaultMockData();
            var db        = new InMemoryDatabase();

            db.Insert <Post>(mockPosts);
            _connectionFactory.GetConnection().Returns(db.OpenConnection());

            var mockPost = mockPosts.Where(x => x.Id == id).FirstOrDefault();

            // Act
            var post = _sut.GetById(id);

            // Assert
            Assert.IsNotNull(post);

            Assert.IsTrue(mockPost.Id == post.Id);
            Assert.IsTrue(mockPost.Title == post.Title);
            Assert.IsTrue(mockPost.Text == post.Text);
        }
Esempio n. 17
0
        public async Task UpdateProduct_When_ProductIsModified()
        {
            // Arrange
            var products = new List <Product>
            {
                new Product {
                    Id = 1, Name = "Product1", Description = "This is the first product"
                },
                new Product {
                    Id = 2, Name = "Product2", Description = "This is the second product"
                }
            };
            var db = new InMemoryDatabase();

            db.Insert(products);

            using (var connection = db.OpenConnection())
            {
                var unitOfWork = new UnitOfWork.UnitOfWork(connection);
                ProductAsyncRepository productAsyncRepository = new ProductAsyncRepository(unitOfWork);
                var product = new Product {
                    Id = 1, Name = "Product1 Updated", Description = "This is the first updated product"
                };

                // Act
                await productAsyncRepository.UpdateAsync(product);

                // Assert
                var allProducts = db.GetAll <Product>();
                Assert.AreEqual(2, allProducts.Count());

                var firstProduct = allProducts.ToList()[0];
                Assert.IsNotNull(firstProduct);

                Assert.AreEqual(1, firstProduct.Id);
                Assert.AreEqual("Product1 Updated", firstProduct.Name);
                Assert.AreEqual("This is the first updated product", firstProduct.Description);
            }
        }
Esempio n. 18
0
        public async Task SaveProduct_When_NewProductInserted()
        {
            // Arrange
            var products = new List <Product>
            {
                new Product {
                    Id = 1, Name = "Product1", Description = "This is the first product"
                },
                new Product {
                    Id = 2, Name = "Product2", Description = "This is the second product"
                }
            };
            var db = new InMemoryDatabase();

            db.Insert(products);

            using (var connection = db.OpenConnection())
            {
                var unitOfWork = new UnitOfWork.UnitOfWork(connection);
                ProductAsyncRepository productAsyncRepository = new ProductAsyncRepository(unitOfWork);
                var product = new Product {
                    Id = 3, Name = "Product3", Description = "This is the third product"
                };

                // Act
                await productAsyncRepository.SaveAsync(product);

                // Assert
                var allProducts = db.GetAll <Product>();
                Assert.AreEqual(3, allProducts.Count());

                var thirdProduct = allProducts.ToList()[2];
                Assert.IsNotNull(thirdProduct);

                Assert.AreEqual(3, thirdProduct.Id);
                Assert.AreEqual("Product3", thirdProduct.Name);
                Assert.AreEqual("This is the third product", thirdProduct.Description);
            }
        }