Ejemplo n.º 1
0
        public void BasicSyncTest()
        {
            // Arrange

            StringBuilder trace;

            var sqlMock = new Mock <ISql>();

            sqlMock.Setup(m => m.GetAllTables())
            .Returns(() => new List <(string fullName, int objectId)>
            {
                ("dbo.Dogs", 1),
                ("dbo.Cats", 2)
            });
            sqlMock.Setup(m => m.GetStoredProcsThatRefEntity(It.IsAny <string>()))
            .Returns(() => new List <(string fullName, int objectId)>
            {
                ("dbo.GetDogsById", 3),
                ("dbo.DogsSave", 4),
                ("dbo.DogsUpdate", 5)
            });
            sqlMock.Setup(m => m.GetColumnNames(It.IsAny <int>()))
            .Returns(() => new string[]
            {
                "id",
                "name",
                "thing"
            });
            sqlMock.Setup(m => m.GetTableObjectId(It.IsAny <string>()))
            .Returns(() => new int?(1));

            var sync = new HarpSynchronizer(sqlMock.Object, new StringBuilder());

            var harpFile = new HarpFile();

            harpFile.Entities.Add("Dogs", new Entity
            {
                Name       = "Dogs",
                Table      = string.Empty, // blank
                Properties = new Dictionary <string, string>
                {
                    { "ID", string.Empty }, // blank
                    { "Name", string.Empty } // blank
                },
                Behaviors = new Dictionary <string, string>
                {
                    { "Get by id", string.Empty } // blank
                }
            });

            // Act
            var results = sync.Synchronize(harpFile);

            // Assert
            Assert.IsTrue(harpFile.IsFullyMapped);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var trace = new StringBuilder();

            try
            {
                var harpFilePath = Path.Combine(Environment.CurrentDirectory, "DataLayer.harp");
                Console.WriteLine(harpFilePath);

                // read
                var inYaml = File.ReadAllText(harpFilePath);
                var result = HarpFile.FromYaml(inYaml);
                Console.WriteLine($"Parse: {result.code}");

                if (result.code != HarpFile.ParseResult.OK)
                {
                    return;
                }

                // synchronize
                var sync    = new HarpSynchronizer(new Sql(), trace);
                var sResult = sync.Synchronize(result.file);
                Console.WriteLine($"Sync: {sResult.Code}");

                if (sResult.Code != HarpSynchronizer.SynchronizeResultCode.OK)
                {
                    return;
                }

                if (sResult.WasUpdated)
                {
                    var outYaml = result.file.GenerateYaml();
                    File.WriteAllText(harpFilePath, outYaml);
                }
            }
            finally
            {
                Console.WriteLine($"----- TRACE -----");
                Console.WriteLine(trace.ToString());
                Console.WriteLine($"-----------------");

                Console.ReadLine();
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            if (args == null || args.Count() < 2)
            {
                logAndTerminate("Both parameters must be specified: {command} {file}");
                return;
            }

            var cmd  = args[0];
            var file = args[1];

            if (string.IsNullOrWhiteSpace(cmd) || string.IsNullOrWhiteSpace(file))
            {
                logAndTerminate("Both parameters must be specified: {command} {file}");
                return;
            }

            var actionSync            = "sync";
            var actionSyncAndGenerate = "syncgen";
            var supportedActions      = new[]
            {
                actionSync,
                actionSyncAndGenerate
            };

            if (!supportedActions.Any(a => cmd.ToLower() == a.ToLower()))
            {
                logAndTerminate("Command not recognized.");
                return;
            }

            if (!File.Exists(file))
            {
                file = Path.Combine(Environment.CurrentDirectory, file);

                if (!File.Exists(file))
                {
                    logAndTerminate("Could not find the specified file");
                    return;
                }
            }

            if (file.EndsWith(".harp"))
            {
                logAndTerminate("File is not a .harp file");
                return;
            }

            var verboseOutput = (args.Length > 2);

            var sb   = new StringBuilder();
            var sync = new HarpSynchronizer(new Sql(), sb);

            // read file
            var harpYaml = File.ReadAllText(file);

            // load
            var loadResult = HarpFile.FromYaml(harpYaml);

            if (loadResult.code != HarpFile.ParseResult.OK)
            {
                logAndTerminate($"Could not load harp file: {loadResult.code}");
                return;
            }

            var syncSuccessful = false;

            var syncResult = sync.Synchronize(loadResult.file);

            if (syncResult.Code == HarpSynchronizer.SynchronizeResultCode.OK)
            {
                syncSuccessful = true;

                log("Sync was successful");

                if (syncResult.WasUpdated)
                {
                    log("Harp file was updated");
                }

                if (syncResult.UnmappedStoredProcs.Any())
                {
                    log("There are unmapped stored procedures");
                }

                if (syncResult.UnmappedTableColumns.Any())
                {
                    log("There are unmapped table columns");
                }
            }
            else
            {
                log($"Sync was NOT successful: {syncResult.Code}");

                if (syncResult.UnmappedStoredProcs.Any())
                {
                    log("There are also some unmapped stored procedures");
                }

                if (syncResult.UnmappedTableColumns.Any())
                {
                    log("There are also some unmapped table columns");
                }

                Environment.Exit(1);
            }

            if (cmd.ToLower() == actionSyncAndGenerate && syncSuccessful)
            {
                log("Not implemented");
                Environment.Exit(1);

                //var gen = new HarpGenerator();
                //gen.Generate(HarpFile,)
            }
        }