コード例 #1
0
    def test___init__(self):

        migrations = relations.Migrations()
        self.assertEqual(migrations.directory, "ddl")

        migrations = relations.Migrations("dll")
        self.assertEqual(migrations.directory, "dll")
コード例 #2
0
    def test_list(self):

        source = relations.unittest.MockSource("MigrationsSource")

        os.makedirs(f"ddl/{source.name}/{source.KIND}")

        pathlib.Path(
            f"ddl/{source.name}/{source.KIND}/definition.json").touch()
        pathlib.Path(
            f"ddl/{source.name}/{source.KIND}/definition-2012-07-07.json"
        ).touch()
        pathlib.Path(
            f"ddl/{source.name}/{source.KIND}/migration-2012-07-07.json"
        ).touch()
        pathlib.Path(
            f"ddl/{source.name}/{source.KIND}/definition-2012-07-08.json"
        ).touch()
        pathlib.Path(
            f"ddl/{source.name}/{source.KIND}/migration-2012-07-08.json"
        ).touch()

        migrations = relations.Migrations()

        self.assertEqual(
            migrations.list("MigrationsSource"), {
                "2012-07-07": {
                    "definition": "definition-2012-07-07.json",
                    "migration": "migration-2012-07-07.json"
                },
                "2012-07-08": {
                    "definition": "definition-2012-07-08.json",
                    "migration": "migration-2012-07-08.json"
                }
            })
コード例 #3
0
    def test_migrate(self):

        migrations = relations.Migrations()

        migrations.generate([Unit])
        migrations.generate([Unit, Test])
        migrations.convert(self.source.name)

        self.assertTrue(
            self.source.migrate(f"ddl/{self.source.name}/{self.source.KIND}"))

        self.assertEqual(Unit.many().count(), 0)
        self.assertEqual(Test.many().count(), 0)

        self.assertFalse(
            self.source.migrate(f"ddl/{self.source.name}/{self.source.KIND}"))

        migrations.generate([Unit, Test, Case])
        migrations.convert(self.source.name)

        self.assertTrue(
            self.source.migrate(f"ddl/{self.source.name}/{self.source.KIND}"))

        self.assertEqual(Case.many().count(), 0)

        self.assertFalse(
            self.source.migrate(f"ddl/{self.source.name}/{self.source.KIND}"))
コード例 #4
0
    def test_apply(self):

        source = relations.unittest.MockSource("MigrationsSource")

        source.ids = {}
        source.data = {}
        source.migrations = []

        # Apply on definition alone

        definition = [{
            "ACTION":
            "add",
            "source":
            "MigrationsSource",
            "name":
            "people",
            "title":
            "People",
            "fields": [{
                "name": "id",
                "kind": "int",
                "store": "id",
                "none": True,
                "auto": True
            }, {
                "name": "name",
                "kind": "str",
                "store": "name",
                "none": False
            }],
            "id":
            "id",
            "unique": {
                "name": ["name"]
            },
            "index": {}
        }]

        os.makedirs("ddl/MigrationsSource/mock", exist_ok=True)

        with open("ddl/MigrationsSource/mock/definition.json",
                  'w') as ddl_file:
            json.dump(definition, ddl_file)

        with open(
                "ddl/MigrationsSource/mock/migration-2021-07-07-11-12-13-000000.json",
                'w') as ddl_file:
            json.dump(definition, ddl_file)

        migrations = relations.Migrations()

        self.assertTrue(migrations.apply("MigrationsSource"))

        self.assertEqual(source.ids, {"people": 0})
        self.assertEqual(source.data, {"people": {}})
        self.assertEqual(source.migrations, ["2021-07-07-11-12-13-000000"])

        self.assertFalse(migrations.apply("MigrationsSource"))
コード例 #5
0
    def test_current(self):

        migrations = relations.Migrations()

        self.assertEqual(migrations.current(), {})

        with open("ddl/definition.json", 'w') as ddl_file:
            json.dump({"a": 1}, ddl_file)

        self.assertEqual(migrations.current(), {"a": 1})
