public void ThrowsArgumentExceptionForNullOrWhitespaceConnectionString()
            {
                var dbContext = new TestDbContextContainer();

                ExceptionTester.CallMethodAndExpectException <ArgumentException>(() => ConnectionStringHelper.SetConnectionString(dbContext, null));
                ExceptionTester.CallMethodAndExpectException <ArgumentException>(() => ConnectionStringHelper.SetConnectionString(dbContext, string.Empty));
            }
            public void ReturnsTableNameIncludingSchemaForType()
            {
                var dbContext = new TestDbContextContainer();
                var tableName = dbContext.GetTableName<DbContextOrder>();

                Assert.AreEqual("[dbo].[DbContextOrder]", tableName);
            }
 public void ThrowsArgumentNullExceptionForNulEntityType()
 {
     using (var dbContext = new TestDbContextContainer())
     {
         ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => dbContext.GetEntityKey(null, 1));
     }
 }
            public void ThrowsArgumentExceptionForNullOrWhitespaceConnectionString()
            {
                var dbContext = new TestDbContextContainer();

                ExceptionTester.CallMethodAndExpectException<ArgumentException>(() => ConnectionStringHelper.SetConnectionString(dbContext, null));
                ExceptionTester.CallMethodAndExpectException<ArgumentException>(() => ConnectionStringHelper.SetConnectionString(dbContext, string.Empty));
            }
 public void ThrowsArgumentNullExceptionForNullType()
 {
     using (var dbContext = new TestDbContextContainer())
     {
         ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => dbContext.GetTableName(null));
     }
 }
            public void ReturnsCorrectEntitySetName()
            {
                var dbContext = new TestDbContextContainer();

                var entitySetName = dbContext.GetEntitySetName(typeof(DbContextProduct));

                Assert.AreEqual("DbContextProducts", entitySetName);
            }
            public void ReturnsCorrectEntitySetName()
            {
                var dbContext = new TestDbContextContainer();

                var entitySetName = dbContext.GetFullEntitySetName(typeof(DbContextProduct));

                Assert.AreEqual("TestDbContextContainer.DbContextProducts", entitySetName);
            }
            public void SetsConnectionString()
            {
                var dbContext = new TestDbContextContainer();

                dbContext.SetConnectionString(TestConnectionStrings.DbContextModified);

                Assert.AreEqual(TestConnectionStrings.DbContextModified, dbContext.Database.Connection.ConnectionString);
            }
            public void ReturnsObjectContextForDbContext()
            {
                var dbContext = new TestDbContextContainer();

                var objectContext = dbContext.GetObjectContext();

                Assert.IsNotNull(objectContext);
            }
            public void ReturnsObjectContextForDbContext()
            {
                var dbContext = new TestDbContextContainer();

                var objectContext = dbContext.GetObjectContext();

                Assert.IsNotNull(objectContext);
            }
            public void SetsConnectionString()
            {
                var dbContext = new TestDbContextContainer();

                dbContext.SetConnectionString(TestConnectionStrings.DbContextModified);

                Assert.AreEqual(TestConnectionStrings.DbContextModified, dbContext.Database.Connection.ConnectionString);
            }
            public void ReturnsCorrectKeyValue()
            {
                var dbContext = new TestDbContextContainer();

                var keyValue = dbContext.GetEntityKey(typeof(DbContextProduct), 1);

                Assert.AreEqual("Id", keyValue.EntityKeyValues[0].Key);
                Assert.AreEqual(1, keyValue.EntityKeyValues[0].Value);
            }
            public void ReturnsCorrectKeyValue()
            {
                var dbContext = new TestDbContextContainer();

                var keyValue = dbContext.GetEntityKey(typeof(DbContextProduct), 1);

                Assert.AreEqual("Id", keyValue.EntityKeyValues[0].Key);
                Assert.AreEqual(1, keyValue.EntityKeyValues[0].Value);
            }
Example #14
0
 public void ReturnsNullIfKeyIsInvalid()
 {
     using (var dbContext = new TestDbContextContainer())
     {
         using (var repository = new DbContextCustomerRepository(dbContext))
         {
             Assert.IsNull(repository.GetByKey(12345));
         }
     }
 }
