Example #1
0
        public void Can_output_templates_to_be_customized()
        {
            var serviceCollection = SetupInitialServices();
            var logger            = new InMemoryCommandLogger("E2ETest");

            serviceCollection.AddScoped(typeof(ILogger), sp => logger);
            var fileService = new InMemoryFileService();

            serviceCollection.AddScoped(typeof(IFileService), sp => fileService);

            var provider = GetMetadataModelProvider(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var generator       = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            var filePaths       = generator.Customize(provider, TestOutputDir);

            Assert.Equal(0, logger.WarningMessages.Count);
            Assert.Equal(0, logger.InformationMessages.Count);
            Assert.Equal(0, logger.VerboseMessages.Count);
            Assert.Equal(2, filePaths.Count);
            Assert.Equal(Path.Combine(TestOutputDir, ProviderDbContextTemplateName), filePaths[0]);
            Assert.Equal(Path.Combine(TestOutputDir, ProviderEntityTypeTemplateName), filePaths[1]);

            var dbContextTemplateContents = fileService.RetrieveFileContents(
                TestOutputDir, ProviderDbContextTemplateName);

            Assert.Equal(provider.DbContextTemplate, dbContextTemplateContents);

            var entityTypeTemplateContents = fileService.RetrieveFileContents(
                TestOutputDir, ProviderEntityTypeTemplateName);

            Assert.Equal(provider.EntityTypeTemplate, entityTypeTemplateContents);
        }
Example #2
0
        public void E2ETest()
        {
            // set current cultures to English because expected results for error messages
            // (both those output to the Logger and those put in comments in the .cs files)
            // are in English
#if DNXCORE50
            CultureInfo.CurrentCulture   = new CultureInfo("en-US");
            CultureInfo.CurrentUICulture = new CultureInfo("en-US");
#else
            Thread.CurrentThread.CurrentCulture   = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
#endif

            var assembly      = Assembly.Load(new AssemblyName(DatabaseTool._defaultReverseEngineeringProviderAssembly));
            var configuration = new ReverseEngineeringConfiguration
            {
                ProviderAssembly = assembly,
                ConnectionString = E2EConnectionString,
                Namespace        = TestNamespace,
                OutputPath       = TestOutputDir
            };

            var serviceProvider = SetupServiceProvider();
            var fileService     = new InMemoryFileService();
            serviceProvider.AddService(typeof(IFileService), fileService);
            var logger = new InMemoryCommandLogger("E2ETest");
            serviceProvider.AddService(typeof(ILogger), logger);

            var expectedFileContents = InitializeExpectedFileContents();

            var generator = new ReverseEngineeringGenerator(serviceProvider);
            var filePaths = generator.GenerateAsync(configuration).Result;

            Assert.Equal(_E2ETestExpectedWarnings.Count, logger.WarningMessages.Count);
            // loop over warnings instead of using the collection form of Assert.Equal()
            // to give better error messages if it does fail. Similarly for file paths below.
            var i = 0;
            foreach (var expectedWarning in _E2ETestExpectedWarnings)
            {
                Assert.Equal(expectedWarning, logger.WarningMessages[i++]);
            }
            Assert.Equal(0, logger.InformationMessages.Count);
            Assert.Equal(0, logger.VerboseMessages.Count);

            var expectedFilePaths = _E2ETestExpectedFileNames.Select(name => TestOutputDir + @"\" + name);
            Assert.Equal(expectedFilePaths.Count(), filePaths.Count);
            i = 0;
            foreach (var expectedFilePath in expectedFilePaths)
            {
                Assert.Equal(expectedFilePath, filePaths[i++]);
            }

            foreach (var fileName in _E2ETestExpectedFileNames)
            {
                var fileContents = fileService.RetrieveFileContents(TestOutputDir, fileName);
                Assert.Equal(expectedFileContents[fileName], fileContents);
            }
        }
Example #3
0
        public void E2ETest()
        {
            SetCurrentCulture();

            var serviceCollection = SetupInitialServices();
            var logger            = new InMemoryCommandLogger("E2ETest");

            serviceCollection.AddScoped(typeof(ILogger), sp => logger);
            var fileService = new InMemoryFileService();

            serviceCollection.AddScoped(typeof(IFileService), sp => fileService);

            var provider = GetMetadataModelProvider(serviceCollection);

            var configuration = new ReverseEngineeringConfiguration
            {
                Provider           = provider,
                ConnectionString   = E2EConnectionString,
                Namespace          = TestNamespace,
                CustomTemplatePath = null, // not used for this test
                OutputPath         = TestOutputDir
            };

            var expectedFileContents = InitializeE2EExpectedFileContents();

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var generator       = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            var filePaths       = generator.GenerateAsync(configuration).Result;

            Assert.Equal(_E2ETestExpectedWarnings.Count, logger.WarningMessages.Count);
            // loop over warnings instead of using the collection form of Assert.Equal()
            // to give better error messages if it does fail. Similarly for file paths below.
            var i = 0;

            foreach (var expectedWarning in _E2ETestExpectedWarnings)
            {
                Assert.Equal(expectedWarning, logger.WarningMessages[i++]);
            }
            Assert.Equal(0, logger.InformationMessages.Count);
            Assert.Equal(0, logger.VerboseMessages.Count);

            var expectedFilePaths = _E2ETestExpectedFileNames.Select(name => Path.Combine(TestOutputDir, name));

            Assert.Equal(expectedFilePaths.Count(), filePaths.Count);
            i = 0;
            foreach (var expectedFilePath in expectedFilePaths)
            {
                Assert.Equal(expectedFilePath, filePaths[i++]);
            }

            var listOfFileContents = new List <string>();

            foreach (var fileName in _E2ETestExpectedFileNames)
            {
                var fileContents = fileService.RetrieveFileContents(TestOutputDir, fileName);
                Assert.Equal(expectedFileContents[fileName], fileContents);
                listOfFileContents.Add(fileContents);
            }

            // compile generated code
            var metadataReferencesProvider =
                (MetadataReferencesProvider)serviceProvider.GetService(typeof(MetadataReferencesProvider));
            var metadataReferences       = SetupMetadataReferencesForCompilationOfGeneratedCode(metadataReferencesProvider);
            var roslynCompilationService = new RoslynCompilationService();
            var compilationResult        =
                roslynCompilationService.Compile(listOfFileContents, metadataReferences);

            if (compilationResult.Messages.Any())
            {
                _output.WriteLine("Compilation Errors from compiling generated code");
                _output.WriteLine("================================================");
                foreach (var message in compilationResult.Messages)
                {
                    _output.WriteLine(message);
                }
                _output.WriteLine("================================================");
                Assert.Equal(string.Empty, "See Compilation Errors in Output.");
            }
        }
Example #4
0
        public void Code_generation_will_use_customized_templates_if_present()
        {
            SetCurrentCulture();

            var serviceCollection = SetupInitialServices();
            var logger            = new InMemoryCommandLogger("E2ETest");

            serviceCollection.AddScoped(typeof(ILogger), sp => logger);
            var fileService = new InMemoryFileService();

            serviceCollection.AddScoped(typeof(IFileService), sp => fileService);
            InitializeCustomizedTemplates(fileService);

            var provider = GetMetadataModelProvider(serviceCollection);

            var configuration = new ReverseEngineeringConfiguration
            {
                Provider           = provider,
                ConnectionString   = E2EConnectionString,
                Namespace          = TestNamespace,
                CustomTemplatePath = CustomizedTemplateDir,
                OutputPath         = TestOutputDir
            };

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var generator       = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            var filePaths       = generator.GenerateAsync(configuration).Result;

            Assert.Equal(_E2ETestExpectedWarnings.Count, logger.WarningMessages.Count);
            // loop over warnings instead of using the collection form of Assert.Equal()
            // to give better error messages if it does fail. Similarly for file paths below.
            var i = 0;

            foreach (var expectedWarning in _E2ETestExpectedWarnings)
            {
                Assert.Equal(expectedWarning, logger.WarningMessages[i++]);
            }

            Assert.Equal(_CustomizedTemplatesTestExpectedInfos.Count, logger.InformationMessages.Count);
            i = 0;
            foreach (var expectedInfo in _CustomizedTemplatesTestExpectedInfos)
            {
                Assert.Equal(expectedInfo, logger.InformationMessages[i++]);
            }

            Assert.Equal(0, logger.VerboseMessages.Count);

            var expectedFilePaths = _E2ETestExpectedFileNames.Select(name => Path.Combine(TestOutputDir, name));

            Assert.Equal(expectedFilePaths.Count(), filePaths.Count);
            i = 0;
            foreach (var expectedFilePath in expectedFilePaths)
            {
                Assert.Equal(expectedFilePath, filePaths[i++]);
            }

            var listOfFileContents = new List <string>();

            foreach (var fileName in _E2ETestExpectedFileNames)
            {
                var fileContents = fileService.RetrieveFileContents(TestOutputDir, fileName);
                if ("SqlServerReverseEngineerTestE2EContext.cs" == fileName)
                {
                    Assert.Equal(CustomDbContextTemplateContents, fileContents);
                }
                else
                {
                    Assert.Equal(CustomEntityTypeTemplateContents, fileContents);
                }
            }
        }