コード例 #6
0
    def test_load(self):

        source = relations.unittest.MockSource("MigrationsSource")

        source.ids = {}
        source.data = {}

        definition = [{
            "ACTION":
            "add",
            "source":
            "MigrationsSource",
            "name":
            "people",
            "title":
            "People",
            "fields": [{
                "name": "id",
                "kind": "int",
                "store": "id",
                "none": True,
                "auto": True
            }, {
                "name": "name",
                "kind": "str",
                "store": "name",
                "none": False
            }],
            "id":
            "id",
            "unique": {
                "name": ["name"]
            },
            "index": {}
        }]

        os.makedirs("ddl/MigrationsSource/mock", exist_ok=True)

        with open("ddl/MigrationsSource/mock/definition.json",
                  'w') as ddl_file:
            json.dump(definition, ddl_file)

        migrations = relations.Migrations()

        migrations.load("MigrationsSource", "definition.json")

        self.assertEqual(source.ids, {"people": 0})
        self.assertEqual(source.data, {"people": {}})
コード例 #7
0
    def test_load(self):

        self.source.ids = {}
        self.source.data = {}

        migrations = relations.Migrations()

        migrations.generate([Unit])
        migrations.convert(self.source.name)

        self.source.load(
            f"ddl/{self.source.name}/{self.source.KIND}/definition.sql")

        cursor = self.source.connection.cursor()

        cursor.execute("SELECT COUNT(*) as `total` FROM `test_source`.`unit`")

        self.assertEqual(cursor.fetchone()["total"], 0)
コード例 #8
0
    def test_define(self):

        migrations = relations.Migrations()

        self.assertEqual(
            migrations.define([People]), {
                "people": {
                    "source":
                    "MigrationsSource",
                    "name":
                    "people",
                    "title":
                    "People",
                    "fields": [{
                        "name": "id",
                        "kind": "int",
                        "store": "id",
                        "none": True,
                        "auto": True
                    }, {
                        "name": "name",
                        "kind": "str",
                        "store": "name",
                        "none": False
                    }, {
                        "name": "gender",
                        "kind": "str",
                        "store": "gender",
                        "options": ["free", "male", "female"],
                        "default": "free",
                        "none": False
                    }],
                    "id":
                    "id",
                    "unique": {
                        "name": ["name"]
                    },
                    "index": {}
                }
            })
