def Generate(self, template_path: str, yapf_path: str):
        """Generating the files.

    Args:
      template_path (str): the path to the template directory
      yapf_path (str): the path to the yapf style file
    """
        self._output_handler.Confirm('Do you want to Generate the files?')

        database_suffix = os.path.splitext(self._testfile)[1][1:]
        sqlite_mapping_helper = mapping_helper.MappingHelper(
            template_path, yapf_path)

        generator = sqlite_generator.SQLiteGenerator(
            self._path, self._name, self._testfile, self._sql_query,
            self._output_handler, sqlite_plugin_helper.SQLitePluginHelper(),
            sqlite_plugin_path_helper.SQLitePluginPathHelper(
                self._path, self._name, database_suffix))

        generator.GenerateSQLitePlugin(
            template_path, file_handler.FileHandler(),
            formatter_init_mapping.FormatterInitMapping(sqlite_mapping_helper),
            parser_init_mapping.ParserInitMapping(sqlite_mapping_helper),
            parser_mapping.ParserMapper(sqlite_mapping_helper),
            formatter_mapping.FormatterMapper(sqlite_mapping_helper),
            parser_test_mapping.ParserTestMapper(sqlite_mapping_helper),
            formatter_test_mapping.FormatterTestMapper(sqlite_mapping_helper),
            sqlite_mapping_helper,
            sqlite_database_information.SQLiteDatabaseInformation(
                self._query_execution))
Ejemplo n.º 2
0
 def testCreateFileFromPath(self):
     """Tests if the creation of a file none existing beforhand works."""
     handler = file_handler.FileHandler()
     with tempfile.TemporaryDirectory() as tmpdir:
         source = os.path.join(tmpdir, self.file)
         self.assertFalse(os.path.exists(source))
         handler.CreateFileFromPath(source)
         self.assertTrue(os.path.exists(source))
Ejemplo n.º 3
0
 def testCreateFile(self):
     """Tests if the creation of a file none existing beforehand works."""
     with tempfile.TemporaryDirectory() as tmpdir:
         handler = file_handler.FileHandler()
         file_path = os.path.join(tmpdir, self.file)
         self.assertFalse(os.path.exists(file_path))
         handler.CreateFile(tmpdir, self.name, self.suffix)
         self.assertTrue(os.path.exists(file_path))
Ejemplo n.º 4
0
 def testCreateFolder(self):
     """Tests if the creation of a folder works."""
     with tempfile.TemporaryDirectory() as tmpdir:
         new_path = os.path.join(tmpdir, "newfolder")
         self.assertFalse(os.path.exists(new_path))
         handler = file_handler.FileHandler()
         handler._CreateFolder(new_path)  # pylint: disable=W0212
         actual = os.path.exists(new_path)
     self.assertTrue(actual)
Ejemplo n.º 5
0
 def testCreateFileIfPathNotExisting(self):
     """Tests if the creation of a file, none existing beforehand and folder
 not existing, works."""
     with tempfile.TemporaryDirectory() as tmpdir:
         new_path = os.path.join(tmpdir, "temp")
         handler = file_handler.FileHandler()
         file_path = os.path.join(new_path, self.file)
         self.assertFalse(os.path.exists(file_path))
         handler.CreateFile(new_path, self.name, self.suffix)
         self.assertTrue(os.path.exists(file_path))
 def testPrintCreate(self):
     """test the print for a create"""
     with tempfile.TemporaryDirectory() as tmpdir:
         fake_path_helper = (
             fake_sqlite_plugin_path_helper.FakeSQLitePluginPathHelper(
                 self.template_path, 'test', 'db'))
         path = os.path.join(tmpdir, 'testfile')
         generator = sqlite_generator.SQLiteGenerator(
             tmpdir, 'test', 'test', ['test'],
             output_handler_file.OutputHandlerFile(
                 path, file_handler.FileHandler()), self.plugin_helper,
             fake_path_helper)
         generator._PrintCreate(path)  # pylint: disable=protected-access
         expected = 'create ' + path
         actual = self._ReadFromFile(path)
     self.assertEqual(expected, actual)
