class AdGroupsChangeStatusSpecs(spec(AdGroupsChangeStatus), CreateTestData): subject = None ad_group_service = None request = None campaign = None ad_group = None ad_group1 = None def before_each(self): self._create_client() self.campaign = self.create_campaign() self.ad_group = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group', status=STATUS_PAUSED) self.ad_group1 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 1', status=STATUS_PAUSED) self.ad_group.revmap_rev_value = 1.34 self.ad_group.revmap_rev_type = 'click' self.ad_group.revmap_opt_value = 13.2 self.ad_group.revmap_opt_type = 'install' self.ad_group.targeting = '{"country":["USA"], "os":"Android"}' self.ad_group.save() self.ad_group1.revmap_rev_value = 11.24 self.ad_group1.revmap_rev_type = 'click' self.ad_group1.revmap_opt_value = 3.21 self.ad_group1.revmap_opt_type = 'install' self.ad_group1.targeting = '{"country":["USA"], "os":"Android"}' self.ad_group1.save() ad_group_service = Mock() request = Mock() request.query_params = {} request.QUERY_PARAMS = {} self.request = request self.ad_group_service = ad_group_service self.subject = AdGroupsChangeStatus( ad_group_service=lambda: ad_group_service) def test__post__set_status_by_ad_id(self): self.assertEqual(self.ad_group.status, STATUS_PAUSED) self.assertEqual(self.ad_group1.status, STATUS_PAUSED) self.request.DATA = { u'src_ad_group_ids': [self.ad_group.pk, self.ad_group1.pk], u'new_status': u'enabled' } response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertEqual( AdGroup.objects.get(pk=self.ad_group.pk).status, STATUS_ENABLED) self.assertEqual( AdGroup.objects.get(pk=self.ad_group1.pk).status, STATUS_ENABLED)
class BaseModelSerializerSpecs(spec(BaseModelSerializer)): subject = None request = None def before_each(self): self.request = Mock() self.request.QUERY_PARAMS = {} self.subject = BaseModelSerializer() self.subject.context['request'] = self.request self.stub(serializers.ModelSerializer).get_fields().thenReturn({ u'field1': Mock(), u'field2': Mock() }) def test__get_fields__returns_fields_specified_by_the_select_query_string_parameter( self): # act self.request.QUERY_PARAMS['$select'] = u'field1' fields = self.subject.get_fields() # assert assert len(fields.items()) == 1 assert fields.items()[0][0] == u'field1' def test__get_fields__returns_all_fields_unless_select_query_string_parameter_is_provided( self): # act fields = self.subject.get_fields() # insert assert len(fields.items()) == 2
class ListPaginationSpecs(spec(ListPagination)): request = None subject = None def before_each(self): self.request = Mock() self.request.query_params = {} self.subject = ListPagination() self.subject.request = self.request self.subject.page = Mock() self.subject.page.paginator = Mock() self.subject.page.paginator.count = 12 def test__get_paginated_response__returns_wrapped_data_argument(self): # act ret = self.subject.get_paginated_response('result1') # assert assert ret.data['count'] == 1 assert ret.data['total'] == 12 assert ret.data['results'] == 'result1' def test__get_page_size__returns_maxint_when_page_size_value_from_query_string_is_zero( self): # act self.request.query_params = {u'$page_size': u'0'} ret = self.subject.get_page_size(self.request) # assert assert ret == maxint
class AdsChangeStatusSpecs(spec(AdsChangeStatus), CreateTestData): ads_change_status_service = None subject = None request = None ad_group = None ad_group1 = None ad1 = None ad2 = None def before_each(self): self._create_client() self.ad_group = self.create_ad_group() self.ad1 = self.create_ad(campaign_id=self.ad_group.campaign_id, ad_group_id=self.ad_group, ad_name='ad 1') self.ad2 = self.create_ad(campaign_id=self.ad_group.campaign_id, ad_group_id=self.ad_group, ad_name='ad 2') self.ads_change_status_service = Mock() self.subject = AdsChangeStatus(ads_change_status_service=lambda: self.ads_change_status_service) request = Mock() request.query_params = {} request.QUERY_PARAMS = {} self.request = request def test__post__set_status_by_ad_id(self): self.assertEqual(self.ad1.status, STATUS_PAUSED) self.assertEqual(self.ad2.status, STATUS_PAUSED) self.request.DATA = { 'src_ad_group_id': self.ad_group.pk, 'src_ad_ids': [self.ad1.pk, self.ad2.pk], 'src_ad_statuses': [u'paused'], 'new_status': u'enabled' } response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertEqual(Ad.objects.get(pk=self.ad1.pk).status, STATUS_ENABLED) self.assertEqual(Ad.objects.get(pk=self.ad2.pk).status, STATUS_ENABLED) def test__post__set_status_to_all(self): self.assertEqual(self.ad1.status, STATUS_PAUSED) self.assertEqual(self.ad2.status, STATUS_PAUSED) self.request.DATA = { 'src_ad_group_id': self.ad_group.pk, 'src_ad_ids': ['-1'], 'src_ad_statuses': [u'paused'], 'new_status': u'enabled' } response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertEqual(Ad.objects.get(pk=self.ad1.pk).status, STATUS_ENABLED) self.assertEqual(Ad.objects.get(pk=self.ad2.pk).status, STATUS_ENABLED)
class FilterByListMixinSpecs(spec(FilterByListMixin)): subject = None request = None def before_each(self): self.request = Mock() self.request.query_params = {} self.subject = FilterByListMixin() self.subject.queryset = Mock() self.subject.queryset.values = Mock(return_value='value_query_set') self.subject.request = self.request def test__get_queryset__applies_select_query_string_argument_and_returns_value_query_set( self): # pylint: disable=unnecessary-pass pass #FIXME: NEED to fix test # pylint: disable=pointless-string-statement '''self.request.query_params['$select'] = u'field1,field2'
class BidderBlacklistBulkSpecs(spec(BidderBlacklistBulk), BaseBidderBlackWhitelist): bidder_black_list_service = None bidder_blacklist1 = None bidder_blacklist2 = None bidder_blacklist3 = None source = None request = None subject = None def before_each(self): self._create_client() self._create_dependency_objects() self.bidder_blacklist1 = self._create_black_list_entry( self.campaign.pk, self.ad_group.pk, self.source.pk) self.bidder_blacklist2 = self._create_black_list_entry( self.campaign.pk, self.ad_group.pk, self.source1.pk) self.bidder_blacklist3 = self._create_black_list_entry( self.campaign.pk, self.ad_group.pk, self.source2.pk) bidder_service = Mock() request = Mock() request.QUERY_PARAMS = {} self.request = request self.bidder_black_list_service = bidder_service self.subject = BidderBlacklistBulk( bidder_service=lambda: bidder_service) def test__post__bidder_black_list_bulk_edit(self): # pylint: disable=no-member self.assertEqual(3, BidderBlacklistIds.objects.count()) self.request.DATA = self._build_request_1(self.bidder_blacklist1, self.bidder_blacklist2, self.bidder_blacklist3) response = self.subject.post(self.request) self.assertEqual(response.status_code, 200) # pylint: disable=no-member self.assertEqual(4, BidderBlacklistIds.objects.count()) for entry in list(BidderBlacklistIds.objects.all()): self.assertEqual(entry.placement_type, 'site') self.assertEqual(entry.placement_id, 'test_update_placement_id') self.assertEqual(entry.tag, 'test_update_tag') audit_qs = AuditLog.objects.filter( audit_type=AUDIT_TYPE_BIDDER_BLACK_LIST) self.assertTrue(len(audit_qs) > 0) self.bidder_blacklist3.delete() for audit in audit_qs: self.assertTrue(audit.audit_type, AUDIT_TYPE_BIDDER_BLACK_LIST) self.assertTrue(audit.audit_action == AUDIT_ACTION_ADD or audit.audit_action == AUDIT_ACTION_UPDATE or audit.audit_action == AUDIT_ACTION_DELETE) def test__post__bidder_black_list_bulck_edit_validation_error(self): # pylint: disable=no-member self.assertEqual(3, BidderBlacklistIds.objects.count()) self.request.DATA = self._build_request_2(self.bidder_blacklist1, self.bidder_blacklist2) self.assertRaises(ValidationError, self.subject.post, self.request) # Without transaction should be 5 rows but after roll back stay 3 as into begining # pylint: disable=no-member self.assertEqual(3, BidderBlacklistIds.objects.count())
class BlobsSpecs(spec(Blobs)): subject = None create_file_storage = None file_storage = None request = None def before_each(self): settings.MNG_CDN = None self.file_storage = Mock() # mock of AbstractFileStorage self.file_storage.upload_file_to_temp = Mock(return_value='file_id_1') self.file_storage.move_file_to_persistent_store = Mock( return_value={ 'path': '/a.txt', 'uri': 'http://cdn.io/a.txt', 'mime': 'text/plain' }) self.create_file_storage = Mock(return_value=self.file_storage) self.subject = Blobs(create_file_storage=self.create_file_storage) request = Mock() input_file = Mock() input_file.name = 'a.txt' request.FILES = {'file': input_file} request.QUERY_PARAMS = {} self.request = request def test__post__saves_files_in_blob_storage_with_the_specified_output_prefix( self): # act self.request.QUERY_PARAMS = {'outputPrefix': 'abc'} self.subject.post(self.request) # assert self.create_file_storage.assert_called_once_with(domain='abc', using='monarch') def test__post__saves_files_in_blob_storage(self): # act self.subject.post(self.request) # assert self.create_file_storage.assert_called_once_with(using='monarch') self.file_storage.upload_file_to_temp.assert_called_once_with( self.request.FILES['file'], 'a.txt') self.file_storage.move_file_to_persistent_store('file_id_1') def test__post__returns_blob_info(self): response = self.subject.post(self.request) result = response.data self.assertEqual(result['path'], 'a.txt') self.assertEqual(result['mime'], 'text/plain') self.assertEqual(result['uri'], 'https://cdn.io/a.txt') def test__post__returns_blob_with_cdn_ulr(self): settings.MNG_CDN = 'http://cdn.manage.com/' response = self.subject.post(self.request) result = response.data self.assertEqual(result['path'], 'a.txt') self.assertEqual(result['mime'], 'text/plain') self.assertEqual(result['uri'], 'http://cdn.manage.com/a.txt')
class TranscodingSpecs(spec(Transcoding)): subject = None request = None etc = None def before_each(self): self.etc = Mock() create_job_result1 = {'Job': {'Id': 'job1', 'Status': 'Submitted'}} self.etc.create_job = Mock(return_value=create_job_result1) transcoding_service = Mock() transcoding_service.create_connection = Mock(return_value=self.etc) transcoding_settings = { 'BUCKET': '1', 'FILE_PATTERN': 'https://{bucket}.s3.amazonaws.com/{key}', 'PIPELINE_ID': 'pipeline1', 'PRESET_ID_MPEG4_270P': 'preset_mpeg4', 'PRESET_ID_WEBM_270P': 'preset_webm', } transcoding_formats = { '480x270': ('preset_mpeg4', 'preset_webm'), } self.subject = Transcoding( transcoding_factory_cls=Mock(return_value=transcoding_service), transcoding_settings=transcoding_settings, transcoding_formats=transcoding_formats, rnd=lambda: 123123123) self.request = Mock() self.request.DATA = {'path': 'dir/file.mpg', 'outputPrefix': 'videos/'} def test__post__schedules_video_transcoding_to_mpeg4_and_mpeg4(self): # act self.subject.post(self.request) # assert self.etc.create_job.assert_called_with( pipeline_id='pipeline1', input_name={'Key': 'dir/file.mpg'}, outputs=[{ 'Width': '480', 'ThumbnailPattern': 'file_480x270_123123123000_mp4_thumb_{count}', 'PresetId': 'preset_mpeg4', 'Key': 'file_480x270_123123123000.mp4', 'Height': '270' }, { 'Width': '480', 'ThumbnailPattern': 'file_480x270_123123123000_webm_thumb_{count}', 'PresetId': 'preset_webm', 'Key': 'file_480x270_123123123000.webm', 'Height': '270' }], output_key_prefix='videos/') def test__post__returns_job_details_when_successfully_scheduled(self): # act response = self.subject.post(self.request) result = response.data # assert assert response.status_code == 201 assert result['id'] == 'job1' assert result['status'] == 'Submitted' assert len(result['outputs']) == 2 assert result['outputs'][0]['mime'] == 'video/mpeg4' assert result['outputs'][0][ 'path'] == 'https://1.s3.amazonaws.com/videos/file_480x270_123123123000.mp4' #pylint: disable=line-too-long assert result['outputs'][0][ 'thumbnail'] == 'https://1.s3.amazonaws.com/videos/file_480x270_123123123000_mp4_thumb_00001.jpg' assert result['outputs'][1]['mime'] == 'video/webm' assert result['outputs'][1][ 'path'] == 'https://1.s3.amazonaws.com/videos/file_480x270_123123123000.webm' #pylint: disable=line-too-long assert result['outputs'][1][ 'thumbnail'] == 'https://1.s3.amazonaws.com/videos/file_480x270_123123123000_webm_thumb_00001.jpg' def test__get__returns_job_details(self): # arrange self.etc.read_job = Mock(return_value={'Job': {'Status': 'Complete'}}) # act response = self.subject.get(Mock(), 'job123') result = response.data # assert self.etc.read_job.assert_called_with('job123') assert response.status_code == 200 assert result['status'] == 'Complete'
class AdsResubmitSpec(spec(AdsResubmit), CreateTestData): subject = None ad_service = None request = None ad_group = None ad1 = None ad2 = None ad3 = None ad_type = 1 encrypted_ad_id = 'HcMSVtnbTX6FZS1OcaBwwQ' creative_id = 1 size = '300x250' redirect_url = 'http://itunes.apple.com/' i_url = 'http://itunes.2apple.com/' def before_each(self): self._create_client() self.ad_group = self.create_ad_group() self.ad1 = self.__ad_object_create(self.ad_group, 0, 0, 'ad 1', 0, 'google.com', AD_STATUS_DISAPPROVED) self.ad2 = self.__ad_object_create(self.ad_group, 0, 0, 'ad 2', 0, 'google.com', AD_STATUS_DISAPPROVED) self.ad3 = self.__ad_object_create(self.ad_group, 0, 0, 'ad 3', 0, 'google.com', AD_STATUS_DISAPPROVED) ad_service = Mock() request = Mock() request.QUERY_PARAMS = {} self.request = request self.ad_service = ad_service self.subject = AdsResubmit(ad_service=lambda: ad_service) def test__post__ad_resubmit_all(self): ad_query_set = Ad.objects.all() self.assertEqual(3, ad_query_set.count()) self.assertTrue( self.__is_adx_status(ad_query_set, AD_STATUS_DISAPPROVED)) ad_ids_list = [self.ad1.pk, self.ad2.pk, self.ad3.pk] self.request.DATA = {'src_ad_ids': ad_ids_list} response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertTrue(self.__is_adx_status(Ad.objects.all(), AD_STATUS_NEW)) self.assertTrue(response.data is not None and response.data) audit_log_qs = AuditLog.objects.filter( item_id__in=ad_ids_list, audit_action=AUDIT_ACTION_UPDATE) self.assertEqual(3, audit_log_qs.count()) for audit_log_entry in audit_log_qs: self.assertEqual(audit_log_entry.old_data, '{"adx_status": "disapproved"}') self.assertEqual(audit_log_entry.new_data, '{"adx_status": "new"}') def test__post__ad_resubmit_bad_request_arguments(self): self.request.DATA = {'src_ad_ids': ['bad_request_long_list_expected']} response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST) self.assertTrue(response.data is None) def test__post__ad_resubmit_no_ads_were_found(self): # send wrong ad_id self.request.DATA = {'src_ad_ids': [self.ad3.pk + 1]} response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST) self.assertTrue(response.data == AdsResubmit.no_ads_found_msg) # pylint: disable=no-self-use def __is_adx_status(self, ad_query_set, adx_status): is_adx_status = True for ad_entry in ad_query_set: if ad_entry.adx_status != adx_status: is_adx_status = False return is_adx_status # pylint: disable=no-self-use def __ad_object_create(self, ad_group, creative_id, ad_type, ad_name, bid, i_url, adx_status): ad_object = Ad(ad_group_id=ad_group, creative_id=creative_id, ad_type=ad_type, ad=ad_name, bid=bid, i_url=i_url, adx_status=adx_status) ad_object.save() return ad_object
class RevmapsSpec(spec(RevmapsUpdateAll), CreateTestData): subject = None revmap_service = None request = None revmap_list = None campaign = None ad_group1 = None ad_group2 = None ad_group3 = None ad_group4 = None opt_type = 'install' opt_value = Decimal('12345.6789') rev_type = 'install' rev_value = Decimal('12345.6789') target_type = 'install' target_value = Decimal('12345.6789') last_update = now() def before_each(self): self._create_client() self.campaign = self.create_campaign() self.ad_group1 = AdGroup(campaign_id=self.campaign, ad_group='ad group 1', inflator_text='* 1.00') self.ad_group1.save() self.ad_group2 = AdGroup(campaign_id=self.campaign, ad_group='ad group 2', inflator_text='* 1.00') self.ad_group2.save() self.ad_group3 = AdGroup(campaign_id=self.campaign, ad_group='ad group 3', inflator_text='* 1.00') self.ad_group3.save() self.ad_group4 = AdGroup(campaign_id=self.campaign, ad_group='ad group 4', inflator_text='* 1.00') self.ad_group4.save() self.__create_entry(self.ad_group1, 53) self.__create_entry(self.ad_group2, 53) self.__create_entry(self.ad_group3, 53) revmap_service = Mock() request = Mock() request.query_params = {} request.QUERY_PARAMS = {} self.request = request self.revmap_service = revmap_service self.subject = RevmapsUpdateAll(revmap_service=lambda: revmap_service) def test__post__update_all_fields(self): # act self.request.DATA = { u'ad_group_ids': [ self.ad_group1.pk, self.ad_group2.pk, self.ad_group3.pk, self.ad_group4.pk ], u'opt_type': u'install', u'opt_value': u'12345.5555', u'rev_type': u'install', u'rev_value': u'12345.6666', u'target_type': u'install', u'target_value': u'12345.7777' } response = self.subject.post(self.request) # assert self.assertEqual(response.status_code, HTTP_200_OK) def test__post__update_two_fields(self): self.request.DATA = { u'ad_group_ids': [self.ad_group1.pk, self.ad_group2.pk, self.ad_group3.pk], u'opt_type': u'click', u'opt_value': u'12345.8888' } response = self.subject.post(self.request) # assert self.assertEqual(response.status_code, HTTP_200_OK) src_ad_group_ids = [long(x) for x in self.request.DATA['ad_group_ids']] for ad_group_id in src_ad_group_ids: revmap = Revmap.objects.get(ad_group_id=ad_group_id) self.assertEqual('click', revmap.opt_type) self.assertEqual(Decimal('12345.8888'), revmap.opt_value) def test__post__create_entry(self): self.assertRaises(Revmap.DoesNotExist, Revmap.objects.get, ad_group_id=self.ad_group4.pk) self.__create_entry(self.ad_group4, self.campaign.pk) revmap = Revmap.objects.get(ad_group_id=self.ad_group4.pk) self.assertIsNotNone(revmap) self.assertEqual(self.ad_group4.pk, revmap.ad_group_id.pk) self.assertEqual(self.ad_group4.ad_group, revmap.ad_group) self.assertEqual(self.campaign.pk, revmap.campaign_id) def __create_entry(self, ad_group, campaign_id): revmap = Revmap(ad_group_id=ad_group, ad_group=ad_group.ad_group, campaign_id=campaign_id, campaign=self.campaign.campaign, opt_type=self.opt_type, opt_value=self.opt_value, rev_type=self.rev_type, rev_value=self.rev_value, target_type=self.target_type, target_value=self.target_value, last_update=self.last_update) revmap.save() return revmap
class AdsReplicateSpecs(spec(AdsReplicate), CreateTestData): subject = None request = None campaign = None ad_group = None ad_group1 = None ad1 = None # pylint: disable=invalid-name ad = None def before_each(self): self._create_client() self.campaign = self.create_campaign() self.ad_group = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group') self.ad_group1 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 1') self.ad1 = self.create_ad(campaign_id=self.ad_group.campaign_id, ad_group_id=self.ad_group, ad_name='ad 1', size='300x250', status=STATUS_ENABLED) ads_replicate_service = Mock() request = Mock() request.query_params = {} request.QUERY_PARAMS = {} self.request = request self.subject = AdsReplicate( ads_replicate_service=lambda: ads_replicate_service) def test__post__creates_copies_of_specified_ads(self): self.assertTrue(self.ad_group.ad_set.count() == 1) self.assertTrue(self.ad_group1.ad_set.count() == 0) self.request.DATA = self.__get_request_data() response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertTrue(self.ad_group1.ad_set.count() == 1) copied_ad = self.ad_group1.ad_set.first() self.assertEqual(copied_ad.campaign_id, self.ad1.ad_group_id.campaign_id.campaign_id) self.assertEqual(copied_ad.adx_status, 'new') self.assertEqual(copied_ad.appnexus_status, 'new') self.assertEqual(copied_ad.a9_status, 'passed') self.assertEqual(copied_ad.status, STATUS_ENABLED) self.assertEqual(copied_ad.size, self.ad1.size) def test__post__creates_existing_copies(self): self.assertTrue(self.ad_group.ad_set.count() == 1) self.assertTrue(self.ad_group1.ad_set.count() == 0) self.request.DATA = self.__get_request_data() response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertRaises(ValidationError, self.subject.post, self.request) self.assertTrue(self.ad_group1.ad_set.count() == 1) def __get_request_data(self): return { 'src_ad_group_id': self.ad_group.pk, 'src_ad_ids': [self.ad1.pk], 'src_ad_statuses': [STATUS_ENABLED], 'dst_ad_group_ids': [self.ad_group1.pk], 'retain_ads_status': 'true', 'all_enabled_ads': 'false', 'all_paused_ads': 'false' }
class CustomHintBulkSpec(spec(CustomHintBulk), CreateTestData): custom_hint_bulk_service = None request = None subject = None source = None source1 = None def before_each(self): self._create_client() self.source = Source(source='name', class_value=CLASS_CHOICES[0][0]) self.source.save() self.source1 = Source(source='name_1', class_value=CLASS_CHOICES[0][0]) self.source1.save() custom_hint_service = Mock() request = Mock() request.QUERY_PARAMS = {} self.request = request self.custom_hint_bulk_service = custom_hint_service self.subject = CustomHintBulk( custom_hint_service=lambda: custom_hint_service) def test__post__custom_hint_bulk_validation_error(self): self.request.DATA = { 'delete': [], 'save': [{ 'campaign_id': 0, 'ad_group_id': 0, 'ad_id': 0, 'source_id': self.source.pk, 'size': '300x250', 'placement_type': 'site', 'placement_id': 'some_id' }, { 'campaign_id': 0, 'ad_group_id': 0, 'ad_id': 0, 'source_id': 0, 'size': '300x250', 'placement_type': 'Site', 'placement_id': 'some_id' }] } self.assertRaises(ValidationError, self.subject.post, self.request) def test__post__custom_hint_bulk_upload(self): self.request.DATA = { 'delete': [], 'save': [{ 'campaign_id': 0, 'ad_group_id': 0, 'ad_id': 0, 'source_id': self.source1.pk, 'size': '300x250', 'placement_type': 'site', 'placement_id': 'some_id', 'end_date': '2016-10-04T12:05:00Z' }, { 'campaign_id': 0, 'ad_group_id': 0, 'ad_id': 0, 'source_id': self.source.pk, 'size': '300x250', 'placement_type': 'site', 'placement_id': 'some_id_1', 'end_date': '2016-11-05T12:05:00Z' }] } response = self.subject.post(self.request) self.assertEqual(response.status_code, 200) self.assertTrue(CustomHintIds.objects.count() == 2)
class AdBulkSpec(spec(AdBulk), CreateTestData): ad_bulk_service = None campaign = None ad_group1 = None ad_group2 = None ad_group3 = None ad_group4 = None ad1 = None ad2 = None ad3 = None request = None subject = None html = '<a href="{CLICK_URL}">' \ '<img src="https://cdn.manage.com/71/iMob_300x250_2Girls.jpg" width="300" height="250"></a>' def before_each(self): self._create_client() self.campaign = self.create_campaign() self.ad_group1 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 1') self.ad_group2 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 2') self.ad_group3 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 3') self.ad_group4 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 4') self.ad1 = self.create_ad(ad_group_id=self.ad_group1, ad_name='ad 1') self.ad2 = self.create_ad(ad_group_id=self.ad_group2, ad_name='ad 2') self.ad3 = self.create_ad(ad_group_id=self.ad_group3, ad_name='ad 3') self.create_revmap(ad_group_id=self.ad_group1, campaign_id=self.campaign.campaign_id) self.create_revmap(ad_group_id=self.ad_group2, campaign_id=self.campaign.campaign_id) self.create_revmap(ad_group_id=self.ad_group3, campaign_id=self.campaign.campaign_id) self.create_revmap(ad_group_id=self.ad_group4, campaign_id=self.campaign.campaign_id) ad_service = Mock() request = Mock() request.query_params = {} request.QUERY_PARAMS = {} self.request = request self.ad_bulk_service = ad_service self.subject = AdBulk(ad_service=lambda: ad_service) def test__post__ad_bulk_edit(self): # pylint: disable=no-member self.assertEqual(3, Ad.objects.count()) self.request.DATA = \ { 'delete': [], 'save': [ { 'ad_group_id': self.ad_group4.ad_group_id, 'ad_type': 1, 'campaign_id': 71, 'encrypted_ad_id': 'HcMSVtnbTX6FZS1OcaBwwQ', 'creative_id': 0, 'ad': 'ad 4', 'size': '300x250', 'html': self.html, 'preview_html': '<a></a>', 'bid': '1.000000', 'targeting': '', 'categories': '', 'attrs': '', 'inflator_text': '1', 'domain': 'machinezone.com', 'redirect_url': 'https://itunes.apple.com/app/imob-2/id490367305?mt=8', 'status': 'enabled', 'adx_status': 'new', 'appnexus_status': 'new', 'a9_status': 'passed', 'external_args': '', 'adx_sensitive_categories': '', 'adx_product_categories': '', 'last_update': '2015-05-23T03:11:19', 'i_url': 'http://google.com', 'created_time': '2015-05-23T03:11:19', 'adx_attrs': '', 'tag': '' }, { 'id': self.ad1.pk, 'ad_group_id': self.ad1.ad_group_id.ad_group_id, 'ad_type': 1, 'campaign_id': 71, 'encrypted_ad_id': 'HcMSVtnbTX6FZS1OcaBwwQ', 'creative_id': 0, 'ad': 'ad 1', 'size': '300x250', 'html': self.html, 'preview_html': '<a></a>', 'bid': '1.000000', 'targeting': '', 'categories': '', 'attrs': '', 'inflator_text': '1', 'domain': 'machinezone.com', 'redirect_url': 'https://itunes.apple.com/app/imob-2/id490367305?mt=8', 'status': 'enabled', 'adx_status': 'new', 'appnexus_status': 'new', 'a9_status': 'passed', 'external_args': '', 'adx_sensitive_categories': '', 'adx_product_categories': '', 'last_update': '2015-05-23T03:11:19', 'i_url': 'http://google.com/', 'created_time': '2015-05-23T03:11:19', 'adx_attrs': '', 'tag': '' }, { 'id': self.ad2.pk, 'ad_group_id': self.ad2.ad_group_id.ad_group_id, 'ad_type': 1, 'campaign_id': 71, 'encrypted_ad_id': 'bE1s9a4HlXrP9hUkBLAGHA', 'creative_id': 0, 'ad': 'ad 2', 'size': '300x250', 'html': self.html, 'preview_html': '<a></a>', 'bid': '1.000000', 'targeting': '', 'categories': '', 'attrs': '', 'inflator_text': '1', 'domain': 'machinezone.com', 'redirect_url': 'https://itunes.apple.com/app/imob-2/id490367305?mt=8', 'status': 'enabled', 'adx_status': 'new', 'appnexus_status': 'new', 'a9_status': 'passed', 'external_args': '', 'adx_sensitive_categories': '', 'adx_product_categories': '', 'last_update': '2015-05-23T03:11:19', 'i_url': 'http://google.com/', 'created_time': '2015-05-23T03:11:19', 'adx_attrs': '', 'tag': '' }, { 'id': self.ad3.pk, 'ad_group_id': self.ad3.ad_group_id.ad_group_id, 'ad_type': 1, 'campaign_id': 71, 'encrypted_ad_id': 'D0RLOQbpY2JrYHLmWl43nw', 'creative_id': 0, 'ad': 'ad 3', 'size': '300x250', 'html': self.html, 'preview_html': '<a></a>', 'bid': '1.000000', 'targeting': '', 'categories': '', 'attrs': '', 'inflator_text': '1', 'domain': 'machinezone.com', 'redirect_url': 'https://itunes.apple.com/app/imob-2/id490367305?mt=8', 'status': 'enabled', 'adx_status': 'new', 'appnexus_status': 'new', 'a9_status': 'passed', 'external_args': '', 'adx_sensitive_categories': '', 'adx_product_categories': '', 'last_update': '2015-05-23T03:11:19', 'i_url': 'http://google.com', 'created_time': '2015-05-23T03:11:19', 'adx_attrs': '', 'tag': '' } ] } response = self.subject.post(self.request) self.assertEqual(response.status_code, 200) # pylint: disable=no-member self.assertEqual(4, Ad.objects.count()) for entry in list(Ad.objects.all()): self.assertEqual(entry.ad_type, 1) self.assertEqual(entry.bid, 1.000000) self.assertEqual(entry.redirect_url, 'https://itunes.apple.com/app/imob-2/id490367305?mt=8') def test__post__ad_bulk_edit_validation_error(self): # pylint: disable=no-member self.assertEqual(3, Ad.objects.count()) self.request.DATA = \ { 'delete': [], 'save': [ { 'id': self.ad1.pk, 'ad_group_id': self.ad1.ad_group_id.ad_group_id, 'ad_type': 1, 'campaign_id': 71, 'encrypted_ad_id': 'bE1s9a4HlXrP9hUkBLAGHA', 'creative_id': 0, 'ad': 'ad 1', 'size': '300x250', 'html': self.html, 'preview_html': '<a></a>', 'bid': '1.000000', 'targeting': '', 'categories': '', 'attrs': '', 'inflator_text': '1', 'domain': 'machinezone.com', 'redirect_url': 'https://itunes.apple.com/app/imob-2/id490367305?mt=8', 'status': 'enabled', 'adx_status': 'new', 'appnexus_status': 'new', 'a9_status': 'passed', 'external_args': '', 'adx_sensitive_categories': '', 'adx_product_categories': '', 'last_update': '2015-05-23T03:11:19', 'i_url': '', 'created_time': '2015-05-23T03:11:19', 'adx_attrs': '', 'tag': '' }, { 'id': self.ad2.pk, 'ad_group_id': self.ad1.ad_group_id.ad_group_id, 'ad_type': 1, 'campaign_id': 71, 'encrypted_ad_id': 'D0RLOQbpY2JrYHLmWl43nw', 'creative_id': 0, 'ad': 'ad 1', 'size': '300x250', 'html': '<a></a>', 'preview_html': self.html, 'bid': '1.000000', 'targeting': '', 'categories': '', 'attrs': '', 'inflator_text': '1', 'domain': 'machinezone.com', 'redirect_url': 'https://itunes.apple.com/app/imob-2/id490367305?mt=8', 'status': 'enabled', 'adx_status': 'new', 'appnexus_status': 'new', 'a9_status': 'passed', 'external_args': '', 'adx_sensitive_categories': '', 'adx_product_categories': '', 'last_update': '2015-05-23T03:11:19', 'i_url': 'h', 'created_time': '2015-05-23T03:11:19', 'adx_attrs': '', 'tag': '' } ] } self.assertRaises(ValidationError, self.subject.post, self.request) # Without transaction should be 5 rows but after roll back stay 3 as into begining # pylint: disable=no-member self.assertEqual(3, Ad.objects.count())
class AdGroupBulkSpec(spec(AdGroupBulk), CreateTestData): ad_group_bulk_service = None advertiser = None campaign = None campaign1 = None ad_group1 = None ad_group2 = None ad_group3 = None request = None subject = None frequency_map = json.dumps({ "*": "1/28800", "0x0": "3/86400", "320x50": "1/7200", "728x90": "1/7200" }) frequency_interval = 1 max_frequency = 2 def before_each(self): self._create_client() self.advertiser = self.create_advertiser() self.campaign = self.create_campaign(advertiser_id=self.advertiser, campaign_name='campaign_1') self.campaign1 = self.create_campaign(advertiser_id=self.advertiser, campaign_name='campaign_2') self.ad_group1 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 1') self.ad_group2 = self.create_ad_group(campaign_id=self.campaign, ad_group_name='ad group 2') ad_group_service = Mock() request = Mock() request.QUERY_PARAMS = {} self.request = request self.ad_group_bulk_service = ad_group_service self.subject = AdGroupBulk(ad_group_service=lambda: ad_group_service) def test__post__ad_group_bulk_edit(self): #FIXME: restore this test return # pylint: disable=no-member self.assertEqual(2, AdGroup.objects.count()) self.request.DATA = self._get_request_date(self.ad_group1, self.ad_group2, self.campaign1) response = self.subject.post(self.request) self.assertEqual(response.status_code, 200) # pylint: disable=no-member self.assertEqual(3, AdGroup.objects.count()) for entry in list(AdGroup.objects.all()): self.assertEqual(entry.status, 'paused') def test__post__ad_bulk_edit_validation_error(self): # pylint: disable=no-member self.assertEqual(2, AdGroup.objects.count()) self.request.DATA = self._get_request_date(self.ad_group1, self.ad_group2, self.campaign) self.assertRaises(ValidationError, self.subject.post, self.request) # Without transaction should be 5 rows but after roll back stay 3 as into begining # pylint: disable=no-member self.assertEqual(2, AdGroup.objects.count()) # pylint: disable=no-self-use def _get_request_date(self, ad_group1, ad_group2, campaign): return { 'delete': [], 'patch': [{ "id": ad_group1.ad_group_id, "campaign_id": ad_group1.campaign_id_id, "frequency_map": self.frequency_map, "frequency_interval": self.frequency_interval, "max_frequency": self.max_frequency, "inflator_text": "* 1", "targeting": "", "event_args": "", "flight_start_date": '2014-12-24T14:00:00Z', "flight_end_date": '2014-12-31T23:00:00Z', "parent_name": "", "ad_group": "ad_group1_test", "ad_group_type": 0, "categories": "", "domain": "", "notes": "", "redirect_url": "", "revmap_rev_type": 'click', "revmap_rev_value": '0.00', "revmap_opt_type": 'click', "revmap_opt_value": '0.00', "priority": 0, "daily_budget_type": "", "daily_budget_value": "0.00", "daily_spend": "0.00", "capped": 'false', "hourly_capped": 'false', "status": "paused", "tag": 'null', "flight_budget_type": "", "flight_budget_value": "0.00", "bidder_args": "", "last_update": "2015-08-17T15:26:29Z", "total_cost_cap": "0.00", "daily_cost_cap": "0.00", "total_loss_cap": "0.00", "daily_loss_cap": "0.00" }, { "id": ad_group2.ad_group_id, "campaign_id": ad_group2.campaign_id_id, "frequency_map": self.frequency_map, "frequency_interval": self.frequency_interval, "max_frequency": self.max_frequency, "inflator_text": "* 1", "targeting": "", "event_args": "", "flight_start_date": '2014-12-24T14:00:00Z', "flight_end_date": '2014-12-31T23:00:00Z', "parent_name": "", "ad_group": "ad_group_test", "ad_group_type": 0, "categories": "", "domain": "", "notes": "", "redirect_url": "", "revmap_rev_type": 'click', "revmap_rev_value": '0.00', "revmap_opt_type": 'click', "revmap_opt_value": '0.00', "priority": 0, "daily_budget_type": "", "daily_budget_value": "0.00", "daily_spend": "0.00", "capped": 'false', "hourly_capped": 'false', "status": "paused", "tag": 'null', "flight_budget_type": "", "flight_budget_value": "0.00", "bidder_args": "", "last_update": "2015-08-17T15:26:29Z", "total_cost_cap": "0.00", "daily_cost_cap": "0.00", "total_loss_cap": "0.00", "daily_loss_cap": "0.00" }], 'save': [{ "campaign_id": campaign.campaign_id, "frequency_map": self.frequency_map, "frequency_interval": self.frequency_interval, "max_frequency": self.max_frequency, "inflator_text": "* 1", "targeting": "", "event_args": "", "flight_start_date": '2014-12-24T14:00:00Z', "flight_end_date": '2014-12-31T23:00:00Z', "parent_name": "", "ad_group": "ad_group_test", "ad_group_type": 0, "categories": "", "domain": "", "notes": "", "redirect_url": "", "revmap_rev_type": 'click', "revmap_rev_value": '0.00', "revmap_opt_type": 'click', "revmap_opt_value": '0.00', "priority": 0, "daily_budget_type": "", "daily_budget_value": "0.00", "daily_spend": "0.00", "capped": 'false', "hourly_capped": 'false', "status": "paused", "tag": 'null', "flight_budget_type": "", "flight_budget_value": "0.00", "bidder_args": "", "last_update": "2015-08-17T15:26:29Z", "total_cost_cap": "0.00", "daily_cost_cap": "0.00", "total_loss_cap": "0.00", "daily_loss_cap": "0.00" }] }
class AdsBulkSpec(spec(AdBulk), CreateTestData): subject = None ad_service = None request = None advertiser = None campaign = None ad_group = None ad_base = None ad_type = 1 encrypted_ad_id = 'HcMSVtnbTX6FZS1OcaBwwQ' creative_id = 1 size = '300x250' redirect_url = 'http://itunes.apple.com/app/imob-2/id490367305?mt=8' i_url = 'http://itunes.2apple.com/app/imob-2/id490367305?mt=8' inflator_text = 'Burstly 1.00\nAppSponsor 1.00\n* 0.00' def before_each(self): self._create_client() self.ad_group = self.create_ad_group() self.ad_base = Ad(ad_group_id=self.ad_group, creative_id=0, ad_type=-0, ad='ad 1', bid=0, i_url='google.com') self.ad_base.save() ad_service = Mock() request = Mock() request.QUERY_PARAMS = {} self.request = request self.ad_service = ad_service self.subject = AdBulk(ad_service=lambda: ad_service) def test__post__ad_bulk_upload_edit(self): # pylint: disable=no-member self.assertEqual(1, Ad.objects.count()) self.request.DATA = self.__request_data(self.ad_group, self.ad_base) response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) self.assertEqual(3, Ad.objects.count()) # pylint: disable=invalid-name for ad in list(Ad.objects.all()): self.assertEqual(ad.ad_group_id.ad_group_id, self.ad_group.pk) self.assertEqual(ad.ad_type, self.ad_type) self.assertEqual(ad.campaign_id, self.ad_group.campaign_id.campaign_id) self.assertEqual(ad.creative_id, self.creative_id) self.assertEqual(ad.size, self.size) self.assertEqual(ad.redirect_url, self.redirect_url) self.assertEqual(ad.i_url, self.i_url) self.assertEqual(ad.inflator_text, self.inflator_text) def test__post__ad_bulk_delete(self): ad1 = Ad(ad_group_id=self.ad_group, creative_id=0, ad_type=-0, ad='ad 2', bid=0, i_url='google.com') ad1.save() # pylint: disable=no-member self.assertEqual(2, Ad.objects.count()) self.request.DATA = {'delete': [self.ad_base.pk, ad1.pk], 'save': []} response = self.subject.post(self.request) self.assertEqual(response.status_code, HTTP_200_OK) # pylint: disable=no-member self.assertEqual(0, Ad.objects.count()) # pylint: disable=invalid-name def __request_data(self, ad_group, ad): return {'delete': [], 'save': [ { "id": ad.pk, "ad_group_id": ad_group.pk, "ad_type": self.ad_type, "campaign_id": ad_group.campaign_id.campaign_id, "encrypted_ad_id": self.encrypted_ad_id, "creative_id": self.creative_id, "ad": "iMob_300x250_2Girls", "size": self.size, "html": "<a href=\"{CLICK_URL}\"></a>", "preview_html": "<a href=\"{CLICK_URL}\"></a>", "bid": "0.000000", "targeting": "", "categories": "", "attrs": "", "inflator_text": self.inflator_text, "domain": "machinezone.com", "redirect_url": self.redirect_url, "status": "enabled", "adx_status": "new", "appnexus_status": "new", "a9_status": "passed", "external_args": "", "adx_sensitive_categories": "", "adx_product_categories": "", "last_update": "2015-08-14T18:05:27", "i_url": self.i_url, "created_time": "2015-08-14T18:05:27", "adx_attrs": "", "tag": "" }, { "ad_group_id": ad_group.pk, "ad_type": self.ad_type, "campaign_id": ad_group.campaign_id.campaign_id, "encrypted_ad_id": self.encrypted_ad_id, "creative_id": self.creative_id, "ad": "iMob_300x250_2Girls_1", "size": self.size, "html": "<a href=\"{CLICK_URL}\"></a>", "preview_html": "<a href=\"{CLICK_URL}\"></a>", "bid": "0.000000", "targeting": "", "categories": "", "attrs": "", "inflator_text": self.inflator_text, "domain": "machinezone.com", "redirect_url": self.redirect_url, "status": "enabled", "adx_status": "new", "appnexus_status": "new", "a9_status": "passed", "external_args": "", "adx_sensitive_categories": "", "adx_product_categories": "", "last_update": "2015-08-14T18:05:27", "i_url": self.i_url, "created_time": "2015-08-14T18:05:27", "adx_attrs": "", "tag": "" }, { "ad_group_id": ad_group.pk, "ad_type": self.ad_type, "campaign_id": ad_group.campaign_id.campaign_id, "encrypted_ad_id": self.encrypted_ad_id, "creative_id": self.creative_id, "ad": "iMob_300x250_2Girls_2", "size": self.size, "html": "<a href=\"{CLICK_URL}\"></a>", "preview_html": "<a href=\"{CLICK_URL}\"></a>", "bid": "0.000000", "targeting": "", "categories": "", "attrs": "", "inflator_text": self.inflator_text, "domain": "machinezone.com", "redirect_url": self.redirect_url, "status": "enabled", "adx_status": "new", "appnexus_status": "new", "a9_status": "passed", "external_args": "", "adx_sensitive_categories": "", "adx_product_categories": "", "last_update": "2015-08-14T18:05:27", "i_url": self.i_url, "created_time": "2015-08-14T18:05:27", "adx_attrs": "", "tag": "" } ]}