def test_update_to_foreign_key(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase, k=6))
        url = "postgresql://localhost/" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        test_schema = deepcopy(TEST_SCHEMA)
        dbu.apply_schema_dict(db, schema_dict=test_schema)

        table1 = db.get_table("table1")
        table1_column1 = table1.table.columns['column1']
        fk1_before = table1_column1.foreign_keys

        self.assertEqual(len(fk1_before), 0)

        dbu.update_to_foreign_key(db, "table1", "column1", "table2", "id")

        table1 = db.get_table("table1")
        table1_column1 = table1.table.columns['column1']
        fk1_after = table1_column1.foreign_keys

        self.assertEqual(len(fk1_after), 1)

        drop_database(url)
    def test_create_db_sqlite(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase,
                                           k=6)) + ".db"
        url = "sqlite:///" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        self.assertEqual(db.engine.url.__str__(), url)

        # test that you can create
        db.create_table("test")
    def test_apply_schema_dict_postgresql(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase, k=6))
        url = "postgresql://localhost/" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        test_schema = deepcopy(TEST_SCHEMA)
        dbu.apply_schema_dict(db, schema_dict=test_schema)

        test_db_schema(self, db)

        drop_database(url)
    def test_create_db_postgresql(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase, k=6))
        url = "postgresql://localhost/" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        self.assertEqual(db.engine.url.__str__(), url)

        # test that you can create
        db.create_table("table")

        del db
        drop_database(url)
    def test_apply_schema_dict_sqlite(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase,
                                           k=6)) + ".db"
        url = "sqlite:///" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        test_schema = deepcopy(TEST_SCHEMA)
        dbu.apply_schema_dict(db, schema_dict=test_schema)

        test_db_schema(self, db)

        del db
        os.remove(temp_name)
    def test_apply_schema_dict_sqlite_foreign_key(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase,
                                           k=6)) + ".db"
        url = "sqlite:///" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        test_schema = deepcopy(TEST_SCHEMA)
        test_schema["table1"]["columns"]["column1"]["foreign_key"] = {
            "reference_table": "table2",
            "reference_column": "id"
        }

        self.assertRaises(TypeError, dbu.apply_schema_dict, db, test_schema)

        del db
        os.remove(temp_name)
    def test_open_database(self, mock_mk, mock_hdb):
        temp_name = ''.join(random.choices(string.ascii_lowercase,
                                           k=6)) + ".db"
        url = "sqlite:///" + this_directory + "/" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        test_schema = deepcopy(TEST_SCHEMA)
        dbu.apply_schema_dict(db, schema_dict=test_schema)
        db.close()

        db = open_database(url, create_format="metadata")
        self.assertEqual(db.url, url)

        mock_mk.assert_not_called()
        mock_hdb.assert_not_called()

        del db
        os.remove(temp_name)
    def test_apply_schema_dict_postgresql_foreign_key(self):
        temp_name = ''.join(random.choices(string.ascii_lowercase, k=6))
        url = "postgresql://localhost/" + temp_name

        dbu = DatabaseUtil()
        db = dbu.create_db(url)

        test_schema = deepcopy(TEST_SCHEMA)
        test_schema["table1"]["columns"]["column1"]["foreign_key"] = {
            "reference_table": "table2",
            "reference_column": "id"
        }

        dbu.apply_schema_dict(db, schema_dict=test_schema)

        table1 = db.get_table("table1")
        table1_column1 = table1.table.columns['column1']
        fk = table1_column1.foreign_keys

        self.assertEqual(len(fk), 1)

        drop_database(url)