Ejemplo n.º 7
0
    def testCopyFile(self):
        """Tests if the copying of a file none existing beforhand works."""
        expected_content = "this is test content."

        with tempfile.TemporaryDirectory() as tmpdir:
            source = os.path.join(tmpdir, self.file)
            destination = os.path.join(tmpdir, "copy", self.file)

            with open(source, "a") as f:
                f.write(expected_content)

            handler = file_handler.FileHandler()
            self.assertFalse(os.path.exists(destination))
            handler.CopyFile(source, destination)
            self.assertTrue(os.path.exists(destination))
            self.assertTrue(filecmp.cmp(destination, source))
Ejemplo n.º 8
0
    def testAddContentIfFileDoesNotExist(self):
        """Tests if the editing of a file not existing works."""
        content = "this is test content. "
        expected = content

        with tempfile.TemporaryDirectory() as tmpdir:
            source = os.path.join(tmpdir, self.file)
            handler = file_handler.FileHandler()
            self.assertFalse(os.path.exists(source))
            handler.AddContent(source, content)
            self.assertTrue(os.path.exists(source))

            with open(source, "r") as f:
                actual = f.read()

        self.assertEqual(expected, actual)
    def testGenerateSQLitePluginWithExistingInit(self):
        """test the output of a generation of a sqlite plugin"""
        fake_handler = fake_file_handler.FakeFileHandler()
        mapping_helper = fake_mapping_helper.FakeMappingHelper(
            self.template_path)
        init_formatter_mapper = fake_sqliteplugin_mapping.FakeSQLitePluginMapper(
            mapping_helper)
        init_parser_mapper = fake_sqliteplugin_mapping.FakeSQLitePluginMapper(
            mapping_helper)
        parser_mapper = fake_sqliteplugin_mapping.FakeSQLitePluginMapper(
            mapping_helper)
        formatter_mapper = fake_sqliteplugin_mapping.FakeSQLitePluginMapper(
            mapping_helper)
        parser_test_mapper = fake_sqliteplugin_mapping.FakeSQLitePluginMapper(
            mapping_helper)
        formatter_test_mapper = (
            fake_sqliteplugin_mapping.FakeSQLitePluginMapper(mapping_helper))
        fake_database_information = (
            fake_sqlite_database_information.FakeSQLiteDatabaseInformation([]))

        with tempfile.TemporaryDirectory() as tmpdir:
            fake_path_helper = (
                fake_sqlite_plugin_path_helper.FakeSQLitePluginPathHelper(
                    self.template_path, 'test', 'db'))
            path = os.path.join(tmpdir, 'testfile')

            generator = sqlite_generator.SQLiteGenerator(
                tmpdir, 'test', 'test', [],
                output_handler_file.OutputHandlerFile(
                    path, file_handler.FileHandler()), self.plugin_helper,
                fake_path_helper)

            generator.init_formatter_exists = True
            generator.init_parser_exists = True

            generator.GenerateSQLitePlugin(
                tmpdir, fake_handler, init_formatter_mapper,
                init_parser_mapper, parser_mapper, formatter_mapper,
                parser_test_mapper, formatter_test_mapper, mapping_helper,
                fake_database_information)
            expected = (
                'create testcreate testcreate testcreate testcopy testedit '
                'testedit test')
            actual = self._ReadFromFile(path)
        self.assertEqual(expected, actual)
