1 using DomainObjects.Facade.Command;
   2 using DomainObjects.Facade.Test;
   3 using DomainObjects.Test.Domain;
   4 using NUnit.Framework;
   5 
   6 namespace DomainObjects.Test.TestFixture
   7 {
   8   /// <summary>
   9   /// Tests the InsertByQuery class.
  10   /// </summary>
  11   [TestFixture]
  12   public class InsertByQueryTests : DomainObjectsTestFixture
  13   {
  14     [Category(UnitTestCategory.DoesNotWorkWithServerSideSequencing)]
  15     [Category(UnitTestCategory.DoesNotWorkWithOracle)]
  16     [Test]
  17     public void ExecuteInsert()
  18     {
  19       // Create and persist an AcousticGuitar to set up the unit test.
  20       // Values from this instance will be selected and inserted into the
  21       // PRODUCT table via an InsertByQuery command.
  22       ProductCategory productCategory = new ProductCategory("Nylon String Guitars", "Acoustic Guitars that have nylon strings").Manage<ProductCategory>();
  23       AcousticGuitar acousticGuitar = new AcousticGuitar(productCategory, "McPherson", 6, "Nylon Classical(N) body shape").Manage<AcousticGuitar>();
  24       PersistenceFacade.Persist(acousticGuitar, productCategory);
  25 
  26       // For assertion purposes later, let's see how many Products we
  27       // have in the database. At the end of the test we should have inserted
  28       // one more. Passing 'null' for the Criteria argument means 'select all rows'.
  29       int productCountBeforeInsertByQuery = Query.GetCount<Product>(null);
  30 
  31       // Create an InsertByQuery command that will insert Product values
  32       // that are selected from the AcousticGuitar instance.
  33       InsertByQuery<Product, AcousticGuitar> insertByQuery = new InsertByQuery<Product, AcousticGuitar>();
  34 
  35       // Select the AcousticGuitar body description value and assign it to the Product name.
  36       insertByQuery.AddInsertField(Types.Product.Field._name, Types.AcousticGuitar.Field._bodyDescription);
  37 
  38       // Associate the Product with the same ProductCategory as the AcousticGuitar.
  39       insertByQuery.AddInsertField(Types.Product.Field._productCategoryId, Types.AcousticGuitar.Field._productCategoryId);
  40 
  41       // Set the number of Product ordered units to 200
  42       insertByQuery.AddInsertField(Types.Product.Field._orderedUnits, 200);
  43 
  44       // Specify which AcousticGuitar rows will be selected.
  45       // Select the AcousticGuitar instance that we created above.
  46       insertByQuery.Where.AddEqualTo(Types.AcousticGuitar.Field._productCategoryId, productCategory.PrimaryKey[0]);
  47 
  48       // Execute the insert
  49       insertByQuery.Execute();
  50 
  51       // DomainObjects generates the following SQL INSERT statement when insertByQuery.Execute() is called:
  52       //
  53       //  INSERT INTO PRODUCT
  54       //       (
  55       //         NAME,
  56       //         PRODUCT_CATEGORY_ID,
  57       //         ORDERED_UNITS
  58       //       )
  59       //  SELECT  GUITARS01.BodyDesc            AS GUITARS01_BodyDesc,
  60       //          GUITARS01.PRODUCT_CATEGORY_ID AS GUITARS01_PRODUCT_CATEGORY_ID,
  61       //          @_0
  62       //  FROM    GUITARS AS GUITARS01
  63       //  WHERE ((GUITARS01.PRODUCT_CATEGORY_ID = @GUITARS01_PRODUCT_CATEGORY_ID_0)
  64       //      AND ((GUITARS01.ConcreteClass IN ( @GUITARS01_ConcreteClass_1, @GUITARS01_ConcreteClass_2)))),
  65 
  66       //  @GUITARS01_PRODUCT_CATEGORY_ID_0=343,
  67       //  @GUITARS01_ConcreteClass_1='DomainObjects.Test.Domain.AcousticGuitar',
  68       //  @GUITARS01_ConcreteClass_2='DomainObjects.Test.Domain.SpanishAcousticGuitar',
  69       //  @_0=200
  70 
  71 
  72       // Validate that a new Product was inserted.
  73       Assert.AreEqual(productCountBeforeInsertByQuery + 1, Query.GetCount<Product>(null), "The InsertByQuery inserted a Product instance.");
  74     }
  75   }
  76 }