Beispiel #1
0
    def test_deserialize_with_m2m(self):
        """Asserts deserializes model with M2M as long as
        M2M instance exists on destination.
        """
        obj = TestModelWithM2m.objects.using("client").create(f1="model1")
        m2m = M2m.objects.using("client").create(name="erik",
                                                 display_name="Erik")
        obj.m2m.add(m2m)
        m2m = M2m.objects.using("client").create(name="bob",
                                                 display_name="Bob")
        obj.m2m.add(m2m)

        tx_exporter = TransactionExporter(export_path=self.export_path,
                                          using="client")
        batch = tx_exporter.export_batch()
        tx_importer = TransactionImporter(import_path=self.import_path)
        batch = tx_importer.import_batch(filename=batch.filename)

        M2m.objects.create(name="erik", display_name="Erik")
        M2m.objects.create(name="bob", display_name="Bob")

        tx_deserializer = TransactionDeserializer(allow_self=True,
                                                  override_role=NODE_SERVER)
        tx_deserializer.deserialize_transactions(
            transactions=batch.saved_transactions, deserialize_only=False)
        obj = TestModelWithM2m.objects.get(f1="model1")

        obj.m2m.get(name="bob")
Beispiel #2
0
 def test_custom_parser_declared_in_apps_fixes_date(self):
     app_config = django_apps.get_app_config("django_collect_offline")
     app_config.custom_json_parsers = [my_test_parser]
     django_apps.app_configs["django_collect_offline"] = app_config
     tx_deserializer = TransactionDeserializer(allow_self=True,
                                               override_role=NODE_SERVER)
     try:
         tx_deserializer.deserialize_transactions(transactions=[self.obj])
     except DeserializationError:
         self.fail("DeserializationError unexpectedly raised")
Beispiel #3
0
 def test_saved(self):
     """Assert transaction object is saved to default db.
     """
     tx_deserializer = TransactionDeserializer(override_role=NODE_SERVER,
                                               using="default")
     tx_deserializer.deserialize_transactions(
         transactions=self.batch.saved_transactions)
     try:
         TestModel.objects.using("default").get(f1="model1")
     except TestModel.DoesNotExist:
         self.fail("TestModel.DoesNotExist unexpectedly raised")
Beispiel #4
0
 def test_deleted_from_client(self):
     """Asserts "default" instance is deleted when "client" instance
     is deleted.
     """
     test_model = TestModel.objects.using("client").create(f1="model1")
     tx_exporter = TransactionExporter(export_path=self.export_path,
                                       using="client")
     batch = tx_exporter.export_batch()
     tx_importer = TransactionImporter(import_path=self.import_path)
     batch = tx_importer.import_batch(filename=batch.filename)
     tx_deserializer = TransactionDeserializer(override_role=NODE_SERVER,
                                               allow_self=True)
     tx_deserializer.deserialize_transactions(
         transactions=batch.saved_transactions, deserialize_only=True)
     test_model.delete()
     tx_exporter = TransactionExporter(export_path=self.export_path,
                                       using="client")
     batch = tx_exporter.export_batch()
     tx_importer = TransactionImporter(import_path=self.import_path)
     batch = tx_importer.import_batch(filename=batch.filename)
     tx_deserializer = TransactionDeserializer(override_role=NODE_SERVER,
                                               allow_self=True)
     tx_deserializer.deserialize_transactions(
         transactions=batch.saved_transactions)
     try:
         TestModel.objects.get(f1="model1")
     except TestModel.DoesNotExist:
         pass
     else:
         self.fail("TestModel unexpectedly exists")
Beispiel #5
0
 def test_flagged_as_deserialized(self):
     """Asserts "default" instance is created when "client" instance
     is created.
     """
     TestModel.objects.using("client").create(f1="model1")
     tx_exporter = TransactionExporter(export_path=self.export_path,
                                       using="client")
     batch = tx_exporter.export_batch()
     tx_importer = TransactionImporter(import_path=self.import_path)
     batch = tx_importer.import_batch(filename=batch.filename)
     tx_deserializer = TransactionDeserializer(override_role=NODE_SERVER,
                                               allow_self=True)
     tx_deserializer.deserialize_transactions(
         transactions=batch.saved_transactions)
     for transaction in batch.saved_transactions:
         self.assertTrue(transaction.is_consumed)
Beispiel #6
0
 def test_saved_on_self(self):
     """Asserts can save on self if allow_self=True.
     """
     TestModel.objects.create(f1="model1")
     tx_exporter = TransactionExporter(export_path=self.export_path)
     batch = tx_exporter.export_batch()
     tx_importer = TransactionImporter(import_path=self.import_path)
     batch = tx_importer.import_batch(filename=batch.filename)
     tx_deserializer = TransactionDeserializer(override_role=NODE_SERVER,
                                               allow_self=True)
     tx_deserializer.deserialize_transactions(
         transactions=batch.saved_transactions)
     try:
         TestModel.objects.get(f1="model1")
     except TestModel.DoesNotExist:
         self.fail("TestModel unexpectedly does not exist")
