Example #1
0
    def test_get_data_for_new_model_with_invalid_serialized_data(self):
        importer = Importer(models=["tests.Advert"])
        advert_serializer = importer.get_model_serializer(
            "tests.serializers.AdvertSerializer")

        mapped_fields = importer.convert_mapped_fields(
            self.get_invalid_record_fields(),
            self.get_invalid_mapped_fields(),
        )
        # Check serialized data is valid
        serialized_data = advert_serializer(data=mapped_fields)
        is_valid = serialized_data.is_valid()
        self.assertFalse(is_valid)

        data_for_new_model = importer.get_data_for_new_model(
            serialized_data, mapped_fields, 'recSomeRecordId')
        self.assertTrue(data_for_new_model.get('airtable_record_id'))
        self.assertEqual(data_for_new_model['airtable_record_id'],
                         'recSomeRecordId')
        self.assertIsNone(data_for_new_model.get('id'))
        self.assertIsNone(data_for_new_model.get('pk'))

        new_dict = mapped_fields.copy()
        new_dict['airtable_record_id'] = 'recSomeRecordId'
        self.assertDictEqual(data_for_new_model,
                             {'airtable_record_id': 'recSomeRecordId'})
Example #2
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 #3
0
 def test_get_incorrect_model_serializer(self):
     importer = Importer(models=["tests.Advert"])
     with self.assertRaises(AttributeError) as context:
         advert_serializer = importer.get_model_serializer(
             "tests.serializers.MissingSerializer")
     self.assertEqual(
         "module 'tests.serializers' has no attribute 'MissingSerializer'",
         str(context.exception))
Example #4
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')
Example #5
0
    def test_update_object_by_uniq_col_name_object_found(self):
        importer = Importer(models=["tests.Advert"])
        advert_serializer = importer.get_model_serializer(
            "tests.serializers.AdvertSerializer")
        record_fields_dict = {
            "Page Title": "Red! It's the new blue!",
            "SEO Description": "Red is a scientifically proven...",
            "External Link": "https://example.com/UPDATED",
            "Is Active": True,
            "rating": "1.5",
            "long_description":
            "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veniam laboriosam consequatur saepe. Repellat itaque dolores neque, impedit reprehenderit eum culpa voluptates harum sapiente nesciunt ratione.</p>",
            "points": 95,
            "slug": "red-its-new-blue",
        }
        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,
            mapped_fields_dict,
        )
        # Check serialized data is valid
        serialized_data = advert_serializer(data=mapped_fields)
        self.assertTrue(serialized_data.is_valid())

        # with self.assertRaises(AttributeError)
        updated = importer.update_object_by_uniq_col_name(
            field_mapping={'slug': 'red-its-new-blue'},
            model=Advert,
            serialized_data=serialized_data,
            record_id='recNewRecordId',
        )
        self.assertTrue(updated)
        self.assertEqual(importer.skipped, 0)
        self.assertEqual(importer.updated, 1)

        advert = Advert.objects.get(slug="red-its-new-blue")
        self.assertEqual(advert.external_link, "https://example.com/UPDATED")
Example #6
0
 def test_get_model_serializer(self):
     importer = Importer(models=["tests.Advert"])
     advert_serializer = importer.get_model_serializer(
         "tests.serializers.AdvertSerializer")
     self.assertEqual(advert_serializer, AdvertSerializer)