"""CRUD API: pg_graphql reflects every column of every table, serves the seed rows, and resolves the relationships the product documents in graphql/schema.graphql. """ import pytest from semprini_data_domain.platform import camel, pascal from semprini_data_domain.testing import contains, expectations_for, same_value EXPECT = expectations_for(__file__) TABLES = list(EXPECT["tables"].items()) SAMPLES = [(table, sample) for table, spec in TABLES for sample in spec.get("samples", [])] QUERIES = EXPECT.get("graphql", []) def key_of(dp, table): return next(t.key for t in dp.product.tables if t.name == table) @pytest.mark.parametrize("table,spec", TABLES, ids=[t for t, _ in TABLES]) def test_type_reflects_every_column(dp, table, spec): data = dp.graphql(f'{{ __type(name: "{pascal(table)}") {{ fields {{ name }} }} }}') assert data["__type"], f"type {pascal(table)} not reflected" fields = {f["name"] for f in data["__type"]["fields"]} missing = [camel(c) for c in dp.columns(table) if camel(c) not in fields] assert not missing, f"{pascal(table)} lacks {missing}" @pytest.mark.parametrize("table,sample", SAMPLES, ids=[t for t, _ in SAMPLES]) def test_seed_row_through_graphql(dp, table, sample): key = key_of(dp, table) fields = " ".join(camel(column) for column in sample) data = dp.graphql( f'{{ {camel(table)}Collection(filter: {{{camel(key)}: {{eq: "{sample[key]}"}}}}) ' f"{{ edges {{ node {{ {fields} }} }} }} }}" ) edges = data[f"{camel(table)}Collection"]["edges"] assert len(edges) == 1, f"{table} {sample[key]}: {edges}" node = edges[0]["node"] for column, expected in sample.items(): assert same_value(expected, node[camel(column)]), f"{camel(column)}: {node[camel(column)]!r} != {expected!r}" @pytest.mark.parametrize("case", QUERIES, ids=[q["name"] for q in QUERIES]) def test_relationship(dp, case): data = dp.graphql(case["query"]) assert contains(case["expect"], data), f"{data} does not contain {case['expect']}"