コード例 #1
0
        public void FluentModelBuilder_WithAdHocModel_ShouldBuildExpectedModel()
        {
            // arrange

            // act
            var model = new CodeGenerationModel()
                        .SetCurrentNamespace("MSJennings.Quizzes")

                        .AddEntity("Quiz")
                        .AddProperty("Id", ModelPropertyLogicalType.Integer, isRequired: true)
                        .AddProperty("Name", ModelPropertyLogicalType.String)
                        .AddProperty("CreatedDate", ModelPropertyLogicalType.DateAndTime)
                        .AddProperty("IsActive", ModelPropertyLogicalType.Boolean)
                        .AddProperty("PassingScore", ModelPropertyLogicalType.Decimal)
                        .AddListProperty("Topics", ModelPropertyLogicalType.String)
                        .AddListProperty("Questions", "Question", isRequired: true)

                        .AddEntity("Question")
                        .AddProperty("Id", ModelPropertyLogicalType.Integer)
                        .AddProperty("Prompt", ModelPropertyLogicalType.String)
                        .AddListProperty("Choices", ModelPropertyLogicalType.KeyValuePair, isRequired: true)
                        .AddProperty("CorrectChoice", ModelPropertyLogicalType.Character)
                        .AddListProperty("QuizIds", ModelPropertyLogicalType.Integer);

            // assert
            AssertIsValidModel(model);
        }
