Exemplo n.º 1
0
        public void IndexRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();

            var valueField1 = objectTheorem.CreateInstance <ValueField>("ValueField1");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");

            // Act
            EFConstraints.AddIndexRuleConstraints(objectTheorem.ConstraintBuilder, stringType);

            objectTheorem.ConstraintBuilder
            .Assert(() => valueField1.Type == stringType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasIndexAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasMaxLengthAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void ComplexTypeConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Z3.ObjectTheorem.EF.Metamodel.Field>();
            objectTheorem.RegisterSuperType <Z3.ObjectTheorem.EF.Metamodel.Type>();

            var addressType = objectTheorem.CreateInstance <ReferenceType>("AddressType");
            var nameField   = objectTheorem.CreateInstance <ValueField>("NameField");

            // Act
            EFConstraints.AddComplexTypeRuleConstraints(objectTheorem.ConstraintBuilder);

            objectTheorem.ConstraintBuilder
            .Assert(() => !addressType.HasCollectionFields && !addressType.HasSingleFields)
            .Assert(() => addressType.ValueFields == new[] { nameField });

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => addressType.HasComplexTypeAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasComplexTypeAttribute = solved.GetValue(addressType, i => i.HasComplexTypeAttribute);

            Assert.IsTrue(HasComplexTypeAttribute);
        }
Exemplo n.º 3
0
        public void IdKeyDiscoveryConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Z3.ObjectTheorem.EF.Metamodel.Type>();
            var employeeType = objectTheorem.CreateInstance <ReferenceType>("EmployeeType");

            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.SetPossibleStringValues("Id", "Name", "Income", "ID", "EmployeeTypeId", "EmployeeTypeID");
            var idField     = objectTheorem.CreateInstance <ValueField>("IdField");
            var nameField   = objectTheorem.CreateInstance <ValueField>("NameField");
            var incomeField = objectTheorem.CreateInstance <ValueField>("IncomeField");

            // Act
            EFConstraints.AddPrimayKeyRuleConstraints(objectTheorem.ConstraintBuilder);

            objectTheorem.ConstraintBuilder
            .Assert(() => employeeType.IsEntity == true)
            .Assert(() => employeeType.ValueFields == new[] { idField, nameField, incomeField })
            .Assert(() => idField.Name == "Id")
            .Assert(() => idField.Owner == employeeType)
            .Assert(() => nameField.Name == "Name")
            .Assert(() => nameField.Owner == employeeType)
            .Assert(() => incomeField.Name == "Income")
            .Assert(() => incomeField.Owner == employeeType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => idField.HasKeyAttribute == false);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => nameField.HasKeyAttribute == false);
            var assume3 = objectTheorem.ConstraintBuilder.Assume(() => incomeField.HasKeyAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasKeyAttribute1 = solved.GetValue(idField, i => i.HasKeyAttribute);
            bool HasKeyAttribute2 = solved.GetValue(nameField, i => i.HasKeyAttribute);
            bool HasKeyAttribute3 = solved.GetValue(incomeField, i => i.HasKeyAttribute);

            Assert.IsFalse(HasKeyAttribute1);
            Assert.IsFalse(HasKeyAttribute2);
            Assert.IsFalse(HasKeyAttribute3);

            bool IsPrimaryKey1 = solved.GetValue(idField, i => i.IsPrimaryKey);
            bool IsPrimaryKey2 = solved.GetValue(nameField, i => i.IsPrimaryKey);
            bool IsPrimaryKey3 = solved.GetValue(incomeField, i => i.IsPrimaryKey);

            Assert.IsTrue(IsPrimaryKey1);
            Assert.IsFalse(IsPrimaryKey2);
            Assert.IsFalse(IsPrimaryKey3);
        }
