public void WriteTest()
        {
            var valueProvider = A.Fake<IValueProviderFabric>();
            var cache = A.Fake<ICache>();
            var insertDataStrategy = A.Fake<IStrategy<DatabaseData>>();

            var reader = new SchemaDescriptionWriter(cache, insertDataStrategy, valueProvider);

            var description = new DocumentDescription() {
                Settings = new Dictionary<string, string> { { "Iteration", "1" } },
            };
            description.EntityDescriptions = new List<EntityDescription>() {
                new EntityDescription {
                    EntityName = "EntityName",
                    PropertyDescriptions = new List<PropertyDescription>()
                    {
                        new PropertyDescription {
                            PropertyName = "PropertyName",
                            ValueProviderName = "ConstStringProvider",
                            Settings = new Dictionary<string, string> { { "Value", "1" } }
                        }
                    }
                }
            };
            reader.Write(description);

            A.CallTo(() => insertDataStrategy.Execute(A<DatabaseData>.Ignored)).MustHaveHappened();
        }
        public void ReadTest()
        {
            var dd = new DocumentDescription();

            var jsonReader = A.Fake<IReadableRepository<DocumentDescription>>();
            A.CallTo(() => jsonReader.Read()).Returns(dd);

            var reader = new SchemaDescriptionJsonReader(jsonReader);

            var result = reader.Read();

            Assert.AreEqual(dd, result);
        }
Esempio n. 3
0
        public void Execute()
        {
            var mapper = new TypeMapper();
            var database = new Document();

            database.Settings[Keys.Namespace] = "dbo";
            if (generateUpdateSettings)
            {
                database.Settings[Keys.Update] = "true";
            }
            database.Name = sqlConnectionBuilder.InitialCatalog;

            var tables = new List<Entity>();
            ExecuteReader(string.Format(readTables, database.Name, database.Settings[Keys.Namespace]), (record) =>
            {
                tables.Add(new Entity { Name = (string)record[0] });
            });

            foreach (var table in tables)
            {
                var columns = new List<Property>();
                ExecuteReader(string.Format(readColumns, database.Name, database.Settings[Keys.Namespace], table.Name), (record) =>
                {
                    var pr = new Property { Name = (string)record[0], PropertyType = (string)record[1] + GetColumnLength(record) };
                    pr.Settings[Keys.IsNotSupportNull] = record[3].ToString() == "NO" ? "true" : "false";
                    columns.Add(pr);
                });
                ExecuteReader(string.Format(readPrimaryColumn, database.Name, database.Settings[Keys.Namespace], table.Name), (record) =>
                {
                    columns.Single(x => x.Name == record[0].ToString()).Settings[Keys.IsPrimary] = "true";
                });

                ExecuteReader(string.Format(readIdentityColumn, database.Name, database.Settings[Keys.Namespace], table.Name), (record) =>
                {
                    columns.Single(x => x.Name == record[0].ToString()).Settings[Keys.Identity] = string.Format("({0},{1})", record[1], record[2]);
                });

                var relations = new List<Relation>();
                ExecuteReader(string.Format(readForeignKeys, database.Name, database.Settings[Keys.Namespace], table.Name), (record) =>
                {
                    relations.Add(new Relation { PrimaryEntity = table.Name, PrimaryProperty = record[1].ToString(), ForeignEntity = record[2].ToString(), ForeignProperty = record[3].ToString() });
                });
                table.Relations = relations;

                table.Properties = columns;
            }

            database.Entities = tables;

            WriteToFile(database);

            var databaseDescription = new DocumentDescription();
            databaseDescription.DatabaseName = database.Name;

            databaseDescription.Settings[Keys.Iteration] = "1000";
            databaseDescription.Settings[Keys.DateTimeFormat] = "yyyy-MM-dd HH:mm:ss";
            databaseDescription.Settings[Keys.DateTimeMinValue] = "2010-01-01 00:00:00";
            databaseDescription.Settings[Keys.DateTimeMaxValue] = "2020-01-01 00:00:00";
            var entityDescriptions = new List<EntityDescription>();
            foreach (var entity in database.Entities)
            {
                var entityDescription = new EntityDescription();
                entityDescription.EntityName = entity.Name;
                entityDescription.NumberOfRecords = 1;
                var propertyDescriptions = new List<PropertyDescription>();
                foreach (var property in entity.Properties)
                {
                    var propertyDescription = new PropertyDescription();
                    propertyDescription.PropertyName = property.Name;
                    propertyDescription.ValueProviderName = "ConstStringProvider";

                    var type = GetDefault(mapper.GetTypeForMap(property.PropertyType));
                    propertyDescription.Settings[Keys.Value] = type == null ? string.Empty : type.ToString();
                    propertyDescriptions.Add(propertyDescription);
                }
                entityDescription.PropertyDescriptions = propertyDescriptions;
                entityDescriptions.Add(entityDescription);
            }
            databaseDescription.EntityDescriptions = entityDescriptions;

            WriteToFile(databaseDescription);
        }
Esempio n. 4
0
 private void WriteToFile(DocumentDescription databaseDescription)
 {
     using (var f = File.CreateText("schemaDescription.json"))
     {
         f.Write(JsonConvert.SerializeObject(databaseDescription, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new WritablePropertiesOnlyResolver() }));
     }
 }