def test__get_export(self): url = "http://remote:8001/api/export/" result = requests.get(url, auth=('export', 'password')) self.assertEqual(result.status_code, 200) import_data = result.json() import_task = ImportTask(import_data, self.affiliated_store) result = import_task.run() self.assertResultsCount(result, "errors", 0) self.assertResultsCount(result, "agencies", 1) self.assertResultsCount(result, "categories", 1) self.assertResultsCount(result, "contact_types", 1) self.assertResultsCount(result, "contacts", 1) self.assertResultsCount(result, "custom_field_types", 1) self.assertResultsCount(result, "custom_fields", 1) self.assertResultsCount(result, "image_types", 8) self.assertResultsCount(result, "images", 9) self.assertResultsCount(result, "intents", 2) self.assertResultsCount(result, "listing_types", 1) self.assertResultsCount(result, "profiles", 1) self.assertResultsCount(result, "tags", 2) self.assertResultsCount(result, "listings", 1)
def run_import_task(import_task_id): """ Run import task """ try: task = models.ImportTask.objects.find_by_id(import_task_id) import_data = get_import_data(task) result = ImportTaskService(import_data.json(), task.affiliated_store).run() errors = result['errors'] empty_result = all(value == [] for value in result.values()) if empty_result: models.ImportTaskResult.objects.create_result(import_task_id, 'Pass', 'Empty Result') if len(errors) == 0: models.ImportTaskResult.objects.create_result(import_task_id, 'Pass', 'Import Successful') else: models.ImportTaskResult.objects.create_result(import_task_id, 'Fail', "Errors Found: %s" % ( errors)) except Exception as e: models.ImportTaskResult.objects.create_result(import_task_id, 'Fail', "Exception(Import Task): Task_ID: %s Exception: %s" % (import_task_id, e)) logger.info("Exception(Import Task): Task_ID: %s Exception: %s" % (import_task_id, e)) print("Exception: %s" % e)
def test__import_task__update_image_type(self): updated_date = timezone.now() initial_date = updated_date - timedelta(days=1) initial_data = {"image_types": [IMAGE_TYPE_1]} ImportTask(initial_data, self.affiliated_store, initial_date).run() updated_data = {"image_types": [IMAGE_TYPE_1_UPDATE]} result = ImportTask(updated_data, self.affiliated_store, updated_date).run() image_type = result["image_types"][0] self.assertImageTypeImport(image_type, IMAGE_TYPE_1_UPDATE) self.assertEqual(image_type.import_metadata.last_updated, updated_date)
def test__import_task__new_listing_type(self): sample_data = {"listing_types": [LISTING_TYPE_1]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertListingTypeImport(result["listing_types"][0], LISTING_TYPE_1)
def test__import_task__new_custom_field_type(self): sample_data = {"custom_field_types": [CUSTOM_FIELD_TYPE_1]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertCustomFieldTypeImport(result["custom_field_types"][0], CUSTOM_FIELD_TYPE_1)
def test__import_task__new_contact_type(self): sample_data = {"contact_types": [CONTACT_TYPE_1]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertContactTypeImport(result["contact_types"][0], CONTACT_TYPE_1)
def test__import_task__new_image_type__invalid(self): sample_data = {"image_types": [IMAGE_TYPE_1_INVALID]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertEqual(len(result["image_types"]), 0) self.assertEqual(len(result["errors"]), 1)
def test__import_task__new_listing(self): import_task = ImportTask(self.import_data, self.affiliated_store) result = import_task.run() self.assertResultsCount(result, "errors", 0) expected = find_by_internal_id(self.import_data["listings"], 2) actual = find_by_external_id(result["listings"], 2) self.assertPropertyEqualValue(actual, "approval_status", "APPROVED") self.assertDateTimePropertyEqual(actual, expected, "approved_date") self.assertDateTimePropertyEqual(actual, expected, "edited_date") self.assertDateTimePropertyEqual(actual, expected, "featured_date") self.assertPropertyEqualValue(actual, "avg_rate", 3.2) self.assertReferenceHasImportMetadata(actual, expected, "agency") self.assertReferenceHasImportMetadata(actual, expected, "banner_icon") self.assertReferenceHasImportMetadata(actual, expected, "large_banner_icon") self.assertReferenceHasImportMetadata(actual, expected, "large_icon") self.assertReferenceHasImportMetadata(actual, expected, "small_icon") self.assertReferenceHasImportMetadata(actual, expected, "last_activity") self.assertReferenceHasImportMetadata(actual, expected, "required_listings") self.assertManyReferenceHaveImportMetadata(actual, expected, "categories") self.assertManyReferenceHaveImportMetadata(actual, expected, "contacts") self.assertManyReferenceHaveImportMetadata(actual, expected, "intents") self.assertManyReferenceHaveImportMetadata(actual, expected, "owners") self.assertManyReferenceHaveImportMetadata(actual, expected, "tags") self.assertEmbeddedListEqual(actual, expected, "doc_urls", self.assertDocUrlImport) self.assertEmbeddedListEqual(actual, expected, "reviews", self.assertReviewImport) self.assertEmbeddedListEqual(actual, expected, "screenshots", self.assertScreenshotImport) self.assertEmbeddedListEqual(actual, expected, "custom_fields", self.assertCustomFieldValueImport)
def test__import_task__new_agency(self): sample_data = { "image_types": [IMAGE_TYPE_1], "images": [IMAGE_1], "agencies": [AGENCY_1] } result = ImportTask(sample_data, self.affiliated_store).run() self.assertAgencyImport(result["agencies"][0], AGENCY_1)
def test__import_task__existing_image_type(self): ImageTypeFactory(name="agency_icon") sample_data = {"image_types": [IMAGE_TYPE_1]} result = ImportTask(sample_data, self.affiliated_store).run() image_type = result["image_types"][0] self.assertImageTypeImport(image_type, IMAGE_TYPE_1) existing = ImageType.objects.internal_only().filter( name="agency_icon").first() self.assertNotEqual(existing.id, image_type.id) total = ImageType.objects.count() self.assertEqual(total, 2)
def test__import_task(self): with open('tests/ozpcenter_api/imports/single_listing_export.json' ) as import_data_json: preload_data = json.load(import_data_json) preload_task = ImportTask(preload_data, self.affiliated_store) result = preload_task.run() self.assertResultsCount(result, "errors", 0) self.assertResultsCount(result, "agencies", 1) self.assertResultsCount(result, "categories", 1) self.assertResultsCount(result, "contact_types", 1) self.assertResultsCount(result, "contacts", 1) self.assertResultsCount(result, "custom_field_types", 1) self.assertResultsCount(result, "custom_fields", 1) self.assertResultsCount(result, "image_types", 1) self.assertResultsCount(result, "images", 1) self.assertResultsCount(result, "intents", 1) self.assertResultsCount(result, "listing_types", 1) self.assertResultsCount(result, "profiles", 1) self.assertResultsCount(result, "tags", 1) self.assertResultsCount(result, "listings", 1) import_task = ImportTask(self.import_data, self.affiliated_store) result = import_task.run() self.assertResultsCount(result, "errors", 0) self.assertResultsCount(result, "agencies", 9) self.assertResultsCount(result, "categories", 15) self.assertResultsCount(result, "contact_types", 3) self.assertResultsCount(result, "contacts", 29) self.assertResultsCount(result, "custom_field_types", 2) self.assertResultsCount(result, "custom_fields", 2) self.assertResultsCount(result, "image_types", 8) self.assertResultsCount(result, "images", 1225) self.assertResultsCount(result, "intents", 2) self.assertResultsCount(result, "listing_types", 5) self.assertResultsCount(result, "profiles", 16) self.assertResultsCount(result, "tags", 371) self.assertResultsCount(result, "listings", 186) self.assertAffiliatedStore(result)
def test__import_task__new_category(self): sample_data = {"categories": [CATEGORY_1]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertCategoryImport(result["categories"][0], CATEGORY_1)
def test__import_task__new_tag(self): sample_data = {"tags": [TAG_1]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertTagImport(result["tags"][0], TAG_1)
def test__import_task__new_image(self): sample_data = {"image_types": [IMAGE_TYPE_1], "images": [IMAGE_1]} result = ImportTask(sample_data, self.affiliated_store).run() self.assertImageImport(result["images"][0], IMAGE_1, IMAGE_TYPE_1)