Example #1
0
 def test_update_object_with_invalid_serialized_data(self):
     instance = Advert.objects.first()
     importer = Importer(models=["tests.Advert"])
     advert_serializer = importer.get_model_serializer(
         "tests.serializers.AdvertSerializer")
     record_fields_dict = {
         "SEO Description": "Red is a scientifically proven...",
         "External Link": "https://example.com/",
         "slug": "red-its-new-blue",
         "Rating": "2.5",
     }
     mapped_fields_dict = {
         "SEO Description": "description",
         "External Link": "external_link",
         "slug": "slug",
         "Rating": "rating",
     }
     mapped_fields = importer.convert_mapped_fields(
         record_fields_dict,
         mapped_fields_dict,
     )
     serialized_data = advert_serializer(data=mapped_fields)
     is_valid = serialized_data.is_valid()
     self.assertFalse(is_valid)
     saved = importer.update_object(instance, 'recNewRecordId',
                                    serialized_data)
     self.assertTrue(saved)
Example #2
0
    def test_update_object(self):
        importer = Importer(models=["tests.Advert"])
        advert_serializer = importer.get_model_serializer(
            "tests.serializers.AdvertSerializer")
        record_fields_dict = self.get_valid_record_fields()
        record_fields_dict[
            "SEO Description"] = "Red is a scientifically proven..."
        mapped_fields_dict = {
            "Page Title": "title",
            "SEO Description": "description",
            "External Link": "external_link",
            "Is Active": "is_active",
            "slug": "slug",
        }
        mapped_fields = importer.convert_mapped_fields(
            record_fields_dict,
            self.get_valid_mapped_fields(),
        )
        # Ensure mapped_fields are mapped properly
        self.assertEqual(
            mapped_fields['description'],
            "Red is a scientifically proven...",
        )
        # Check serialized data is valid
        serialized_data = advert_serializer(data=mapped_fields)
        is_valid = serialized_data.is_valid()
        self.assertTrue(is_valid)
        # Get the advert object.
        instance = Advert.objects.first()
        self.assertEqual(instance.airtable_record_id, '')
        # Importer should have zero updates objects.
        self.assertEqual(importer.updated, 0)

        saved = importer.update_object(instance, 'recNewRecordId',
                                       serialized_data)

        self.assertTrue(saved)
        # Check to make sure _skip_signals is set.
        self.assertTrue(instance._skip_signals)
        self.assertEqual(importer.updated, 1)
        self.assertEqual(importer.records_used, ['recNewRecordId'])
        # Re-fetch the Advert instance and check its airtable_record_id
        instance = Advert.objects.first()
        self.assertEqual(instance.airtable_record_id, 'recNewRecordId')