def test__campaign_name_bigger_than_50__error(self): # Act serializer = CampaignSerializer( data=JsonObjects.campaign(name=self.NAME_WITH_51_CHARACTERS)) serializer.is_valid() # Assert self.assertEqual(self.ERRO_NAME_BIGGER_THAN_50, str(serializer.errors[Field.NAME]))
def test__create_campaign_with_begin_date_bigger_than_end_date__false(self): # Arrange data = JsonObjects.campaign(begin_date="2055-09-04T19:13:40Z") # Act serializer = CampaignSerializer(data=data) is_true = serializer.is_valid() # Assert self.assertFalse(is_true)
def test__compare_dict_list__different_lists__true(self): # Act response = CampaignSerializer().compare_dict_lists(self.list_1_to_compare_dict_lists, self.list_2_to_compare_dict_lists) response_inverted_parameters = CampaignSerializer().compare_dict_lists(self.list_2_to_compare_dict_lists, self.list_1_to_compare_dict_lists) # Assert self.assertTrue(response) self.assertTrue(response_inverted_parameters)
def test__campaign_has_been_started__begin_date_greater_than_timezone_now__false(self, mock_timezone): # Arrange mock_timezone.return_value = datetime(2012, 2, 24, 13, 59, tzinfo=timezone("UTC")) # Act response = CampaignSerializer().campaign_has_been_started(self.instance) # Assert self.assertFalse(response)
def test__started_campaign_can_be_updated__just_campaign_was_modified__true(self, mock_game, mock_region, mock_campaign): # Act was_modified = CampaignSerializer().started_campaign_can_be_updated(self.instance, self.validate_data, "MOCK", "MOCK") # Assert self.assertTrue(was_modified)
def test__campaign__was__modified__false(self, mock_game, mock_regions): # Act was_modified = CampaignSerializer().was_campaign_modified(instance=self.instance, validated_data=self.validate_data, form_data='MOCK', regions_data='MOCK') # Assert self.assertFalse(was_modified)
def test__started_campaign_can_be_updated__game_and_regions_modified__false(self, mock_game, mock_region, mock_campaign): # Act was_modified = CampaignSerializer().started_campaign_can_be_updated(self.instance, self.validate_data, "MOCK", "MOCK") # Arrange self.assertFalse(was_modified)
def test__campaign_finished_by_participant_limit__participants_total__greater_than_participant_limit__true( self, mock_participations_count): # Arrange mock_participations_count.count.return_value = self.instance.participant_limit + 1 # Act response = CampaignSerializer().is_campaign_finished_by_participant_limit(self.instance) # Assert self.assertTrue(response)
class BannerConfigurationSerializer(serializers.ModelSerializer): banners = BannerSerializer(many=True) regions = RegionSerializer(many=True) games = GameSerializer(many=True) campaigns = CampaignSerializer(many=True) class Meta: model = BannerConfiguration related_fields = (Field.BANNERS, Field.REGIONS, Field.GAMES) fields = (Field.ID, Field.MODIFIED_DATE, Field.BANNER_CONFIGURATION_NAME, Field.ACTIVE, Field.CAMPAIGNS, Field.BEGIN, Field.END, Field.GMT_TIMEZONE, Field.IS_EXPIRED, Field.IS_USING_CAMPAIGN_DATE) + related_fields def validate(self, data): begin = None end = None if {Field.END, Field.BEGIN, Field.GMT_TIMEZONE}.issubset(set(data.keys())): is_begin_or_end_date_null = xor(data[Field.END] is None, data[Field.BEGIN] is None) if (data[Field.GMT_TIMEZONE] or not data[Field.GMT_TIMEZONE]) and is_begin_or_end_date_null: raise serializers.ValidationError({Constants.DATES: Constants.BEGIN_OR_END_DATE_NULL_ERROR}) elif data[Field.BEGIN] and data[Field.END] and not data[Field.GMT_TIMEZONE]: raise serializers.ValidationError({Field.GMT_TIMEZONE: Constants.TIMEZONE_ERROR}) if data[Field.END] and data[Field.BEGIN]: if self.instance: begin = data.get(Field.BEGIN, self.instance.begin_date) end = data.get(Field.END, self.instance.end_date) else: begin = data.get(Field.BEGIN) end = data.get(Field.END) if end <= begin: raise serializers.ValidationError( Constants.END_DATE + " must be greater than " + Constants.BEGIN_DATE) return data def validate_banners(self, data): if not data: raise serializers.ValidationError({Field.BANNERS: Constants.EMPTY_FIELD}) return self.initial_data[Field.BANNERS] def validate_games(self, data): if not data: raise serializers.ValidationError({Field.GAMES: Constants.EMPTY_FIELD}) return self.initial_data[Field.GAMES] def validate_regions(self, data): return ValidateRegionsUtil.validate_regions(self.initial_data, data) def validate_campaigns(self, data): return self.initial_data[Field.CAMPAIGNS] def create(self, validated_data): return BannerConfiguration.manager.create(validated_data) def update(self, instance, validated_data): return BannerConfiguration.manager.edit(instance, validated_data)
def test__game__was_modified__false(self): # Arrange rule = [] rule_object = Rule(**self.rule_dict) rule.append(rule_object) # Act is_modified = CampaignSerializer().was_game_modified(self.form_data, rule) # Assert self.assertFalse(is_modified)
def test__regions__was_modified__false(self): # Arrange old_region_list = [] old_region_object = Region(**self.old_region) old_region_list.append(old_region_object) # Act was_modified = CampaignSerializer().was_regions_modified(old_region_list, self.new_region) # Assert self.assertFalse(was_modified)
def test__update_started_campaign__started_campaign_with_invalid_rules__exception( self, mock_started_campaign_can_be_updated): # Arrange regions = Mock() form_data = Mock() validated_data = Mock() # Act / Assert with self.assertRaisesMessage(ValidationError, Constants.STARTED_CAMPAIGN_EDIT_CONTROL_ERROR): CampaignSerializer().update_started_campaign(self.instance, validated_data, regions, form_data)
def test__game__was_modified__true(self): # Arrange rule = [] self.rule_dict[Field.GAME_ID] = 100 rule_object = Rule(**self.rule_dict) rule.append(rule_object) # Act is_modified = CampaignSerializer().was_game_modified(self.form_data, rule) # Assert self.assertTrue(is_modified)
def test__update_campaign_according_rules__campaign_not_modified__campaign_instance( self, mock_campaign_finished_by_participant_limit, mock_campaign_has_been_started): # Arrange regions_data = "fake_region" form_data = 'fake_form_data' # Act response = CampaignSerializer().update_campaign_according_rules(self.instance, self.validate_data, regions_data, form_data) # Assert self.assertIsInstance(response, Campaign)
def test__update_campaign_according_rules__not_started_campaign__exception( self, mock_campaign_finished_by_participant_limit, mock_campaign_has_been_started): # Arrange self.validate_data[Field.PARTICIPANT_LIMIT] += 1 regions_data = "fake_region" form_data = 'fake_form_data' # Act / Assert with self.assertRaisesMessage(ValidationError, Constants.PARTICIPANT_LIMIT_EDIT_CONTROL_ERROR): CampaignSerializer().update_campaign_according_rules(self.instance, self.validate_data, regions_data, form_data)
def test__update_started_campaign__started_campaign__campaign_instance( self, mock_started_campaign_can_be_updated, mock_edit_campaign): # Arrange regions = Mock() form_data = Mock() validated_data = Mock() # Act response = CampaignSerializer().update_started_campaign(self.instance, validated_data, regions, form_data) # Assert self.assertIsInstance(response, Campaign)
def test__create_campaign__new_campaign(self): # Arrange create_game(name="zennyworldz", analytics_view_id=00000, package_name="br.com.asus.app.zennyworldz", form_template=FORM_TEMPLATE, game_image="https://gamma-test.s3.amazonaws.com/game/game_icon_1460643319494.jpg", api_key="lr4cE2jGYJj46H8Gw0esY6k8Ll98Ej6D", game_service_id=1) # Act serializer = CampaignSerializer().create(JsonObjects.campaign()) # Assert self.assertIsNotNone(serializer) self.assertFalse(serializer.active)
def test__update_campaign_according_rules__campaign_finished_by_participant_limit__exception( self, mock_campaign_finished_by_participant_limit, mock_was_campaign_modified): # Arrange regions_data = "fake_region" form_data = 'fake_form_data' validate_data = "fake_validated_data" instance = "fake_instance" # Act / Assert with self.assertRaisesMessage(ValidationError, Constants.CAMPAIGN_FINISHED_BY_PARTICIPANT_LIMIT_EDIT_CONTROL_ERROR): CampaignSerializer().update_campaign_according_rules(instance, validate_data, regions_data, form_data)
def test__active_campaign__invalid_type(self): # Act serializer = CampaignSerializer(data=JsonObjects.campaign(active="Active")) serializer.is_valid() # Assert self.assertEqual(self.ERROR_INVALID_TYPE_ACTIVE_FIELD, str(serializer.errors[Field.ACTIVE]))
def test__active_campaign__not_null(self): # Act serializer = CampaignSerializer(data=JsonObjects.campaign(active=None)) serializer.is_valid() self.assertEqual(self.ERROR_NOT_NULL, str(serializer.errors[Field.ACTIVE]))
def test__campaign_name__null(self): # Act serializer = CampaignSerializer(data=JsonObjects.campaign(name=None)) serializer.is_valid() # Assert self.assertEqual(self.ERROR_NOT_NULL, str(serializer.errors[Field.NAME]))
def test__compare_dict_list__equal_lists__false(self): # Act response = CampaignSerializer().compare_dict_lists(self.list_1_to_compare_dict_lists, self.list_1_to_compare_dict_lists) # Assert self.assertFalse(response)
def test__update_not_started_campaign__not_started_campaign_with_invalid_rules__exception( self, mock_started_campaign_can_be_updated): # Assert / Act with self.assertRaisesMessage(ValidationError, Constants.PARTICIPANT_LIMIT_EDIT_CONTROL_ERROR): CampaignSerializer().update_not_started_campaign(self.instance, self.validate_data)
def test__update_not_started_campaign__not_started_campaign__campaign_instance( self, mock_started_campaign_can_be_updated, mock_edit_campaign): # Act response = CampaignSerializer().update_not_started_campaign(self.instance, self.validate_data) # Assert self.assertIsInstance(response, Campaign)