|
| 1 | +"""Round-trip tests for the `references` fixture. |
| 2 | +
|
| 3 | +Verifies that the `document-reference` primitive emitted by the Python |
| 4 | +generator (`firestore.DocumentReference` / |
| 5 | +`typing.List[firestore.DocumentReference]` / |
| 6 | +`typing.Dict[str, firestore.DocumentReference]`) round-trips correctly |
| 7 | +through the Firestore emulator using the official |
| 8 | +`google-cloud-firestore` client (which is what `firebase-admin` uses |
| 9 | +underneath). |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import json |
| 15 | +import uuid |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | +from google.cloud import firestore |
| 20 | + |
| 21 | + |
| 22 | +def _load_sample(fixtures_root: Path, name: str) -> dict: |
| 23 | + return json.loads((fixtures_root / "samples" / "references" / f"{name}.json").read_text()) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def references_module(import_generated_module): |
| 28 | + return import_generated_module("references") |
| 29 | + |
| 30 | + |
| 31 | +def test_note_link_round_trips_document_references_via_firestore_emulator( |
| 32 | + references_module, |
| 33 | + fixtures_root: Path, |
| 34 | + firestore_client: firestore.Client, |
| 35 | + isolated_collection: firestore.CollectionReference, |
| 36 | +) -> None: |
| 37 | + """A document with a top-level reference + list-of-references + |
| 38 | + map-of-references survives a Pydantic-validate -> emulator-write -> |
| 39 | + emulator-read -> Pydantic-validate cycle with each reference's path |
| 40 | + preserved exactly.""" |
| 41 | + |
| 42 | + NoteLink = references_module.NoteLink |
| 43 | + |
| 44 | + sample = _load_sample(fixtures_root, "note-link") |
| 45 | + |
| 46 | + target = firestore_client.document(sample["target_path"]) |
| 47 | + related = [firestore_client.document(p) for p in sample["related_paths"]] |
| 48 | + by_label = {k: firestore_client.document(v) for k, v in sample["by_label_paths"].items()} |
| 49 | + |
| 50 | + # Sanity-check the fixture itself: distinct paths so a buggy SDK that |
| 51 | + # aliased every reference to the same value would still be caught. |
| 52 | + assert target.path == sample["target_path"] |
| 53 | + assert len({r.path for r in related}) == len(related) |
| 54 | + assert len(related) == 3 |
| 55 | + assert len(by_label) == 3 |
| 56 | + |
| 57 | + note_link_in = NoteLink.model_validate( |
| 58 | + { |
| 59 | + "label": sample["label"], |
| 60 | + "target": target, |
| 61 | + "related": related, |
| 62 | + "by_label": by_label, |
| 63 | + "created_at": sample["created_at"], |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + # The generator emits Pydantic types that store `DocumentReference` |
| 68 | + # values verbatim; check that nothing has been auto-coerced (e.g. to |
| 69 | + # a string path) before we even reach Firestore. |
| 70 | + assert isinstance(note_link_in.target, firestore.DocumentReference) |
| 71 | + assert all(isinstance(r, firestore.DocumentReference) for r in note_link_in.related) |
| 72 | + assert all(isinstance(v, firestore.DocumentReference) for v in note_link_in.by_label.values()) |
| 73 | + |
| 74 | + doc_ref = isolated_collection.document(uuid.uuid4().hex) |
| 75 | + doc_ref.set(note_link_in.model_dump()) |
| 76 | + |
| 77 | + snapshot = doc_ref.get() |
| 78 | + assert snapshot.exists, "expected the written document to be readable" |
| 79 | + |
| 80 | + raw = snapshot.to_dict() |
| 81 | + # Wire-level expectations: the Firestore Python client returns refs |
| 82 | + # as `DocumentReference` for top-level fields, list entries, and map |
| 83 | + # values. |
| 84 | + assert isinstance(raw["target"], firestore.DocumentReference) |
| 85 | + assert isinstance(raw["related"], list) |
| 86 | + assert all(isinstance(r, firestore.DocumentReference) for r in raw["related"]) |
| 87 | + assert isinstance(raw["by_label"], dict) |
| 88 | + assert all(isinstance(v, firestore.DocumentReference) for v in raw["by_label"].values()) |
| 89 | + |
| 90 | + # Re-validate through the generated Pydantic model to confirm the |
| 91 | + # generated schema round-trips without rejecting plain |
| 92 | + # `DocumentReference` values. |
| 93 | + note_link_out = NoteLink.model_validate(raw) |
| 94 | + assert note_link_out.label == sample["label"] |
| 95 | + assert note_link_out.target.path == target.path |
| 96 | + assert [r.path for r in note_link_out.related] == [r.path for r in related] |
| 97 | + assert {k: v.path for k, v in note_link_out.by_label.items()} == { |
| 98 | + k: v.path for k, v in by_label.items() |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +def test_note_link_rejects_string_target(references_module, firestore_client: firestore.Client) -> None: |
| 103 | + """The generated Pydantic class should reject obvious type mismatches |
| 104 | + on the reference-typed fields (e.g. a bare string path where a |
| 105 | + `DocumentReference` instance is expected).""" |
| 106 | + |
| 107 | + NoteLink = references_module.NoteLink |
| 108 | + |
| 109 | + with pytest.raises(Exception): |
| 110 | + NoteLink.model_validate( |
| 111 | + { |
| 112 | + "label": "x", |
| 113 | + "target": "targets/canonical", |
| 114 | + "related": [], |
| 115 | + "by_label": {}, |
| 116 | + "created_at": "2024-01-01T00:00:00.000Z", |
| 117 | + } |
| 118 | + ) |
0 commit comments