Пример #1
0
    def setUp(self):
        site_offline_models.registry = {}
        site_offline_models.loaded = False
        sync_models = [
            "collect_offline_app.testmodel",
            "collect_offline_app.testmodelwithfkprotected",
            "collect_offline_app.testmodelwithm2m",
            "collect_offline_app.testmodelhook",
        ]
        site_offline_models.register(sync_models)

        self.export_path = os.path.join(tempfile.gettempdir(), "export")
        if not os.path.exists(self.export_path):
            os.mkdir(self.export_path)
        self.import_path = self.export_path
        IncomingTransaction.objects.all().delete()
        OutgoingTransaction.objects.using("client").all().delete()
        TestModel.objects.all().delete()
        TestModel.objects.using("client").all().delete()
        TestModel.history.all().delete()
        TestModel.history.using("client").all().delete()
        TestModel.objects.using("client").create(f1="model1")
        TestModel.objects.using("client").create(f1="model2")

        tx_exporter = TransactionExporter(export_path=self.export_path,
                                          using="client")
        batch = tx_exporter.export_batch()
        tx_importer = TransactionImporter(import_path=self.import_path)
        self.batch = tx_importer.import_batch(filename=batch.filename)
Пример #2
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")
Пример #3
0
    def setUp(self):
        site_offline_models.registry = {}
        site_offline_models.loaded = False
        sync_models = ["collect_offline_app.testmodeldates"]
        site_offline_models.register(sync_models, wrapper_cls=OfflineModel)

        self.export_path = os.path.join(tempfile.gettempdir(), "export")
        if not os.path.exists(self.export_path):
            os.mkdir(self.export_path)
        self.import_path = self.export_path
        IncomingTransaction.objects.all().delete()
        OutgoingTransaction.objects.using("client").all().delete()
        TestModelDates.objects.all().delete()
        TestModelDates.objects.using("client").all().delete()
        self.date = get_utcnow()
        TestModelDates.objects.using("client").create(f2=self.date.date())

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

        datetime_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        self.obj = IncomingTransaction.objects.all()[0]
        bad_date = self.date.strftime(datetime_format)
        json_text = self.obj.aes_decrypt(self.obj.tx)
        json_obj = json.loads(json_text)
        json_obj[0]["fields"]["f2"] = bad_date
        json_text = json.dumps(json_obj)
        self.obj.tx = self.obj.aes_encrypt(json_text)
        self.obj.save()
        self.obj = IncomingTransaction.objects.get(id=self.obj.id)
        self.app_config = copy(
            django_apps.get_app_config("django_collect_offline"))
Пример #4
0
 def test_dont_allow_saved_on_self(self):
     """Asserts cannot save on self by default.
     """
     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)
     tx_importer.import_batch(filename=batch.filename)
     self.assertRaises(TransactionDeserializerError,
                       TransactionDeserializer)
Пример #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)
Пример #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")
Пример #7
0
 def test_created_from_client(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)
     try:
         TestModel.objects.get(f1="model1")
     except TestModel.DoesNotExist:
         self.fail("TestModel unexpectedly does not exists")
Пример #8
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,
     )
Пример #9
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")
Пример #10
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")