示例#1
0
 def setup(self):
     self.runner = CliRunner()
     self.conn = Connection('postgresql://localhost/tormordb')
     self.conn.execute("DROP TABLE IF EXISTS migration;")
     self.conn.execute("DROP TABLE IF EXISTS module;")
     self.conn.execute("DROP TABLE IF EXISTS customer;")
     self.conn.execute("DROP TABLE IF EXISTS employee;")
     self.conn.execute("DROP TABLE IF EXISTS product;")
示例#2
0
def script(ctx, h, d, u, p, password):
    cnx_destination = {
        '-h': h,
        '-d': d,
        '-U': u,
        '-p': p,
        '-P': password
    }
    dsn = makeDSN(cnx_destination)
    ctx.obj = {'cnx': Connection(dsn)}
示例#3
0
 def setup(self):
     self.conn = Connection('postgresql://localhost/tormordb')
     self.conn.execute("DROP TABLE IF EXISTS module")
     self.conn.execute(SQL_TO_CREATE_INSERT_MANY_MODULES)
示例#4
0
class TestConnection:
    def setup(self):
        self.conn = Connection('postgresql://localhost/tormordb')
        self.conn.execute("DROP TABLE IF EXISTS module")
        self.conn.execute(SQL_TO_CREATE_INSERT_MANY_MODULES)

    def teardown(self):
        self.conn.execute("DROP TABLE module")
        self.conn.close()

    def test_fetch(self):
        result = self.conn.fetch("SELECT name FROM module")
        expected_result = {"Module1", "Module2", "Module3"}
        actual_result = set(record.get("name") for record in result)
        assert actual_result == expected_result

    def test_load_module(self):
        actual_result = self.conn.load_modules()
        expected_result = {'Module1', 'Module2', 'Module3'}
        assert actual_result == expected_result

    def test_assert_module_exist(self):
        self.conn.assert_module("Module1")

    def test_assert_module_not_exist(self):
        with pytest.raises(ModuleNotFoundError):
            self.conn.assert_module("none")

    def test_transaction_rollback(self):
        with pytest.raises(UndefinedTableError):
            result = self.conn.fetch("SELECT name FROM module_none")
示例#5
0
def test_connection_not_working():
    with pytest.raises(InvalidCatalogNameError):
        conn = Connection('postgresql://localhost/tormordb_none')
        conn.close()
示例#6
0
def test_connection_working():
    conn = Connection('postgresql://localhost/tormordb')
    conn.close()