Beispiel #7
0
 def test_raises_for_invalid_date(self):
     tx_deserializer = TransactionDeserializer(allow_self=True,
                                               override_role=NODE_SERVER)
     self.assertRaises(
         DeserializationError,
         tx_deserializer.deserialize_transactions,
         transactions=IncomingTransaction.objects.all(),
     )
Beispiel #8
0
 def test_tx_deserializer_is_centralserver(self):
     """Asserts OK if is a server.
     """
     with override_settings(DEVICE_ID=None, DEVICE_ROLE=None):
         app_config = django_apps.get_app_config("edc_device")
         app_config.device_id = "99"
         app_config.device_role = CENTRAL_SERVER
         app_config.ready()
         try:
             TransactionDeserializer()
         except TransactionDeserializerError as e:
             self.fail(f"TransactionDeserializerError unexpectedly raised. Got {e}")
         django_apps.app_configs["edc_device"].device_id = "98"
         try:
             TransactionDeserializer()
         except TransactionDeserializerError as e:
             self.fail(f"TransactionDeserializerError unexpectedly raised. Got {e}")
Beispiel #9
0
 def test_tx_deserializer_override_role(self):
     """Asserts can override role if not a server by device id.
     """
     with override_settings(DEVICE_ID=None, DEVICE_ROLE=None):
         app_config = django_apps.get_app_config("edc_device")
         app_config.device_id = "15"
         app_config.device_role = CLIENT
         app_config.ready()
         try:
             TransactionDeserializer(override_role=NODE_SERVER)
         except TransactionDeserializerError as e:
             self.fail(f"TransactionDeserializerError unexpectedly raised. Got {e}")
Beispiel #10
0
    def test_deserialized_with_history(self):
        """Asserts correctly deserialized model with history.
        """
        TestModel.objects.using("client").create(f1="model1")
        self.assertRaises(ObjectDoesNotExist,
                          TestModel.history.get,
                          f1="model1")

        tx_exporter = TransactionExporter(export_path=self.export_path,
                                          using="client")
        batch = tx_exporter.export_batch()
        tx_importer = TransactionImporter(import_path=self.import_path)
        batch = tx_importer.import_batch(filename=batch.filename)
        tx_deserializer = TransactionDeserializer(allow_self=True,
                                                  override_role=NODE_SERVER)
        tx_deserializer.deserialize_transactions(
            transactions=batch.saved_transactions)

        try:
            TestModel.history.get(f1="model1")
        except TestModel.DoesNotExist:
            self.fail("TestModel history unexpectedly does not exist")
Beispiel #11
0
    def test_deserialized_with_fk_and_history(self):
        """Asserts correctly deserialized model with FK.
        """
        # create model instance on 'client' DB
        test_model = TestModel.objects.using("client").create(f1="model1")
        test_model = TestModel.objects.using("client").get(f1="model1")
        TestModelWithFkProtected.objects.using("client").create(
            f1="f1", test_model=test_model)

        # export tx
        tx_exporter = TransactionExporter(export_path=self.export_path,
                                          using="client")
        batch = tx_exporter.export_batch()
        tx_importer = TransactionImporter(import_path=self.import_path)
        batch = tx_importer.import_batch(filename=batch.filename)
        tx_deserializer = TransactionDeserializer(allow_self=True,
                                                  override_role=NODE_SERVER)
        tx_deserializer.deserialize_transactions(
            transactions=batch.saved_transactions)

        try:
            TestModelWithFkProtected.objects.get(f1="f1")
        except TestModelWithFkProtected.DoesNotExist:
            self.fail("TestModelWithFkProtected unexpectedly does not exist")

        try:
            TestModelWithFkProtected.history.get(f1="f1")
        except TestModelWithFkProtected.DoesNotExist:
            self.fail("TestModelWithFkProtected unexpectedly does not exist")

        try:
            TestModel.objects.get(f1="model1")
        except TestModel.DoesNotExist:
            self.fail("TestModel unexpectedly does not exist")

        try:
            TestModel.history.get(f1="model1")
        except TestModel.DoesNotExist:
            self.fail("TestModel unexpectedly does not exist")
Beispiel #12
0
 def test_deserialize_with_m2m_missing(self):
     """Asserts deserialization error if m2m instance does not
     exist on destination.
     """
     obj = TestModelWithM2m.objects.using("client").create(f1="model1")
     m2m = M2m.objects.using("client").create(name="erik")
     obj.m2m.add(m2m)
     tx_exporter = TransactionExporter(export_path=self.export_path,
                                       using="client")
     batch = tx_exporter.export_batch()
     tx_importer = TransactionImporter(import_path=self.import_path)
     batch = tx_importer.import_batch(filename=batch.filename)
     tx_deserializer = TransactionDeserializer(allow_self=True,
                                               override_role=NODE_SERVER)
     self.assertRaises(
         DeserializationError,
         tx_deserializer.deserialize_transactions,
         transactions=batch.saved_transactions,
     )
Beispiel #13
0
 def test_deserilized_object(self):
     tx_deserializer = TransactionDeserializer(override_role=NODE_SERVER)
     tx_deserializer.deserialize_transactions(
         transactions=self.batch.saved_transactions)