Exemplo n.º 4
0
        public ObjectTheoremResult Solve(IEnumerable <INamedTypeSymbol> entityTypeSymbols)
        {
            _objectTheorem.RegisterSuperType <Metamodel.Type>();
            _objectTheorem.RegisterSuperType <Field>();
            _objectTheorem.RegisterSuperType <ReferenceField>();

            var stopwatch = Stopwatch.StartNew();

            var efMetamodelInstantiation = new EFMetamodelInstantiation(entityTypeSymbols, _objectTheorem, _classAssumptions, _propertyAssumptions);

            efMetamodelInstantiation.GenerateConstraints();

            stopwatch.Stop();
            Trace.WriteLine("Parsing & Metamodel Instantiation: " + stopwatch.Elapsed);

            EFConstraints.AddAll(_objectTheorem, efMetamodelInstantiation.CreatedTypes);

            return(_objectTheorem.Solve());
        }
Exemplo n.º 5
0
        public void IndexRule_Unsuccessful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();

            var valueField1 = objectTheorem.CreateInstance <ValueField>("ValueField1");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");

            // Act
            EFConstraints.AddIndexRuleConstraints(objectTheorem.ConstraintBuilder, stringType);

            objectTheorem.ConstraintBuilder
            .Assert(() => valueField1.Type == stringType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasIndexAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasMaxLengthAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);

            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasComplexTypeAttribute = solved.GetValue(valueField1, i => i.HasIndexAttribute);

            Assert.IsFalse(HasComplexTypeAttribute);
        }
        public void NotMappedRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.RegisterSuperType <ReferenceField>();

            var customerType = objectTheorem.CreateInstance <ReferenceType>("Customer1");
            var addressType  = objectTheorem.CreateInstance <ReferenceType>("Address1");

            var arrayOfAddressField = objectTheorem.CreateInstance <CollectionField>("AddressField");
            var namesField          = objectTheorem.CreateInstance <ValueField>("NamesField");

            var arrayOfStringType = objectTheorem.CreateInstance <ValueType>("CollectionOfStringType");

            // Act
            EFConstraints.AddNotMappedRuleConstraints(objectTheorem.ConstraintBuilder, new[] { arrayOfStringType });

            objectTheorem.ConstraintBuilder
            .Assert(() => customerType.ValueFields == new[] { namesField })
            .Assert(() => namesField.Owner == customerType)
            .Assert(() => namesField.Type == arrayOfStringType)
            .Assert(() => customerType.CollectionFields == new[] { arrayOfAddressField })
            .Assert(() => arrayOfAddressField.Owner == customerType)
            .Assert(() => arrayOfAddressField.Type == addressType)
            .Assert(() => addressType.HasComplexTypeAttribute == true);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => arrayOfAddressField.HasNotMappedAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => namesField.HasNotMappedAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void DatabaseGeneratedRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            var employeeType = objectTheorem.CreateInstance <ReferenceType>("EmployeeType");

            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.SetPossibleStringValues("Id", "ID", "EmployeeTypeId", "EmployeeTypeID");
            var idField = objectTheorem.CreateInstance <ValueField>("IdField");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");
            var numberType = objectTheorem.CreateInstance <ValueType>("NumberType");
            var guidType   = objectTheorem.CreateInstance <ValueType>("GuidType");

            // Act
            EFConstraints.AddPrimayKeyRuleConstraints(objectTheorem.ConstraintBuilder);
            EFConstraints.AddDatabaseGeneratedRuleConstraints(objectTheorem.ConstraintBuilder, stringType, numberType, guidType);

            objectTheorem.ConstraintBuilder
            .Assert(() => employeeType.IsEntity == true)
            .Assert(() => employeeType.ValueFields == new[] { idField })
            .Assert(() => idField.Name == "Id")
            .Assert(() => idField.Owner == employeeType)
            .Assert(() => idField.Type == numberType)
            .Assert(() => idField.DatabaseGeneratedAttribute == DatabaseGeneratedAttribute.None);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => idField.DatabaseGeneratedAttribute == DatabaseGeneratedAttribute.None);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }