From b95984bc191294a7b0981779aba21c684d181a62 Mon Sep 17 00:00:00 2001 From: Vagif Abilov Date: Sun, 5 Aug 2012 23:10:50 +0200 Subject: [PATCH] Added tests for batch requests. --- .../EntityFrameworkCodeFirstMsSqlTests.cs | 102 ++++++++++ Northwind.ServiceTests/BatchTestBase.cs | 182 ++++++++++++++++++ .../Northwind.ServiceTests.csproj | 2 + 3 files changed, 286 insertions(+) create mode 100644 Northwind.ServiceTests/Batch/EntityFrameworkCodeFirstMsSqlTests.cs create mode 100644 Northwind.ServiceTests/BatchTestBase.cs diff --git a/Northwind.ServiceTests/Batch/EntityFrameworkCodeFirstMsSqlTests.cs b/Northwind.ServiceTests/Batch/EntityFrameworkCodeFirstMsSqlTests.cs new file mode 100644 index 0000000..eb950d7 --- /dev/null +++ b/Northwind.ServiceTests/Batch/EntityFrameworkCodeFirstMsSqlTests.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data.Services.Client; +using System.Linq; +using System.Text; +using NUnit.Framework; +using Northwind.ServiceTests; +using Northwind.ServiceTests.EntityFrameworkCodeFirstMsSqlService; + +namespace Northwind.ServiceTests.Batch +{ + [TestFixture] + public class EntityFrameworkCodeFirstMsSqlTests : BatchTestBase + { + public EntityFrameworkCodeFirstMsSqlTests() + : base(new Northwind.ServiceTests.Query.EntityFrameworkCodeFirstMsSqlTests()) + { + } + + public override void SaveChanges(SaveChangesOptions options = SaveChangesOptions.Batch) + { + this.context.SaveChanges(options); + } + + public override void ClearTestData() + { + this.context.Products.Where(x => x.ProductName.StartsWith("Test_")).ToList() + .ForEach(x => this.context.DeleteObject(x)); + this.context.Categories.Where(x => x.CategoryName.StartsWith("Test_")).ToList() + .ForEach(x => this.context.DeleteObject(x)); + this.context.SaveChanges(); + } + + public override Categories CreateCategory() + { + int nextRandom = random.Next(); + var category = new Categories() + { + CategoryID = nextRandom, + CategoryName = "Test_" + nextRandom.ToString() + }; + this.context.AddToCategories(category); + return category; + } + + public override Categories CreateInvalidCategory() + { + var category = new Categories(); + this.context.AddToCategories(category); + return category; + } + + public override Categories UpdateCategory(Categories category) + { + int nextRandom = random.Next(); + category.CategoryName = "Test_" + nextRandom.ToString(); + this.context.UpdateObject(category); + return category; + } + + public override void DeleteCategory(Categories category) + { + this.context.DeleteObject(category); + } + + public override int GetCategoryID(Categories category) + { + return category.CategoryID; + } + + public override string GetCategoryName(Categories category) + { + return category.CategoryName; + } + + public override Products CreateProduct() + { + int nextRandom = random.Next(); + var product = new Products() + { + ProductID = nextRandom, + ProductName = "Test_" + nextRandom.ToString(), + CategoryID = 1, + SupplierID = 1, + }; + this.context.AddToProducts(product); + return product; + } + + public override void AttachProductToCategory(Categories category, Products product) + { + this.context.AddLink(category, "Products", product); + } + + public override void AssignCategoryToProduct(Products product, Categories category) + { + this.context.SetLink(product, "Category", category); + } + } +} diff --git a/Northwind.ServiceTests/BatchTestBase.cs b/Northwind.ServiceTests/BatchTestBase.cs new file mode 100644 index 0000000..1689d29 --- /dev/null +++ b/Northwind.ServiceTests/BatchTestBase.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Data.Services.Client; +using System.Linq; +using System.Text; +using NUnit.Framework; + +namespace Northwind.ServiceTests +{ + public abstract class BatchTestBase< + TContext, TCategories, TCustomers, TCustomerDemographics, TEmployees, TOrders, TOrderDetails, TProducts, + TRegions, TShippers, TSuppliers, TTerritories> + { + protected static Random random = new Random(); + protected TContext context + { + get { return queryTestBase.context; } + set { queryTestBase.context = value; } + } + + protected QueryTestBase< + TContext, TCategories, TCustomers, TCustomerDemographics, TEmployees, TOrders, TOrderDetails, TProducts, TRegions, TShippers, TSuppliers, TTerritories> queryTestBase; + + public abstract void SaveChanges(SaveChangesOptions options = SaveChangesOptions.Batch); + public abstract void ClearTestData(); + public abstract TCategories CreateCategory(); + public abstract TCategories CreateInvalidCategory(); + public abstract TCategories UpdateCategory(TCategories category); + public abstract void DeleteCategory(TCategories category); + public abstract int GetCategoryID(TCategories category); + public abstract string GetCategoryName(TCategories category); + public abstract TProducts CreateProduct(); + public abstract void AttachProductToCategory(TCategories category, TProducts product); + public abstract void AssignCategoryToProduct(TProducts product, TCategories category); + + public BatchTestBase(QueryTestBase< + TContext, TCategories, TCustomers, TCustomerDemographics, TEmployees, TOrders, TOrderDetails, TProducts, TRegions, TShippers, TSuppliers, TTerritories> queryTestBase) + { + this.queryTestBase = queryTestBase; + } + + [SetUp] + public void SetUp() + { + this.context = queryTestBase.CreateContext(); + } + + [TearDown] + public void TearDown() + { + ClearTestData(); + } + + [Test] + public void should_have_nonnull_context() + { + Assert.IsNotNull(context); + } + + [Test] + public void should_create_category() + { + var category = CreateCategory(); + SaveChanges(); + category = queryTestBase.GetCategory(GetCategoryName(category)); + Assert.IsNotNull(category); + } + + [Test] + public void should_not_find_unsaved_category() + { + var category = CreateCategory(); + Assert.Throws(() => queryTestBase.GetCategory(GetCategoryName(category))); + } + + [Test] + public void should_update_category() + { + var category = CreateCategory(); + var oldCategoryName = GetCategoryName(category); + var newCategory = UpdateCategory(category); + SaveChanges(); + Assert.AreNotEqual(oldCategoryName, GetCategoryName(newCategory)); + category = queryTestBase.GetCategory(GetCategoryName(newCategory)); + Assert.AreEqual(GetCategoryName(newCategory), GetCategoryName(category)); + } + + [Test] + public void should_create_and_update_category() + { + var category = CreateCategory(); + var savedCategoryName = GetCategoryName(category); + SaveChanges(); + category = CreateCategory(); + var createdCategoryName = GetCategoryName(category); + category = queryTestBase.GetCategory(savedCategoryName); + category = UpdateCategory(category); + var updatedCategoryName = GetCategoryName(category); + SaveChanges(); + Assert.AreNotEqual(savedCategoryName, updatedCategoryName); + Assert.AreNotEqual(createdCategoryName, updatedCategoryName); + category = queryTestBase.GetCategory(createdCategoryName); + Assert.IsNotNull(category); + category = queryTestBase.GetCategory(updatedCategoryName); + Assert.IsNotNull(category); + Assert.Throws(() => queryTestBase.GetCategory(savedCategoryName)); + } + + [Test] + public void should_delete_category() + { + var category = CreateCategory(); + DeleteCategory(category); + SaveChanges(); + Assert.Throws(() => queryTestBase.GetCategory(GetCategoryName(category))); + } + + [Test] + public void should_not_create_any_category_on_error_in_batch() + { + var category = CreateCategory(); + var categoryName = GetCategoryName(category); + CreateInvalidCategory(); + Assert.Throws(() => SaveChanges()); + this.context = queryTestBase.CreateContext(); + Assert.Throws(() => queryTestBase.GetCategory(categoryName)); + } + + [Test] + public void should_create_one_category_on_error_in_non_batch_with_stop() + { + var category = CreateCategory(); + var categoryName1 = GetCategoryName(category); + CreateInvalidCategory(); + category = CreateCategory(); + var categoryName2 = GetCategoryName(category); + Assert.Throws(() => SaveChanges(SaveChangesOptions.None)); + this.context = queryTestBase.CreateContext(); + category = queryTestBase.GetCategory(categoryName1); + Assert.IsNotNull(category); + Assert.Throws(() => queryTestBase.GetCategory(categoryName2)); + } + + [Test] + public void should_create_two_categories_on_error_in_non_batch_with_continue() + { + var category = CreateCategory(); + var categoryName1 = GetCategoryName(category); + CreateInvalidCategory(); + category = CreateCategory(); + var categoryName2 = GetCategoryName(category); + Assert.Throws(() => SaveChanges(SaveChangesOptions.ContinueOnError)); + this.context = queryTestBase.CreateContext(); + category = queryTestBase.GetCategory(categoryName1); + Assert.IsNotNull(category); + category = queryTestBase.GetCategory(categoryName2); + Assert.IsNotNull(category); + } + + [Test] + public void should_attach_product_to_category() + { + var category = CreateCategory(); + var product = CreateProduct(); + AttachProductToCategory(category, product); + SaveChanges(); + var products = queryTestBase.GetCategoryProducts(category); + Assert.AreEqual(1, products.Count()); + } + + [Test] + public void should_set_category_to_product() + { + var category = CreateCategory(); + var product = CreateProduct(); + AssignCategoryToProduct(product, category); + SaveChanges(); + category = queryTestBase.GetProductCategory(product); + Assert.IsNotNull(category); + } + } +} diff --git a/Northwind.ServiceTests/Northwind.ServiceTests.csproj b/Northwind.ServiceTests/Northwind.ServiceTests.csproj index 545a099..4da1b58 100644 --- a/Northwind.ServiceTests/Northwind.ServiceTests.csproj +++ b/Northwind.ServiceTests/Northwind.ServiceTests.csproj @@ -48,6 +48,8 @@ + +