Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Run seed scripts in DbContainer #542

Closed
wants to merge 1 commit into from

Conversation

OverkillGuy
Copy link
Contributor

@OverkillGuy OverkillGuy commented Apr 16, 2024

New capability of "seeding" a db container, by running a database-specific command or twelve to inject data (DB schema or sample data).

Ref #541, implemented for MySQL but generalizeable

return self

def _configure(self) -> None:
raise NotImplementedError

def _seed(self) -> None:
raise NotImplementedError
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized we of course shouldn't penalize DbContainer variants for not defining this method, should do something like if not self.seeds: return and only otherwise raise this error

@@ -30,6 +30,24 @@ def test_docker_run_mysql_8():
assert row[0].startswith("8")


@pytest.mark.skipif(is_arm(), reason="mysql container not available for ARM")
def test_docker_run_mysql_8_seed():
seeds = ("modules/mysql/tests/", ["schema.sql", "seeds.sql"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These paths are hardcoded, assuming pytest executes with cwd at the top of the repo, and will fail if we cd beforehand.

Leaving it up to maintainers to agree if that's good enough, of if a more obscure __file__ related path derivation should be explored instead: is the overhead of more complex test script worth it?

New capability of "seeding" a db container, by running a
database-specific command or twelve to inject data (DB schema or sample
data).

>>> import sqlalchemy
>>> from testcontainers.mysql import MySqlContainer
>>> seed_data = ("../../tests/", ["schema.sql", "data.sql"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tradeoff: the docstring is ugly due to weird relative path to real sql files, but if I write what I mean:

Suggested change
>>> seed_data = ("../../tests/", ["schema.sql", "data.sql"])
>>> seed_data = ("db/scripts/", ["schema.sql", "data.sql"])

Well then the doctests fail, which won't work either!

@OverkillGuy OverkillGuy marked this pull request as ready for review April 16, 2024 22:47
@OverkillGuy OverkillGuy changed the title Add DbContainer._seed() func, used in MySQL feat: Run seed scripts in DbContainer Apr 16, 2024
@alexanderankin
Copy link
Collaborator

see discussion in #541

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants