1 using DomainObjects.Facade.Command; 2 using DomainObjects.Facade.Command.Function; 3 using DomainObjects.Test.Domain; 4 using NUnit.Framework; 5 using System; 6 7 namespace DomainObjects.Test.TestFixture 8 { 9 /// <summary> 10 /// Tests INSERTs. 11 /// </summary> 12 [TestFixture] 13 public class InsertTests : InternalDomainObjectsTestFixture 14 { 15 [Category(UnitTestCategory.DoesNotWorkWithServerSideSequencing)] 16 [Category(UnitTestCategory.DoesNotWorkWithOracle)] 17 [Test] 18 public void ExecuteInsertByQuery() 19 { 20 // Create and persist an AcousticGuitar to set up the unit test. 21 // Values from this instance will be selected and inserted into the 22 // PRODUCT table via an InsertByQuery command. 23 ProductCategory productCategory = new ProductCategory("Nylon String Guitars", "Acoustic Guitars that have nylon strings").Manage<ProductCategory>(); 24 AcousticGuitar acousticGuitar = new AcousticGuitar(productCategory, "McPherson", 6, "Nylon Classical(N) body shape").Manage<AcousticGuitar>(); 25 PersistenceFacade.Persist(acousticGuitar, productCategory); 26 27 // For assertion purposes later, let's see how many Products we 28 // have in the database. At the end of the test we should have inserted 29 // one more. Passing 'null' for the Criteria argument means 'select all rows'. 30 int productCountBeforeInsertByQuery = Query.GetCount<Product>(null); 31 32 // Create an InsertByQuery command that will insert Product values 33 // that are selected from the AcousticGuitar instance. 34 InsertByQuery<Product, AcousticGuitar> insertByQuery = new InsertByQuery<Product, AcousticGuitar>(); 35 36 // Select the AcousticGuitar body description value and assign it to the Product name. 37 insertByQuery.AddInsertField(Types.Product.Field._name, Types.AcousticGuitar.Field._bodyDescription); 38 39 // Associate the Product with the same ProductCategory as the AcousticGuitar. 40 insertByQuery.AddInsertField(Types.Product.Field._productCategoryId, Types.AcousticGuitar.Field._productCategoryId); 41 42 // Set the number of Product ordered units to 200 43 insertByQuery.AddInsertField(Types.Product.Field._orderedUnits, 200); 44 45 // Specify which AcousticGuitar rows will be selected. 46 // Select the AcousticGuitar instance that we created above. 47 insertByQuery.Where.AddEqualTo(Types.AcousticGuitar.Field._productCategoryId, productCategory.PrimaryKey[0]); 48 49 // Execute the insert 50 insertByQuery.Execute(); 51 52 // DomainObjects generates the following SQL INSERT statement when insertByQuery.Execute() is called: 53 // 54 // INSERT INTO PRODUCT 55 // ( 56 // NAME, 57 // PRODUCT_CATEGORY_ID, 58 // ORDERED_UNITS 59 // ) 60 // SELECT GUITARS01.BodyDesc AS GUITARS01_BodyDesc, 61 // GUITARS01.PRODUCT_CATEGORY_ID AS GUITARS01_PRODUCT_CATEGORY_ID, 62 // @_0 63 // FROM GUITARS AS GUITARS01 64 // WHERE ((GUITARS01.PRODUCT_CATEGORY_ID = @GUITARS01_PRODUCT_CATEGORY_ID_0) 65 // AND ((GUITARS01.ConcreteClass IN ( @GUITARS01_ConcreteClass_1, @GUITARS01_ConcreteClass_2)))), 66 67 // @GUITARS01_PRODUCT_CATEGORY_ID_0=343, 68 // @GUITARS01_ConcreteClass_1='DomainObjects.Test.Domain.AcousticGuitar', 69 // @GUITARS01_ConcreteClass_2='DomainObjects.Test.Domain.SpanishAcousticGuitar', 70 // @_0=200 71 72 73 // Validate that a new Product was inserted. 74 Assert.AreEqual(productCountBeforeInsertByQuery + 1, Query.GetCount<Product>(null), "The InsertByQuery inserted a Product instance."); 75 } 76 77 [Test] 78 public void InsertWithComputedColumn() 79 { 80 Product product = CreateProduct(); 81 product.SetComputedUnitsForUnitTest(20); 82 PersistenceFacade.Persist(product); 83 ClearAppDomainAndContextCache(); 84 Assert.AreEqual(234 + 17, product.ComputedUnits, "The computed units value of '20' was not inserted into the database."); 85 } 86 87 /// <summary> 88 /// This test passes if no exception is thrown. This case was 89 /// throwing an exception because only unique items (incorrectly) were being 90 /// added to the select field sequence. 91 /// </summary> 92 [Category(UnitTestCategory.DoesNotWorkWithServerSideSequencing)] 93 [Category(UnitTestCategory.DoesNotWorkWithOracle)] 94 [Test] 95 public void InsertByQueryUsingMultipleGetDateFunctions() 96 { 97 InsertByQuery<BaseTypeReference, BaseTypeReference> insertByQuery = new InsertByQuery<BaseTypeReference, BaseTypeReference>(); 98 insertByQuery.AddInsertField(Types.BaseTypeReference.Field._nullableType, new GetDate()); 99 insertByQuery.AddInsertField(Types.BaseTypeReference.Field._nullableDateTime, new GetDate()); 100 insertByQuery.Top = 1; 101 insertByQuery.Execute(); 102 } 103 104 /// <summary> 105 /// Validates that identity insert can be enabled for an <see cref="InsertByQuery{InsertIntoClass,SelectFromClass}"/> statement. 106 /// </summary> 107 [Category(UnitTestCategory.DoesNotWorkWithServerSideSequencing)] 108 [Category(UnitTestCategory.DoesNotWorkWithOracle)] 109 [Test] 110 public void SetIdentityInsertOn() 111 { 112 InsertByQuery<TableFour, Product> insertByQuery = new InsertByQuery<TableFour, Product>(); 113 long primaryKey = DateTime.Now.Ticks; 114 insertByQuery.AddInsertField(Types.TableFour.Field._primaryKey, primaryKey); 115 insertByQuery.AddInsertField(Types.TableFour.Field._referenceType, new ReferenceType(DateTime.Now.Ticks.ToString())); 116 insertByQuery.AddInsertField(Types.TableFour.Field._tableFourValueOne, DateTime.Now); 117 insertByQuery.SetIdentityInsertOn = true; 118 insertByQuery.Top = 1; 119 insertByQuery.Execute(); 120 121 Query<TableFour> tableFourQuery = new Query<TableFour>(); 122 tableFourQuery.Where.AddEqualTo(Types.TableFour.Field._primaryKey, primaryKey); 123 Assert.IsTrue(tableFourQuery.Exists(), "The object was inserted with identity insert enabled."); 124 } 125 } 126 }