コード例 #9
0
    def test_convert(self):

        relations.unittest.MockSource("MigrationsSource")

        with open("ddl/definition.json", 'w') as ddl_file:
            json.dump({"people": People.thy().define()}, ddl_file)

        with open("ddl/migration-1234.json", 'w') as ddl_file:
            json.dump(
                {
                    "change": {
                        "migs": {
                            "definition": {
                                "source":
                                "MigrationsSource",
                                "name":
                                "migs",
                                "fields": [{
                                    "name": "fie",
                                    "store": "fie",
                                    "kind": "int"
                                }, {
                                    "name": "foe",
                                    "store": "foe",
                                    "kind": "int"
                                }]
                            },
                            "migration": {
                                "source": "MigrationsSource",
                                "name": "mig",
                                "fields": {
                                    "add": [{
                                        "name": "fee",
                                        "store": "fee",
                                        "kind": "int"
                                    }],
                                    "remove": ["fie"],
                                    "change": {
                                        "foe": {
                                            "name": "fum",
                                            "kind": "float"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }, ddl_file)

        migrations = relations.Migrations()

        migrations.convert("MigrationsSource")

        with open("ddl/MigrationsSource/mock/definition.json",
                  'r') as ddl_file:
            self.assertEqual(json.load(ddl_file), [{
                "ACTION":
                "add",
                "source":
                "MigrationsSource",
                "name":
                "people",
                "title":
                "People",
                "fields": [{
                    "name": "id",
                    "kind": "int",
                    "store": "id",
                    "none": True,
                    "auto": True
                }, {
                    "name": "name",
                    "kind": "str",
                    "store": "name",
                    "none": False
                }, {
                    "name": "gender",
                    "kind": "str",
                    "store": "gender",
                    "options": ["free", "male", "female"],
                    "default": "free",
                    "none": False
                }],
                "id":
                "id",
                "unique": {
                    "name": ["name"]
                },
                "index": {}
            }])

        with open("ddl/MigrationsSource/mock/migration-1234.json",
                  'r') as ddl_file:
            self.assertEqual(json.load(ddl_file), [{
                "ACTION": "change",
                "DEFINITION": {
                    "source":
                    "MigrationsSource",
                    "name":
                    "migs",
                    "fields": [{
                        "name": "fie",
                        "store": "fie",
                        "kind": "int"
                    }, {
                        "name": "foe",
                        "store": "foe",
                        "kind": "int"
                    }]
                },
                "MIGRATION": {
                    "source":
                    "MigrationsSource",
                    "name":
                    "mig",
                    "fields": [{
                        "ACTION": "add",
                        "name": "fee",
                        "store": "fee",
                        "kind": "int"
                    }, {
                        "ACTION": "remove",
                        "name": "fie",
                        "store": "fie",
                        "kind": "int"
                    }, {
                        "ACTION": "change",
                        "DEFINITION": {
                            "name": "foe",
                            "store": "foe",
                            "kind": "int"
                        },
                        "MIGRATION": {
                            "name": "fum",
                            "kind": "float"
                        }
                    }]
                }
            }])
コード例 #10
0
    def test_generate(self):

        migrations = relations.Migrations()

        self.assertTrue(migrations.generate([People]))

        with open("ddl/definition.json", 'r') as ddl_file:

            current = json.load(ddl_file)

            self.assertEqual(
                current, {
                    "people": {
                        "source":
                        "MigrationsSource",
                        "name":
                        "people",
                        "title":
                        "People",
                        "fields": [{
                            "name": "id",
                            "kind": "int",
                            "store": "id",
                            "none": True,
                            "auto": True
                        }, {
                            "name": "name",
                            "kind": "str",
                            "store": "name",
                            "none": False
                        }, {
                            "name": "gender",
                            "kind": "str",
                            "store": "gender",
                            "options": ["free", "male", "female"],
                            "default": "free",
                            "none": False
                        }],
                        "id":
                        "id",
                        "unique": {
                            "name": ["name"]
                        },
                        "index": {}
                    }
                })

        self.assertFalse(migrations.generate([People]))

        current["people"]["fields"][2]["store"] = "genders"

        with open("ddl/definition.json", 'w') as ddl_file:
            json.dump(current, ddl_file)

        self.assertTrue(migrations.generate([People]))

        with open("ddl/definition-2021-07-08-11-12-13-000000.json",
                  'r') as ddl_file:
            self.assertEqual(
                json.load(ddl_file), {
                    "people": {
                        "source":
                        "MigrationsSource",
                        "name":
                        "people",
                        "title":
                        "People",
                        "fields": [{
                            "name": "id",
                            "kind": "int",
                            "store": "id",
                            "none": True,
                            "auto": True
                        }, {
                            "name": "name",
                            "kind": "str",
                            "store": "name",
                            "none": False
                        }, {
                            "name": "gender",
                            "kind": "str",
                            "store": "genders",
                            "options": ["free", "male", "female"],
                            "default": "free",
                            "none": False
                        }],
                        "id":
                        "id",
                        "unique": {
                            "name": ["name"]
                        },
                        "index": {}
                    }
                })

        with open("ddl/migration-2021-07-08-11-12-13-000000.json",
                  'r') as ddl_file:
            self.assertEqual(
                json.load(ddl_file), {
                    "change": {
                        "people": {
                            "definition": {
                                "source":
                                "MigrationsSource",
                                "name":
                                "people",
                                "title":
                                "People",
                                "fields": [
                                    {
                                        "name": "id",
                                        "kind": "int",
                                        "store": "id",
                                        "none": True,
                                        "auto": True
                                    }, {
                                        "name": "name",
                                        "kind": "str",
                                        "store": "name",
                                        "none": False
                                    }, {
                                        "name": "gender",
                                        "kind": "str",
                                        "store": "genders",
                                        "options": ["free", "male", "female"],
                                        "default": "free",
                                        "none": False
                                    }
                                ],
                                "id":
                                "id",
                                "unique": {
                                    "name": ["name"]
                                },
                                "index": {}
                            },
                            "migration": {
                                "fields": {
                                    "change": {
                                        "gender": {
                                            "store": "gender"
                                        }
                                    }
                                }
                            }
                        }
                    }
                })

        with open("ddl/definition.json", 'r') as ddl_file:
            self.assertEqual(
                json.load(ddl_file), {
                    "people": {
                        "source":
                        "MigrationsSource",
                        "name":
                        "people",
                        "title":
                        "People",
                        "fields": [{
                            "name": "id",
                            "kind": "int",
                            "store": "id",
                            "none": True,
                            "auto": True
                        }, {
                            "name": "name",
                            "kind": "str",
                            "store": "name",
                            "none": False
                        }, {
                            "name": "gender",
                            "kind": "str",
                            "store": "gender",
                            "options": ["free", "male", "female"],
                            "default": "free",
                            "none": False
                        }],
                        "id":
                        "id",
                        "unique": {
                            "name": ["name"]
                        },
                        "index": {}
                    }
                })