askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_import_own_export.py · 4.4 KB · 103 lines
| 1 | """object_import accepts what object_import exports. |
| 2 | |
| 3 | Found by simulation: `export` emits computed fields (a rollup like |
| 4 | redemptions_used, a formula like full_name) and `run` refused every row |
| 5 | carrying one ("field 'x' is computed and cannot be imported"); because the |
| 6 | refused parent rows never reached the run's known ids, every row in a |
| 7 | linked collection then failed its relation check too. Computed fields are |
| 8 | now skipped with a note, and `"identity": true` in a collection's config |
| 9 | maps every source column to the same-named field, so an export is its own |
| 10 | valid input. |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | from pathlib import Path |
| 15 | |
| 16 | import object_import |
| 17 | import object_records |
| 18 | |
| 19 | PEOPLE = {"name": "people", "fields": [ |
| 20 | {"name": "id"}, |
| 21 | {"name": "first_name", "type": "text"}, |
| 22 | {"name": "last_name", "type": "text"}, |
| 23 | {"name": "full_name", "type": "computed", "formula": 'first_name + " " + last_name'}, |
| 24 | {"name": "note_count", "type": "computed", |
| 25 | "rollup": {"collection": "notes", "fk_field": "person_id", "op": "count"}}, |
| 26 | ]} |
| 27 | NOTES = {"name": "notes", "fields": [ |
| 28 | {"name": "id"}, |
| 29 | {"name": "person_id", "relation": {"collection": "people", "display_field": "first_name"}}, |
| 30 | {"name": "body", "type": "text"}, |
| 31 | ]} |
| 32 | |
| 33 | |
| 34 | def _install(data_dir: Path): |
| 35 | for schema in (PEOPLE, NOTES): |
| 36 | path = data_dir / "schemas" / f"{schema['name']}.json" |
| 37 | path.parent.mkdir(parents=True, exist_ok=True) |
| 38 | path.write_text(json.dumps(schema)) |
| 39 | |
| 40 | |
| 41 | def _plan(tmp_path, exports): |
| 42 | plan = {"order": ["people", "notes"], "collections": { |
| 43 | name: {"input": str(path), "identity": True} for name, path in exports.items()}} |
| 44 | path = tmp_path / "plan.json" |
| 45 | path.write_text(json.dumps(plan)) |
| 46 | return object_import.load_plan(path) |
| 47 | |
| 48 | |
| 49 | def test_a_collection_imports_its_own_export_and_linked_rows_follow(tmp_path): |
| 50 | source = tmp_path / "source" |
| 51 | _install(source) |
| 52 | object_records.create_collection_record( |
| 53 | "people", {"id": "p1", "first_name": "Ada", "last_name": "Lovelace"}, base_dir=source) |
| 54 | object_records.create_collection_record( |
| 55 | "notes", {"id": "n1", "person_id": "p1", "body": "hello"}, base_dir=source) |
| 56 | exported = {} |
| 57 | for name in ("people", "notes"): |
| 58 | rows = object_import.export_collection(name, base_dir=source) |
| 59 | exported[name] = tmp_path / f"{name}.json" |
| 60 | exported[name].write_text(json.dumps(rows)) |
| 61 | # the export really does carry the computed columns |
| 62 | assert "full_name" in json.loads(exported["people"].read_text())[0] |
| 63 | |
| 64 | target = tmp_path / "target" |
| 65 | _install(target) |
| 66 | reports = object_import.run_import(_plan(tmp_path, exported), base_dir=target, |
| 67 | dry_run=False, actor="importer") |
| 68 | |
| 69 | by_name = {r.collection: r for r in reports} |
| 70 | assert by_name["people"].errors == 0 and by_name["people"].created == 1 |
| 71 | assert by_name["notes"].errors == 0 and by_name["notes"].created == 1 |
| 72 | assert "full_name" in " ".join(by_name["people"].notes) |
| 73 | assert "note_count" in " ".join(by_name["people"].notes) |
| 74 | assert "notes" in by_name["people"].to_dict() |
| 75 | # computed values are the target's own, derived, not the exported ones |
| 76 | row = object_records.get_collection_record("people", "p1", base_dir=target) |
| 77 | assert row["first_name"] == "Ada" |
| 78 | assert row["full_name"] == "Ada Lovelace" |
| 79 | |
| 80 | |
| 81 | def test_an_explicit_mapping_to_a_computed_field_is_skipped_not_fatal(tmp_path): |
| 82 | target = tmp_path / "target" |
| 83 | _install(target) |
| 84 | src = tmp_path / "people.json" |
| 85 | src.write_text(json.dumps([{"Id": "p1", "First": "Ada", "Total": "9"}])) |
| 86 | plan = {"order": ["people"], "collections": {"people": { |
| 87 | "input": str(src), "mapping": {"Id": "id", "First": "first_name", "Total": "note_count"}}}} |
| 88 | (tmp_path / "plan.json").write_text(json.dumps(plan)) |
| 89 | reports = object_import.run_import(object_import.load_plan(tmp_path / "plan.json"), |
| 90 | base_dir=target, dry_run=True) |
| 91 | assert reports[0].errors == 0 |
| 92 | assert any("note_count" in note for note in reports[0].notes) |
| 93 | |
| 94 | |
| 95 | def test_a_column_the_schema_does_not_have_is_still_an_error(tmp_path): |
| 96 | target = tmp_path / "target" |
| 97 | _install(target) |
| 98 | src = tmp_path / "people.json" |
| 99 | src.write_text(json.dumps([{"id": "p1", "first_name": "Ada", "nonsense": "x"}])) |
| 100 | reports = object_import.run_import( |
| 101 | _plan(tmp_path, {"people": src, "notes": src}), base_dir=target, dry_run=True) |
| 102 | assert reports[0].errors == 1 |
| 103 | assert "nonsense" in " ".join(reports[0].rows[0].reasons) |