Example #15
0
 public void ThrowsArgumentNullExceptionForNullEntity()
 {
     using (var dbContext = new TestDbContextContainer())
     {
         using (var repository = new DbContextCustomerRepository(dbContext))
         {
             ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => repository.Update(null));
         }
     }
 }
Example #16
0
 public void ThrowsExceptionWhenTableDoesNotContainEntity()
 {
     using (var dbContext = new TestDbContextContainer())
     {
         using (var repository = new DbContextCustomerRepository(dbContext))
         {
             ExceptionTester.CallMethodAndExpectException <InvalidOperationException>(() => repository.Single(x => x.Id == 999));
         }
     }
 }
            public void ReturnsNamedConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = string.Format("{0};Application Name=EntityFrameworkMUE", TestConnectionStrings.DbContextDefault);

                var connectionString = context.GetConnectionString();

                Assert.AreEqual(expectedString, connectionString, true);
            }
            public void ReturnsNamedConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = string.Format("{0};Application Name=EntityFrameworkMUE", TestConnectionStrings.DbContextDefault);

                var connectionString = context.GetConnectionString();

                Assert.IsTrue(string.Equals(expectedString, connectionString, StringComparison.OrdinalIgnoreCase));
            }
            public void ReturnsRealConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = TestConnectionStrings.DbContextModified;

                context.SetConnectionString(TestConnectionStrings.DbContextModified);
                var connectionString = context.GetConnectionString();

                Assert.IsTrue(string.Equals(expectedString, connectionString, StringComparison.OrdinalIgnoreCase));
            }
            public void ReturnsObjectContextForDbContext()
            {
                using (var dbContext = new TestDbContextContainer())
                {
#pragma warning disable IDISP001
                    var objectContext = dbContext.GetObjectContext();
#pragma warning restore IDISP001

                    Assert.IsNotNull(objectContext);
                }
            }
            public void ReturnsRealConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = TestConnectionStrings.DbContextModified;

                context.SetConnectionString(TestConnectionStrings.DbContextModified);
                var connectionString = context.GetConnectionString();

                Assert.AreEqual(expectedString, connectionString, true);
            }
Example #22
0
 public void ReturnsNullWhenTableContainsEntity()
 {
     using (var dbContext = new TestDbContextContainer())
     {
         using (var repository = new DbContextCustomerRepository(dbContext))
         {
             var customer = repository.SingleOrDefault(x => x.Id == 999);
             Assert.IsNull(customer);
         }
     }
 }
Example #23
0
            public void SucceedsWhenNoEntitiesMatchFilter()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        repository.Delete(x => x.Id == 999);

                        dbContext.SaveChanges();
                    }
                }
            }
Example #24
0
            public void ReturnsEntityIfKeyIsValid()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(42);

                        var existingCustomer = repository.GetByKey(42);

                        Assert.IsNotNull(existingCustomer);
                    }
                }
            }
            public void IncludesEntitiesUsingExpression()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(42);

                        var existingCustomer = repository.GetAll().Include(x => x.DbContextOrders).FirstOrDefault();

                        Assert.IsNotNull(existingCustomer);
                    }
                }
            }
Example #26
0
            public void ReturnsEntityWhenTableContainsEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(1);

                        var customer = repository.Single(x => x.Id == 1);

                        Assert.IsNotNull(customer);
                        Assert.AreEqual(1, customer.Id);
                    }
                }
            }
Example #27
0
            public void ReturnsCorrectEntityCount()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(100);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(101);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(102);

                        var customerCount = repository.Count(x => x.Id >= 100 && x.Id <= 102);

                        Assert.AreEqual(3, customerCount);
                    }
                }
            }
Example #28
0
            public void ReturnsCorrectEntities()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(100);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(101);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(102);

                        var customers = repository.GetAll().ToList();

                        Assert.IsTrue(customers.Count >= 3);
                    }
                }
            }
Example #29
0
            public void AddsNonExistingEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        var customer = EFTestHelper.CreateCustomer(1235);

                        repository.Attach(customer);

                        dbContext.SaveChanges();

                        var fetchedCustomer = repository.GetByKey(1235);
                        Assert.AreEqual(customer, fetchedCustomer);
                    }
                }
            }
