Skip to content

Commit

Permalink
Configure cli with envvars
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Jul 13, 2023
1 parent b5a4620 commit 4c31dca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/aaq_sync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,30 @@ def convert(self, value, param, ctx):
return MODEL_MAPPING[super().convert(value, param, ctx)]


@click.command()
@click.command(context_settings={"auto_envvar_prefix": "AAQ_SYNC"})
@DbURLParam.option("--db-url", help="Database URL.")
@HttpURLParam.option("--export-url", help="Data export API URL.")
@click.option("--export-token", type=str, required=True, help="Export API auth token.")
@TableChoiceParam.option(
"models", "--table", multiple=True, help="Table to sync. (Multiple allowed.)"
"tables", "--table", multiple=True, help="Table to sync. (Multiple allowed.)"
)
def aaq_sync(
db_url: DbURL,
export_url: HttpURL,
export_token: str,
models: list[type[Base]],
tables: list[type[Base]],
):
"""
Sync one or more AAQ tables from the given data export API endpoint to the
given database.
"""
print(db_url, export_url, models)
print(db_url, export_url, tables)
dbengine = create_engine(db_url, echo=False)
with (
Session(dbengine) as session,
ExportClient(export_url, export_token) as exporter,
):
for model in models:
click.echo(f"Syncing {model.__tablename__} ...")
synced = sync_model_items(model, exporter, session)
click.echo(f"Synced {len(synced)} {model.__tablename__} items.")
for table in tables:
click.echo(f"Syncing {table.__tablename__} ...")
synced = sync_model_items(table, exporter, session)
click.echo(f"Synced {len(synced)} {table.__tablename__} items.")
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,27 @@ def test_sync_faqmatches(runner, fake_data_export, db):
]

result = runner.invoke(aaq_sync, opts)
print(result.output)
assert result.exit_code == 0
assert db.fetch_faqs() == [faq1, faq2]


def test_sync_faqmatches_envvars(runner, fake_data_export, db, monkeypatch):
"""
All config options can be provided through envvars.
"""
faqds = json.loads(read_test_data("two_faqs.json"))["result"]
[faq1, faq2] = [FAQModel.from_json(faqd) for faqd in faqds]
fake_data_export.faqmatches.extend(faqds)

assert db.fetch_faqs() == []

monkeypatch.setenv("AAQ_SYNC_DB_URL", str(db.engine.url))
monkeypatch.setenv("AAQ_SYNC_EXPORT_URL", str(fake_data_export.base_url))
monkeypatch.setenv("AAQ_SYNC_EXPORT_TOKEN", "faketoken")
monkeypatch.setenv("AAQ_SYNC_TABLES", "faqmatches")

result = runner.invoke(aaq_sync, [])
print(result.output)
assert result.exit_code == 0
assert db.fetch_faqs() == [faq1, faq2]

0 comments on commit 4c31dca

Please sign in to comment.