示例#7
0
class TestScript():
    def setup(self):
        self.runner = CliRunner()
        self.conn = Connection('postgresql://localhost/tormordb')
        self.conn.execute("DROP TABLE IF EXISTS migration;")
        self.conn.execute("DROP TABLE IF EXISTS module;")
        self.conn.execute("DROP TABLE IF EXISTS customer;")
        self.conn.execute("DROP TABLE IF EXISTS employee;")
        self.conn.execute("DROP TABLE IF EXISTS product;")

    def teardown(self):
        self.conn.execute("DROP TABLE IF EXISTS migration;")
        self.conn.execute("DROP TABLE IF EXISTS module;")
        self.conn.execute("DROP TABLE IF EXISTS customer;")
        self.conn.execute("DROP TABLE IF EXISTS employee;")
        self.conn.execute("DROP TABLE IF EXISTS product;")
        self.conn.close()

    def test_script_to_invalid_command(self):
        result = self.runner.invoke(script, ['--xyz'])
        assert result.exit_code == click.UsageError.exit_code

    def test_script_to_migrate(self):
        self.runner.invoke(
            script,
            ['-h', 'localhost', '-d', 'tormordb', 'migrate', 'customer'])
        table_exists = self.conn.fetch("""
            SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = 'customer');
        """)
        assert table_exists[0][0] == True
        customers = self.conn.fetch("""
            SELECT * FROM public.customer;
        """)
        actual_result = set(record.get("name") for record in customers)
        expected_result = {"Customer1", "Customer2", "Customer3", "Customer5"}
        assert actual_result == expected_result

    def test_script_to_migrate_multiple_module(self):
        self.runner.invoke(script, [
            '-h', 'localhost', '-d', 'tormordb', 'migrate', 'employee',
            'customer'
        ])
        customer_table_exists = self.conn.fetch("""
            SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = 'customer');
        """)
        assert customer_table_exists[0][0] == True
        enployee_table_exists = self.conn.fetch("""
            SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = 'employee');
        """)
        assert enployee_table_exists[0][0] == True

        customers = self.conn.fetch("""
            SELECT * FROM public.customer;
        """)
        actual_result = set(record.get("name") for record in customers)
        expected_result = {"Customer1", "Customer2", "Customer3", "Customer5"}
        assert actual_result == expected_result

        employees = self.conn.fetch("""
            SELECT * FROM public.employee;
        """)
        actual_result = set(record.get("name") for record in employees)
        expected_result = {"Employee1", "Employee2", "Employee3"}
        assert actual_result == expected_result

        migration = self.conn.fetch("""
            SELECT * FROM public.migration;
        """)
        assert migration[0][0] == 'customer'
        assert migration[0][1] == '01_customer.sql'

        assert migration[1][0] == 'employee'
        assert migration[1][1] == '01_employee.sql'

        assert migration[2][0] == 'customer'
        assert migration[2][1] == '03_customer.sql'

    def test_script_to_dry_migrate(self):
        self.conn.execute(BOOTSTRAP_SQL)
        self.conn.execute('''INSERT INTO module(name) VALUES ('customer')''')
        self.conn.execute('''INSERT INTO module(name) VALUES ('employee')''')
        self.conn.execute('''INSERT INTO module(name) VALUES ('product')''')
        self.conn.execute('''INSERT INTO module(name) VALUES ('department')''')
        result = self.runner.invoke(script, [
            '-h', 'localhost', '-d', 'tormordb', 'migrate', 'test_module',
            '--dry-run'
        ])
        assert result.exit_code == 0
        assert self.conn.fetch('''SELECT * FROM migration''') == []

    def test_script_to_include(self):
        self.runner.invoke(script, [
            '-h', 'localhost', '-d', 'tormordb', 'include',
            'tormor/tests/script_file.txt'
        ])
        result = self.conn.fetch(
            "SELECT name FROM module GROUP BY name ORDER BY name")
        actual_result = [x['name'] for x in result]
        assert ["customer", "employee", "product"] == actual_result

    def test_script_to_include_with_dry_migrate(self):
        self.runner.invoke(script, [
            '-h', 'localhost', '-d', 'tormordb', 'include', '--dry-run',
            'tormor/tests/script_file_dry.txt'
        ])
        result = self.conn.fetch(
            "SELECT name FROM module GROUP BY name ORDER BY name")
        actual_result = [x['name'] for x in result]
        assert [] == actual_result

    def test_script_to_include_without_file(self):
        result = self.runner.invoke(
            script, ['-h', 'localhost', '-d', 'tormordb', 'include'])
        assert result.exit_code == click.UsageError.exit_code

    def test_script_to_execute_sql(self):
        self.runner.invoke(script, [
            '-h', 'localhost', '-d', 'tormordb', 'sql',
            'tormor/tests/Schema/customer/01_customer.sql'
        ])
        result = self.conn.fetch("SELECT * FROM customer")
        actual_result = set(record.get("name") for record in result)
        expected_result = {"Customer1", "Customer2", "Customer3"}
        assert actual_result == expected_result

    def test_script_to_execute_sql_no_file(self):
        result = self.runner.invoke(
            script, ['-h', 'localhost', '-d', 'tormordb', 'sql'])
        assert result.exit_code == click.UsageError.exit_code