|
| 1 | +import json |
| 2 | + |
| 3 | +from loro import ExportMode, LoroDoc, VersionVector |
| 4 | + |
| 5 | + |
| 6 | +def test_pending_updates_survive_a_snapshot_checkpoint(): |
| 7 | + source = LoroDoc() |
| 8 | + source.get_text("text").insert(0, "base") |
| 9 | + base = source.export(ExportMode.Snapshot()) |
| 10 | + before_a = source.oplog_vv |
| 11 | + source.get_text("text").insert(4, "A") |
| 12 | + a = source.export(ExportMode.Updates(before_a)) |
| 13 | + after_a = source.oplog_vv |
| 14 | + source.get_text("text").insert(5, "B") |
| 15 | + b = source.export(ExportMode.Updates(after_a)) |
| 16 | + |
| 17 | + receiver = LoroDoc() |
| 18 | + receiver.import_(base) |
| 19 | + receiver.import_(b) |
| 20 | + assert receiver.get_text("text").to_string() == "base" |
| 21 | + snapshot = receiver.export(ExportMode.Snapshot()) |
| 22 | + pending = receiver.export_pending_updates() |
| 23 | + assert isinstance(pending, bytes) |
| 24 | + assert receiver.export_pending_updates() == pending |
| 25 | + |
| 26 | + restored = LoroDoc() |
| 27 | + restored.import_(snapshot) |
| 28 | + restored.import_(pending) |
| 29 | + assert restored.get_text("text").to_string() == "base" |
| 30 | + restored.import_(a) |
| 31 | + assert restored.get_text("text").to_string() == "baseAB" |
| 32 | + assert restored.export_pending_updates() is None |
| 33 | + |
| 34 | + |
| 35 | +def test_pending_export_does_not_commit_local_edits(): |
| 36 | + doc = LoroDoc() |
| 37 | + doc.get_text("text").insert(0, "local") |
| 38 | + pending_count = doc.get_pending_txn_len() |
| 39 | + assert pending_count > 0 |
| 40 | + assert doc.export_pending_updates() is None |
| 41 | + assert doc.get_pending_txn_len() == pending_count |
| 42 | + |
| 43 | + |
| 44 | +def test_json_export_keeps_the_existing_schema(): |
| 45 | + doc = LoroDoc() |
| 46 | + doc.get_text("text").insert(0, "hello") |
| 47 | + doc.commit() |
| 48 | + updates = doc.export_json_updates(VersionVector(), doc.oplog_vv) |
| 49 | + assert "schema_version" in json.loads(updates) |
| 50 | + restored = LoroDoc() |
| 51 | + restored.import_json_updates(updates) |
| 52 | + assert restored.get_text("text").to_string() == "hello" |
0 commit comments