コード例 #2
0
        private void GenerateCode(CodeGenerationModel model, Project prj)
        {
            if (prj != null)
            {
                try
                {
                    var generator           = new Generator(prj, model.Solution);
                    GenerationResult result = generator.Generate(model.Destination);
                    switch (result)
                    {
                    case GenerationResult.Success:
                        MessageBox.Show(Strings.CodeGenerationCompleted, Strings.CodeGeneration, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;

                    case GenerationResult.Error:
                        MessageBox.Show(Strings.CodeGenerationFailed, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Strings.UnknownError,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
コード例 #3
0
        public void LoadFromAssembly_WithAssemblyFileName_ShouldLoadExpectedModel()
        {
            // arrange
            var model = new CodeGenerationModel();

            var testAssembly         = typeof(Quiz).Assembly;
            var testAssemblyFileName = testAssembly.Location;

            // act
            model.LoadFromAssembly(testAssemblyFileName);

            // assert
            AssertIsValidModel(model);
        }
コード例 #4
0
        public void ToModelPropertyType_WithObject_ShouldReturnExpectedResult()
        {
            // Arrange
            var testValue     = new CodeGenerationModel();
            var testValueType = testValue.GetType();

            // Act
            var modelPropertyType = testValueType.ToModelPropertyType();

            // Assert
            Assert.Equal(ModelPropertyLogicalType.Object, modelPropertyType.LogicalType);
            Assert.Equal(testValueType.Name, modelPropertyType.ObjectTypeName);
            Assert.Null(modelPropertyType.ListItemType);
        }
コード例 #5
0
        public void LoadFromOtherModel_WithExistingModel_ShouldLoadExpectedModel()
        {
            // arrange
            var originalModel = new CodeGenerationModel();

            originalModel.LoadFromTypes(
                typeof(Quiz),
                typeof(Question));

            var newModel = new CodeGenerationModel();

            // act
            newModel.LoadFromOtherModel(originalModel);

            // assert
            AssertIsValidModel(newModel);
        }
コード例 #6
0
        public void LoadFromTypes_WithListOfTypes_ShouldLoadExpectedModel()
        {
            // arrange
            var model = new CodeGenerationModel();

            var types = new List <Type>
            {
                typeof(Quiz),
                typeof(Question)
            };

            // act
            model.LoadFromTypes(types);

            // assert
            AssertIsValidModel(model);
        }
コード例 #7
0
        private void AssertIsValidModel(CodeGenerationModel model)
        {
            Assert.NotNull(model);
            Assert.Equal(1, model.Namespaces.Count);

            // assert that the number of enties is correct
            Assert.Equal(2, model.Entities.Count());

            // assert that the expected entities are present
            Assert.Contains(model.Entities, x => x.Name.Equals("Quiz", StringComparison.Ordinal));
            Assert.Contains(model.Entities, x => x.Name.Equals("Question", StringComparison.Ordinal));

            var quizEntity     = model.Entities.First(x => x.Name.Equals("Quiz", StringComparison.Ordinal));
            var questionEntity = model.Entities.First(x => x.Name.Equals("Question", StringComparison.Ordinal));

            // assert that entities have the expected number of properties
            Assert.Equal(7, quizEntity.Properties.Count);
            Assert.Equal(5, questionEntity.Properties.Count);

            // assert that entities have the expected properties and property types
            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("Id", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Integer &&
                            x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("Name", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.String &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("CreatedDate", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.DateAndTime &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("IsActive", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Boolean &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("PassingScore", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Decimal &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("Topics", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.List &&
                            x.PropertyType.ListItemType.LogicalType == ModelPropertyLogicalType.String &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("Questions", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.List &&
                            x.PropertyType.ListItemType.ObjectTypeName.Equals("Question", StringComparison.Ordinal) &&
                            x.IsRequired);

            Assert.Contains(questionEntity.Properties, x =>
                            x.Name.Equals("Choices", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.List &&
                            x.PropertyType.ListItemType.LogicalType == ModelPropertyLogicalType.KeyValuePair &&
                            x.IsRequired);

            Assert.Contains(questionEntity.Properties, x =>
                            x.Name.Equals("CorrectChoice", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Character &&
                            !x.IsRequired);

            Assert.Contains(questionEntity.Properties, x =>
                            x.Name.Equals("QuizIds", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.List &&
                            x.PropertyType.ListItemType.LogicalType == ModelPropertyLogicalType.Integer &&
                            !x.IsRequired);
        }
        public void LoadFromSqlDatabase_WithSqlDatabase_ShouldLoadExpectedModel()
        {
            // Arrange
            var database = new SqlDatabase();

            database.Tables.Add(new SqlTable("Quiz")
            {
                Database = database, Schema = new SqlSchemaName("Quizzes")
            });
            var quizTable = database.Tables.Get("Quizzes", "Quiz");

            quizTable.Columns.Add(new SqlTableColumn("Id")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "INT"
                }, IsNullable = false
            });
            quizTable.Columns.Add(new SqlTableColumn("Name")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "VARCHAR", MaxLength = 50
                }, IsNullable = true
            });
            quizTable.Columns.Add(new SqlTableColumn("CreatedDate")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "DATE"
                }, IsNullable = true
            });
            quizTable.Columns.Add(new SqlTableColumn("IsActive")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "BIT"
                }, IsNullable = true
            });
            quizTable.Columns.Add(new SqlTableColumn("PassingScore")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "NUMERIC", Precision = 9, Scale = 2
                }, IsNullable = true
            });

            database.Tables.Add(new SqlTable("Question")
            {
                Database = database, Schema = new SqlSchemaName("Quizzes")
            });
            var questionTable = database.Tables.Get("Quizzes", "Question");

            questionTable.Columns.Add(new SqlTableColumn("Id")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "INT"
                }, IsNullable = false
            });
            questionTable.Columns.Add(new SqlTableColumn("Prompt")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "VARCHAR", MaxLength = 100
                }, IsNullable = true
            });
            questionTable.Columns.Add(new SqlTableColumn("CorrectChoice")
            {
                DataType = new SqlDataType {
                    SqlTypeName = "CHAR", MaxLength = 1
                }, IsNullable = false
            });

            var model = new CodeGenerationModel();

            // Act
            model.LoadFromSqlDatabase(database);

            // Assert
            Assert.NotNull(model);
            Assert.Equal(1, model.Namespaces.Count);

            // assert that the number of enties is correct
            Assert.Equal(2, model.Entities.Count());

            // assert that the expected entities are present
            Assert.Contains(model.Entities, x => x.Name.Equals("Quiz", StringComparison.Ordinal));
            Assert.Contains(model.Entities, x => x.Name.Equals("Question", StringComparison.Ordinal));

            var quizEntity     = model.Entities.First(x => x.Name.Equals("Quiz", StringComparison.Ordinal));
            var questionEntity = model.Entities.First(x => x.Name.Equals("Question", StringComparison.Ordinal));

            // assert that entities have the expected number of properties
            Assert.Equal(5, quizEntity.Properties.Count);
            Assert.Equal(3, questionEntity.Properties.Count);

            // assert that entities have the expected properties and property types
            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("Id", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Integer &&
                            x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("Name", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.String &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("CreatedDate", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Date &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("IsActive", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Boolean &&
                            !x.IsRequired);

            Assert.Contains(quizEntity.Properties, x =>
                            x.Name.Equals("PassingScore", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Decimal &&
                            !x.IsRequired);

            Assert.Contains(questionEntity.Properties, x =>
                            x.Name.Equals("CorrectChoice", StringComparison.Ordinal) &&
                            x.PropertyType.LogicalType == ModelPropertyLogicalType.Character &&
                            x.IsRequired);
        }
コード例 #9
0
 public CodeGenerationContext(CodeGenerationModel model, CodeWriter codeWriter)
 {
     CodeWriter = codeWriter ?? new CodeWriter();
     Model      = model ?? new CodeGenerationModel();
 }