1 using System.Collections.Generic;
   2 using DomainObjects.Diagnostics;
   3 using DomainObjects.Facade.Command;
   4 using DomainObjects.Facade.Test;
   5 using DomainObjects.Test.Domain;
   6 using NUnit.Framework;
   7 
   8 namespace DomainObjects.Test.TestFixture
   9 {
  10   /// <summary>
  11   /// Tests the DeleteByCriteria class.
  12   /// </summary>
  13   [TestFixture]
  14   public class DeleteByCriteriaTests : DomainObjectsTestFixture
  15   {
  16     [Test]
  17     public void ExecuteDelete()
  18     {
  19       // Create and persist a set of products to set up the unit test.
  20       // These products will be deleted by criteria later in the test.
  21       ProductCategory productCategory = new ProductCategory(GenerateUniqueString(Types.ProductCategory.Field._name), GenerateUniqueString(Types.ProductCategory.Field._description)).Manage<ProductCategory>();
  22       string product1Name = GenerateUniqueString(Types.Product.Field._name);
  23       string product2Name = GenerateUniqueString(Types.Product.Field._name);
  24       Product product1 = new Product(productCategory, product1Name).Manage<Product>();
  25       Product product2 = new Product(productCategory, product2Name).Manage<Product>();
  26       PersistenceFacade.Persist(product1, product2, productCategory);
  27 
  28       // Create an delete command
  29       DeleteByCriteria deleteByCriteria = new DeleteByCriteria();
  30 
  31       // Specify which product will be deleted.
  32       deleteByCriteria.Where.AddEqualTo(Types.Product.Field._name, product1Name);
  33 
  34       // Execute the delete
  35       deleteByCriteria.Execute<Product>();
  36 
  37       // Ensure that our assertions below are
  38       // retrieving the values from the database
  39       // and not the local caches
  40       ClearAppDomainAndContextCache();
  41 
  42       // Validate that that only the product we specified was deleted
  43       Query<Product> productQuery = new Query<Product>();
  44       productQuery.Where = Types.Product.Field._name.Value == product1Name;
  45       Assert.IsNull(productQuery.FindObject(), "The Product with the name of product1Name should be deleted.");
  46 
  47       Query<Product> productQuery2 = new Query<Product>();
  48       productQuery2.Where = Types.Product.Field._name.Value == product2Name;
  49       Assert.IsNotNull(productQuery2.FindObject(), "The Product with the name of product2Name should not be deleted.");
  50     }
  51 
  52     /// <summary>
  53     /// Validates that an 'DELETE TOP()' clause is generated when <see cref="DeleteByCriteria.Top"/> is specified in an DeleteByCriteria command.
  54     /// </summary>
  55     [Test]
  56     public void ExecuteDeleteWithTop()
  57     {
  58       // Create and persist a set of products to set up the unit test.
  59       // These products will be deleted by criteria later in the test.
  60       ProductCategory productCategory = new ProductCategory(GenerateUniqueString(Types.ProductCategory.Field._name), GenerateUniqueString(Types.ProductCategory.Field._description)).Manage<ProductCategory>();
  61       string product1Name = GenerateUniqueString(Types.Product.Field._name);
  62       string product2Name = GenerateUniqueString(Types.Product.Field._name);
  63       Product product1 = new Product(productCategory, product1Name).Manage<Product>();
  64       Product product2 = new Product(productCategory, product2Name).Manage<Product>();
  65       PersistenceFacade.Persist(product1, product2, productCategory);
  66 
  67       // Create an delete command
  68       DeleteByCriteria deleteByCriteria = new DeleteByCriteria();
  69 
  70       // Specify which products will be deleted.
  71       // They each are associated with the same ProductCategory
  72       // so use this to constrain the delete.
  73       deleteByCriteria.Where.AddEqualTo(Types.Product.Field._productCategoryId, productCategory.PrimaryKey[0]);
  74 
  75       // Specify that only the top 1 will be deleted
  76       deleteByCriteria.Top = 1;
  77 
  78       // Track the commands executed against the database
  79       CommandMonitor commandMonitor = new CommandMonitor();
  80 
  81       // Execute the delete
  82       deleteByCriteria.Execute<Product>();
  83 
  84       // Validate that the expected SQL statement was generated.
  85       Assert.IsTrue(commandMonitor.FirstCommandText.Contains("DELETE TOP (1)"), "A 'DELETE TOP()' SQL statement was generated.");
  86 
  87       // Validate that only one product was deleted
  88       Query<Product> productQuery = new Query<Product>();
  89       productQuery.Where.AddIn(Types.Product.Field._name, product1Name, product2Name);
  90       List<Product> deletedProducts = productQuery.FindObjectSet();
  91 
  92       Assert.AreEqual(1, deletedProducts.Count, "Exactly one product was deleted.");
  93     }
  94   }
  95 }