Ejemplo n.º 10
0
    def testAddContentIfFileAndFolderDoesNotExist(self):
        """Tests if the method create or modify file with content works, if the
    file and Folder does not exist"""
        content = "this is test content. "
        expected = content

        with tempfile.TemporaryDirectory() as tmpdir:
            new_path = os.path.join(tmpdir, "newfolder")
            source = os.path.join(new_path, self.file)
            handler = file_handler.FileHandler()
            self.assertFalse(os.path.exists(source))
            handler.CreateOrModifyFileWithContent(source, content)
            self.assertTrue(os.path.exists(source))

            with open(source, "r") as f:
                actual = f.read()

        self.assertEqual(expected, actual)
    def testPrint(self):
        """test print"""
        with tempfile.TemporaryDirectory() as tmpdir:
            fake_path_helper = \
              fake_sqlite_plugin_path_helper.FakeSQLitePluginPathHelper(
                  self.template_path, 'test', 'db')
            path = os.path.join(tmpdir, 'testfile')
            generator = sqlite_generator.SQLiteGenerator(
                tmpdir, 'test', 'test', ['test'],
                output_handler_file.OutputHandlerFile(
                    path, file_handler.FileHandler()), self.plugin_helper,
                fake_path_helper)
            arguments = 'test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7'
            generator._Print(*arguments)  # pylint: disable=protected-access
            actual = self._ReadFromFile(path)

        expected = 'create test1create test2create test3create test4copy ' \
                   'test5create test6create test7'
        self.assertEqual(expected, actual)
