|
| 1 | +import json |
1 | 2 | import uuid |
2 | 3 |
|
3 | 4 | import mock |
4 | 5 | from django.core.exceptions import ValidationError |
5 | 6 | from django.core.serializers.json import DjangoJSONEncoder |
| 7 | +from django.db import connection |
6 | 8 | from django.db.utils import IntegrityError |
7 | 9 | from django.test import SimpleTestCase |
8 | 10 | from django.test import TestCase |
| 11 | +from django.test.utils import CaptureQueriesContext |
9 | 12 | from facility_profile.models import ConditionalLog |
10 | 13 | from facility_profile.models import Facility |
| 14 | +from facility_profile.models import MyUser |
11 | 15 | from facility_profile.models import SummaryLog |
12 | 16 |
|
13 | 17 | from morango.errors import MorangoDirtyParent |
14 | 18 | from morango.errors import MorangoMissingParent |
15 | 19 | from morango.models.certificates import Filter |
16 | 20 | from morango.models.core import Store |
17 | 21 | from morango.models.core import SyncableModel |
| 22 | +from morango.sync.operations import _deserialize_from_store |
| 23 | +from morango.sync.stream.core import Sink |
18 | 24 | from morango.sync.stream.deserialize import AppModelDeserialize |
19 | 25 | from morango.sync.stream.deserialize import AppModelValidate |
20 | 26 | from morango.sync.stream.deserialize import DeserializeOutcome |
21 | 27 | from morango.sync.stream.deserialize import DeserializeTask |
22 | 28 | from morango.sync.stream.deserialize import StoreModelSource |
| 29 | +from morango.utils import exception_path |
23 | 30 |
|
24 | 31 | from ...helpers import StoreFactory |
25 | 32 |
|
@@ -681,3 +688,244 @@ def test_stream__ignores_other_profiles(self): |
681 | 688 | ) |
682 | 689 |
|
683 | 690 | self.assertEqual(self._stream_ids(), [included.id]) |
| 691 | + |
| 692 | + |
| 693 | +class CollectingSink(Sink[DeserializeTask]): |
| 694 | + """Collects each task's resolved outcome and first error type, keyed by the task's ID""" |
| 695 | + |
| 696 | + def __init__(self): |
| 697 | + self.results = {} |
| 698 | + |
| 699 | + def consume(self, task: DeserializeTask) -> None: |
| 700 | + error_type = type(task.errors[0]) if task.errors else None |
| 701 | + self.results[task.id] = (task.outcome, error_type) |
| 702 | + |
| 703 | + |
| 704 | +class DeserializeFixturesMixin: |
| 705 | + """Builds store records for the `facilitydata` profile and runs them through the transforms""" |
| 706 | + |
| 707 | + profile = "facilitydata" |
| 708 | + |
| 709 | + def _store(self, app_model, dirty_bit=True, **kwargs): |
| 710 | + store_kwargs = dict( |
| 711 | + id=app_model.id, |
| 712 | + profile=self.profile, |
| 713 | + model_name=app_model.morango_model_name, |
| 714 | + partition=app_model.id, |
| 715 | + source_id=uuid.uuid4().hex, |
| 716 | + serialized=DjangoJSONEncoder().encode(app_model.serialize()), |
| 717 | + last_saved_instance=uuid.uuid4().hex, |
| 718 | + last_saved_counter=1, |
| 719 | + dirty_bit=dirty_bit, |
| 720 | + ) |
| 721 | + store_kwargs.update(kwargs) |
| 722 | + return StoreFactory(**store_kwargs) |
| 723 | + |
| 724 | + def _facility(self, parent=None, order=None, **kwargs): |
| 725 | + facility = Facility( |
| 726 | + id=uuid.uuid4().hex, |
| 727 | + name="facility", |
| 728 | + parent_id=parent.id if parent else None, |
| 729 | + ) |
| 730 | + if order is None and parent is None: |
| 731 | + order = 0 |
| 732 | + return self._store( |
| 733 | + facility, _self_ref_fk=facility.parent_id or "", _self_ref_order=order, **kwargs |
| 734 | + ) |
| 735 | + |
| 736 | + def _user(self, **kwargs): |
| 737 | + user = MyUser(id=uuid.uuid4().hex, username=uuid.uuid4().hex[:20], password="password") |
| 738 | + return self._store(user, **kwargs) |
| 739 | + |
| 740 | + def _summary_log(self, user_id, **kwargs): |
| 741 | + return self._store(SummaryLog(id=uuid.uuid4().hex, user_id=user_id), **kwargs) |
| 742 | + |
| 743 | + def _run(self, sync_filter=None): |
| 744 | + sink = CollectingSink() |
| 745 | + ( |
| 746 | + StoreModelSource(self.profile, sync_filter=sync_filter) |
| 747 | + .pipe(AppModelDeserialize(sync_filter=sync_filter)) |
| 748 | + .pipe(AppModelValidate(sync_filter=sync_filter)) |
| 749 | + .end(sink) |
| 750 | + ) |
| 751 | + return sink.results |
| 752 | + |
| 753 | + def _run_both_modes(self): |
| 754 | + """Runs once for each `Facility.clean_dereferences_parent` mode, returning both results""" |
| 755 | + results = {} |
| 756 | + for dereferences_parent in (False, True): |
| 757 | + with mock.patch.object(Facility, "clean_dereferences_parent", dereferences_parent): |
| 758 | + results[dereferences_parent] = self._run() |
| 759 | + return results |
| 760 | + |
| 761 | + |
| 762 | +class DeserializeTransformsIntegrationTestCase(DeserializeFixturesMixin, TestCase): |
| 763 | + """ |
| 764 | + Streams real store records through the source and both transforms, without a writing sink, |
| 765 | + so app rows only exist where a test creates them. This mirrors the sink lag, where records |
| 766 | + accepted earlier in the run have not been written yet. |
| 767 | + """ |
| 768 | + |
| 769 | + def test_self_ref_chain_in_run(self): |
| 770 | + root = self._facility() |
| 771 | + child = self._facility(parent=root, order=1) |
| 772 | + grandchild = self._facility(parent=child, order=2) |
| 773 | + |
| 774 | + with mock.patch.object(Facility, "clean_dereferences_parent", False): |
| 775 | + results = self._run() |
| 776 | + |
| 777 | + self.assertEqual(results[root.id], (DeserializeOutcome.SAVE, None)) |
| 778 | + self.assertEqual(results[child.id], (DeserializeOutcome.SAVE, None)) |
| 779 | + self.assertEqual(results[grandchild.id], (DeserializeOutcome.SAVE, None)) |
| 780 | + self.assertEqual(Facility.objects.count(), 0) |
| 781 | + |
| 782 | + def test_self_ref_chain_in_run__dereferences_parent(self): |
| 783 | + """Loading the parent fails, because the sink has not written it yet""" |
| 784 | + root = self._facility() |
| 785 | + child = self._facility(parent=root, order=1) |
| 786 | + grandchild = self._facility(parent=child, order=2) |
| 787 | + |
| 788 | + with mock.patch.object(Facility, "clean_dereferences_parent", True): |
| 789 | + results = self._run() |
| 790 | + |
| 791 | + self.assertEqual(results[root.id], (DeserializeOutcome.SAVE, None)) |
| 792 | + self.assertEqual(results[child.id], (DeserializeOutcome.ERROR, MorangoDirtyParent)) |
| 793 | + self.assertEqual(results[grandchild.id], (DeserializeOutcome.ERROR, MorangoDirtyParent)) |
| 794 | + |
| 795 | + def test_cross_model_chain_in_run(self): |
| 796 | + user = self._user() |
| 797 | + log = self._summary_log(user.id) |
| 798 | + |
| 799 | + results = self._run() |
| 800 | + |
| 801 | + self.assertEqual(results[user.id], (DeserializeOutcome.SAVE, None)) |
| 802 | + self.assertEqual(results[log.id], (DeserializeOutcome.SAVE, None)) |
| 803 | + |
| 804 | + def test_parent_not_in_run(self): |
| 805 | + """The common case, where the parent is unchanged and found through the app tables""" |
| 806 | + parent = Facility.objects.create(name="parent") |
| 807 | + self._store(parent, dirty_bit=False, _self_ref_order=0) |
| 808 | + child = self._facility(parent=parent, order=1) |
| 809 | + |
| 810 | + for mode, results in self._run_both_modes().items(): |
| 811 | + with self.subTest(dereferences_parent=mode): |
| 812 | + self.assertEqual(results[child.id], (DeserializeOutcome.SAVE, None)) |
| 813 | + |
| 814 | + def test_parent_failed_in_run(self): |
| 815 | + root = self._facility(serialized="{bad") |
| 816 | + child = self._facility(parent=root, order=1) |
| 817 | + grandchild = self._facility(parent=child, order=2) |
| 818 | + |
| 819 | + for mode, results in self._run_both_modes().items(): |
| 820 | + with self.subTest(dereferences_parent=mode): |
| 821 | + self.assertEqual(results[root.id], (DeserializeOutcome.ERROR, json.JSONDecodeError)) |
| 822 | + self.assertEqual(results[child.id], (DeserializeOutcome.ERROR, MorangoDirtyParent)) |
| 823 | + self.assertEqual( |
| 824 | + results[grandchild.id], (DeserializeOutcome.ERROR, MorangoDirtyParent) |
| 825 | + ) |
| 826 | + |
| 827 | + def test_parent_missing(self): |
| 828 | + missing_parent = Facility(id=uuid.uuid4().hex, name="missing") |
| 829 | + child = self._facility(parent=missing_parent) |
| 830 | + |
| 831 | + for mode, results in self._run_both_modes().items(): |
| 832 | + with self.subTest(dereferences_parent=mode): |
| 833 | + self.assertEqual( |
| 834 | + results[child.id], (DeserializeOutcome.ERROR, MorangoMissingParent) |
| 835 | + ) |
| 836 | + |
| 837 | + def test_parent_deleted_in_run(self): |
| 838 | + root = self._facility(deleted=True) |
| 839 | + child = self._facility(parent=root, order=1) |
| 840 | + grandchild = self._facility(parent=child, order=2) |
| 841 | + |
| 842 | + for mode, results in self._run_both_modes().items(): |
| 843 | + with self.subTest(dereferences_parent=mode): |
| 844 | + self.assertEqual(results[root.id], (DeserializeOutcome.DELETE, None)) |
| 845 | + self.assertEqual(results[child.id], (DeserializeOutcome.PROPAGATE_DELETE, None)) |
| 846 | + self.assertEqual( |
| 847 | + results[grandchild.id], (DeserializeOutcome.PROPAGATE_DELETE, None) |
| 848 | + ) |
| 849 | + |
| 850 | + def test_parent_deleted_earlier(self): |
| 851 | + root = self._facility(dirty_bit=False, deleted=True) |
| 852 | + child = self._facility(parent=root, order=1) |
| 853 | + |
| 854 | + for mode, results in self._run_both_modes().items(): |
| 855 | + with self.subTest(dereferences_parent=mode): |
| 856 | + self.assertEqual(results[child.id], (DeserializeOutcome.PROPAGATE_DELETE, None)) |
| 857 | + |
| 858 | + def test_parent_hard_deleted_earlier(self): |
| 859 | + root = self._facility(dirty_bit=False, deleted=True, hard_deleted=True) |
| 860 | + child = self._facility(parent=root, order=1) |
| 861 | + |
| 862 | + for mode, results in self._run_both_modes().items(): |
| 863 | + with self.subTest(dereferences_parent=mode): |
| 864 | + self.assertEqual( |
| 865 | + results[child.id], (DeserializeOutcome.PROPAGATE_HARD_DELETE, None) |
| 866 | + ) |
| 867 | + |
| 868 | + def test_target_missing(self): |
| 869 | + log = self._summary_log(uuid.uuid4().hex) |
| 870 | + |
| 871 | + results = self._run() |
| 872 | + |
| 873 | + self.assertEqual(results[log.id], (DeserializeOutcome.ERROR, IntegrityError)) |
| 874 | + |
| 875 | + def test_transforms_only_read(self): |
| 876 | + root = self._facility() |
| 877 | + self._facility(parent=root, order=1) |
| 878 | + self._facility(serialized="{bad") |
| 879 | + self._facility(deleted=True) |
| 880 | + self._summary_log(uuid.uuid4().hex) |
| 881 | + |
| 882 | + with CaptureQueriesContext(connection) as context: |
| 883 | + self._run() |
| 884 | + |
| 885 | + statements = [ |
| 886 | + query["sql"].strip().upper() |
| 887 | + for query in context.captured_queries |
| 888 | + if not query["sql"].strip().upper().startswith(("SAVEPOINT", "RELEASE")) |
| 889 | + ] |
| 890 | + self.assertTrue(statements) |
| 891 | + for statement in statements: |
| 892 | + self.assertTrue(statement.startswith("SELECT"), statement) |
| 893 | + |
| 894 | + |
| 895 | +class LegacyParityTestCase(DeserializeFixturesMixin, TestCase): |
| 896 | + """ |
| 897 | + Characterizes the legacy `_deserialize_from_store` errors against the transforms, on the same |
| 898 | + store records. The transforms don't write, so both can run against the same database state. |
| 899 | + Runs with the default `Facility.clean_dereferences_parent`, which is the legacy test behavior. |
| 900 | + """ |
| 901 | + |
| 902 | + def _assert_parity(self, *store_ids): |
| 903 | + results = self._run() |
| 904 | + _deserialize_from_store(self.profile) |
| 905 | + |
| 906 | + legacy_exceptions = dict( |
| 907 | + Store.objects.filter(id__in=store_ids).values_list("id", "deserialization_exception") |
| 908 | + ) |
| 909 | + for store_id in store_ids: |
| 910 | + with self.subTest(store_id=store_id): |
| 911 | + _, error_type = results[store_id] |
| 912 | + self.assertIsNotNone(error_type) |
| 913 | + self.assertEqual(exception_path(error_type), legacy_exceptions[store_id]) |
| 914 | + |
| 915 | + def test_parent_failed(self): |
| 916 | + root = self._facility(serialized="{bad") |
| 917 | + child = self._facility(parent=root, order=1) |
| 918 | + grandchild = self._facility(parent=child, order=2) |
| 919 | + |
| 920 | + self._assert_parity(root.id, child.id, grandchild.id) |
| 921 | + |
| 922 | + def test_parent_missing(self): |
| 923 | + missing_parent = Facility(id=uuid.uuid4().hex, name="missing") |
| 924 | + child = self._facility(parent=missing_parent) |
| 925 | + |
| 926 | + self._assert_parity(child.id) |
| 927 | + |
| 928 | + def test_target_missing(self): |
| 929 | + log = self._summary_log(uuid.uuid4().hex) |
| 930 | + |
| 931 | + self._assert_parity(log.id) |
0 commit comments