Example #30
0
        public static void CreateCustomerIfNotAlreadyExists(int id)
        {
            using (var dbContext = new TestDbContextContainer())
            {
                using (var repository = new DbContextCustomerRepository(dbContext))
                {
                    var existingCustomer = repository.FirstOrDefault(x => x.Id == id);
                    if (existingCustomer == null)
                    {
                        var customer = CreateCustomer(id);

                        repository.Add(customer);

                        dbContext.SaveChanges();
                    }
                }
            }
        }
Example #31
0
            public void SucceedsWhenEntitiesMatchFilter()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(201);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(202);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(203);

                        repository.Delete(x => x.Id >= 201 && x.Id <= 203);

                        dbContext.SaveChanges();

                        Assert.IsNull(repository.GetByKey(201));
                        Assert.IsNull(repository.GetByKey(202));
                        Assert.IsNull(repository.GetByKey(203));
                    }
                }
            }
Example #32
0
            public void UpdatesEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(301);

                        var customer1 = repository.GetByKey(301);
                        customer1.Name = "John Doe";

                        repository.Update(customer1);

                        dbContext.SaveChanges();

                        var customer2 = repository.GetByKey(301);

                        Assert.IsNotNull(customer2);
                        Assert.AreEqual("John Doe", customer2.Name);
                    }
                }
            }
Example #33
0
            public void DeletesSpecificEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(201);

                        var customer1 = repository.GetByKey(201);

                        Assert.IsNotNull(customer1);

                        repository.Delete(customer1);

                        dbContext.SaveChanges();

                        var customer2 = repository.GetByKey(201);

                        Assert.IsNull(customer2);
                    }
                }
            }
    public static void SetUp()
    {
        //System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

        // Required since we do multithreaded initialization
        //TypeCache.InitializeTypes(allowMultithreadedInitialization: false);
        TypeCache.InitializeTypes();

        using (var dbContext = new TestDbContextContainer())
        {
            dbContext.Database.CreateIfNotExists();

            // Delete all data
            var allOrders = (from x in dbContext.DbContextOrders
                             select x).ToList();
            foreach (var x in allOrders)
            {
                dbContext.DbContextOrders.Remove(x);
            }

            var allCustomers = (from x in dbContext.DbContextCustomers
                                select x).ToList();
            foreach (var x in allCustomers)
            {
                dbContext.DbContextCustomers.Remove(x);
            }

            var allProducts = (from x in dbContext.DbContextProducts
                               select x).ToList();
            foreach (var x in allProducts)
            {
                dbContext.DbContextProducts.Remove(x);
            }

            dbContext.SaveChanges();
        }
    }
            public void ThrowsArgumentNullExceptionForNullType()
            {
                var dbContext = new TestDbContextContainer();

                ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => dbContext.GetTableName(null));
            }
            public void ReturnsTableNameIncludingSchemaForType()
            {
                var dbContext = new TestDbContextContainer();
                var tableName = dbContext.GetTableName<DbContextOrder>();

                Assert.AreEqual("[dbo].[DbContextOrder]", tableName);
            }
            public void ReturnsRealConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = TestConnectionStrings.DbContextModified;

                context.SetConnectionString(TestConnectionStrings.DbContextModified);
                var connectionString = context.GetConnectionString();

                Assert.IsTrue(string.Equals(expectedString, connectionString, StringComparison.OrdinalIgnoreCase));
            }
            public void ReturnsNamedConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = string.Format("{0};Application Name=EntityFrameworkMUE", TestConnectionStrings.DbContextDefault);

                var connectionString = context.GetConnectionString();

                Assert.IsTrue(string.Equals(expectedString, connectionString, StringComparison.OrdinalIgnoreCase));
            }
            public void ReturnsRealConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = TestConnectionStrings.DbContextModified;

                context.SetConnectionString(TestConnectionStrings.DbContextModified);
                var connectionString = context.GetConnectionString();

                Assert.AreEqual(expectedString, connectionString, true);
            }
            public void ThrowsArgumentNullExceptionForNulEntityType()
            {
                var dbContext = new TestDbContextContainer();

                ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => dbContext.GetEntityKey(null, 1));
            }
            public void ReturnsNamedConnectionString()
            {
                var context = new TestDbContextContainer();

                string expectedString = string.Format("{0};Application Name=EntityFrameworkMUE", TestConnectionStrings.DbContextDefault);

                var connectionString = context.GetConnectionString();

                Assert.AreEqual(expectedString, connectionString, true);
            }