Ejemplo n.º 12
0
    def testCreateOrModifyFileWithContentIfFileExists(self):
        """Tests if the method create or modify file with content works, if the
    file exists"""
        content = "this is test content. "
        expected = content + content

        with tempfile.TemporaryDirectory() as tmpdir:
            source = os.path.join(tmpdir, self.file)
            with open(source, "a") as f:
                f.write(content)

            handler = file_handler.FileHandler()
            self.assertTrue(os.path.exists(source))
            handler.CreateOrModifyFileWithContent(source, content)
            self.assertTrue(os.path.exists(source))

            with open(source, "r") as f:
                actual = f.read()

        self.assertEqual(expected, actual)
    def testNormalGenerate(self):
        """Test a normal generation."""
        expected_path = os.path.join(os.path.dirname(__file__),
                                     'expected_files_twitterdb')
        plugin_name = 'the_plugin'
        database_suffix = 'db'
        test_file = os.path.join(path_helper.TestDatabasePath(),
                                 'twitter_ios.db')
        with tempfile.TemporaryDirectory() as tmpdir:
            output_path = os.path.join(tmpdir, "temp")

            file_handler.FileHandler()._CreateFolder(output_path)

            output_console_path = os.path.join(output_path, 'prompts.txt')
            output_handler = output_handler_file.OutputHandlerFile(
                file_path=output_console_path,
                file_handler=file_handler.FileHandler(),
                confirm=True)

            sqlite_path_helper = sqlite_plugin_path_helper.SQLitePluginPathHelper(
                path=output_path,
                plugin_name=plugin_name,
                database_suffix=database_suffix)

            plugin_helper = sqlite_plugin_helper.SQLitePluginHelper()
            controller = sqlite_controller.SQLiteController(
                output_handler=output_handler, plugin_helper=plugin_helper)

            query_execution = sqlite_query_execution.SQLiteQueryExecution(
                test_file)
            query_execution.TryToConnect()

            query_first = 'select * from users'
            query_data_first = plugin_helper.RunSQLQuery(
                query_first, query_execution)
            data_first = plugin_helper.GetColumnsAndTimestampColumn(
                query_data_first.columns, ['createdDate', 'updatedAt'],
                query_data_first.data)
            query_data_first_timestamp = data_first[1]
            query_data_first_normal = data_first[0]
            query_data_first_normal[0].customize = True
            query_data_first_normal[1].customize = True
            query_data_first_normal[2].customize = True

            query_model_first = sql_query_model.SQLQueryModel(
                'select * from users', 'Users', query_data_first_normal,
                query_data_first_timestamp, True, len(query_data_first.data))

            query_second = 'select * from statuses'
            query_data_second = plugin_helper.RunSQLQuery(
                query_second, query_execution)
            data_second = plugin_helper.GetColumnsAndTimestampColumn(
                query_data_second.columns, ['date', 'updatedAt'],
                query_data_second.data)
            query_data_second_timestamp = data_second[1]
            query_data_second_normal = data_second[0]

            query_model_second = sql_query_model.SQLQueryModel(
                'select * from users', 'Statuses',
                query_data_second_normal, query_data_second_timestamp, False,
                len(query_data_second.data))

            sql_query = [query_model_first, query_model_second]

            controller._path = output_path
            controller._name = plugin_name
            controller._testfile = test_file
            controller._sql_query = sql_query
            controller._plugin_helper = plugin_helper
            controller._output_handler = output_handler
            controller._query_execution = query_execution

            controller.Generate(path_helper.TemplatePath(),
                                path_helper.YapfStyleFilePath())

            formatter_init = self._ReadFromFile(
                sqlite_path_helper.formatter_init_file_path)
            formatter = self._ReadFromFile(
                sqlite_path_helper.formatter_file_path)
            formatter_test = self._ReadFromFile(
                sqlite_path_helper.formatter_test_file_path)
            parser_init = self._ReadFromFile(
                sqlite_path_helper.parser_init_file_path)
            parser = self._ReadFromFile(sqlite_path_helper.parser_file_path)
            parser_test = self._ReadFromFile(
                sqlite_path_helper.parser_test_file_path)
            console_output = self._ReadFromFile(
                os.path.join(output_path, 'prompts.txt'))

            expected_formatter_init = self._ReadFromFile(
                os.path.join(expected_path, 'formatters_init.py'))
            expected_formatter = self._ReadFromFile(
                os.path.join(expected_path, 'formatters.py'))
            expected_formatter_test = self._ReadFromFile(
                os.path.join(expected_path, 'formatters_test.py'))
            expected_parser_init = self._ReadFromFile(
                os.path.join(expected_path, 'parsers_init.py'))
            expected_parser = self._ReadFromFile(
                os.path.join(expected_path, 'parsers.py'))
            expected_parser_test = self._ReadFromFile(
                os.path.join(expected_path, 'parsers_test.py'))
            expected_console_output = (
                'Do you want to Generate the files?create '
                '{0}create {1}create {2}create '
                '{3}copy {4}create {5}create {6}'.format(
                    sqlite_path_helper.formatter_file_path,
                    sqlite_path_helper.parser_file_path,
                    sqlite_path_helper.formatter_test_file_path,
                    sqlite_path_helper.parser_test_file_path,
                    sqlite_path_helper.database_path,
                    sqlite_path_helper.parser_init_file_path,
                    sqlite_path_helper.formatter_init_file_path))

            self.assertEqual(formatter_init, expected_formatter_init)
            self.assertEqual(formatter, expected_formatter)
            self.assertEqual(formatter_test, expected_formatter_test)
            self.assertEqual(parser_init, expected_parser_init)
            self.assertEqual(parser, expected_parser)
            self.assertEqual(parser_test, expected_parser_test)
            self.assertEqual(console_output, expected_console_output)
    def testNormalGenerateForAllTypes(self):
        """Test a normal generation."""
        expected_path = os.path.join(os.path.dirname(__file__),
                                     'expected_files_typesdb')
        plugin_name = 'the_plugin'
        database_suffix = 'db'
        test_file = os.path.join(path_helper.TestDatabasePath(),
                                 'test_database_types.db')
        with tempfile.TemporaryDirectory() as tmpdir:
            output_path = os.path.join(tmpdir, "temp")

            file_handler.FileHandler()._CreateFolder(output_path)

            output_console_path = os.path.join(output_path, 'prompts.txt')
            output_handler = output_handler_file.OutputHandlerFile(
                file_path=output_console_path,
                file_handler=file_handler.FileHandler(),
                confirm=True)

            sqlite_path_helper = sqlite_plugin_path_helper.SQLitePluginPathHelper(
                path=output_path,
                plugin_name=plugin_name,
                database_suffix=database_suffix)

            plugin_helper = sqlite_plugin_helper.SQLitePluginHelper()
            controller = sqlite_controller.SQLiteController(
                output_handler=output_handler, plugin_helper=plugin_helper)

            query_execution = sqlite_query_execution.SQLiteQueryExecution(
                test_file)
            query_execution.TryToConnect()

            query_blob = 'select * from blobtypes'
            query_integer = 'select * from integertypes'
            query_numeric = 'select * from numerictypes'
            query_real = 'select * from realtypes'
            query_text = 'select * from texttypes'
            query_no_data = 'select * from nodata'

            query_data_blob = plugin_helper.RunSQLQuery(
                query_blob, query_execution)
            query_data_integer = plugin_helper.RunSQLQuery(
                query_integer, query_execution)
            query_data_numeric = plugin_helper.RunSQLQuery(
                query_numeric, query_execution)
            query_data_real = plugin_helper.RunSQLQuery(
                query_real, query_execution)
            query_data_text = plugin_helper.RunSQLQuery(
                query_text, query_execution)
            query_data_no_data = plugin_helper.RunSQLQuery(
                query_no_data, query_execution)

            query_model_blob = sql_query_model.SQLQueryModel(
                query_blob, 'blobtypes', query_data_blob.columns, [], False, 0)
            query_model_integer = sql_query_model.SQLQueryModel(
                query_integer, 'integertypes', query_data_integer.columns, [],
                False, 0)
            query_model_numeric = sql_query_model.SQLQueryModel(
                query_numeric, 'numerictypes', query_data_numeric.columns, [],
                False, 0)
            query_model_real = sql_query_model.SQLQueryModel(
                query_real, 'realtypes', query_data_real.columns, [], False, 0)
            query_model_text = sql_query_model.SQLQueryModel(
                query_text, 'texttypes', query_data_text.columns, [], False, 0)
            query_model_no_data = sql_query_model.SQLQueryModel(
                query_no_data, 'nodata', query_data_no_data.columns, [], False,
                0)

            sql_query = [
                query_model_blob, query_model_integer, query_model_numeric,
                query_model_real, query_model_text, query_model_no_data
            ]

            controller._path = output_path
            controller._name = plugin_name
            controller._testfile = test_file
            controller._sql_query = sql_query
            controller._plugin_helper = plugin_helper
            controller._output_handler = output_handler
            controller._query_execution = query_execution

            controller.Generate(path_helper.TemplatePath(),
                                path_helper.YapfStyleFilePath())

            formatter_init = self._ReadFromFile(
                sqlite_path_helper.formatter_init_file_path)
            formatter = self._ReadFromFile(
                sqlite_path_helper.formatter_file_path)
            formatter_test = self._ReadFromFile(
                sqlite_path_helper.formatter_test_file_path)
            parser_init = self._ReadFromFile(
                sqlite_path_helper.parser_init_file_path)
            parser = self._ReadFromFile(sqlite_path_helper.parser_file_path)
            parser_test = self._ReadFromFile(
                sqlite_path_helper.parser_test_file_path)
            console_output = self._ReadFromFile(
                os.path.join(output_path, 'prompts.txt'))

            expected_formatter_init = self._ReadFromFile(
                os.path.join(expected_path, 'formatters_init.py'))
            expected_formatter = self._ReadFromFile(
                os.path.join(expected_path, 'formatters.py'))
            expected_formatter_test = self._ReadFromFile(
                os.path.join(expected_path, 'formatters_test.py'))
            expected_parser_init = self._ReadFromFile(
                os.path.join(expected_path, 'parsers_init.py'))
            expected_parser = self._ReadFromFile(
                os.path.join(expected_path, 'parsers.py'))
            expected_parser_test = self._ReadFromFile(
                os.path.join(expected_path, 'parsers_test.py'))
            expected_console_output = (
                'Do you want to Generate the files?create '
                '{0}create {1}create {2}create '
                '{3}copy {4}create {5}create {6}'.format(
                    sqlite_path_helper.formatter_file_path,
                    sqlite_path_helper.parser_file_path,
                    sqlite_path_helper.formatter_test_file_path,
                    sqlite_path_helper.parser_test_file_path,
                    sqlite_path_helper.database_path,
                    sqlite_path_helper.parser_init_file_path,
                    sqlite_path_helper.formatter_init_file_path))

            self.assertEqual(formatter_init, expected_formatter_init)
            self.assertEqual(formatter, expected_formatter)
            self.assertEqual(formatter_test, expected_formatter_test)
            self.assertEqual(parser_init, expected_parser_init)
            self.assertEqual(parser, expected_parser)
            self.assertEqual(parser_test, expected_parser_test)
            self.assertEqual(console_output, expected_console_output)