class TestPlatform: @pytest.mark.django_db @pytest.mark.parametrize('name, icon, extensions, errors_dict', [ ('platform_name', 'test_image.ppm', 'deb', mount_error_dict(['icon'], [[ErrorMessage.IMAGE_EXTENSION]])), ('platform_name', 'test_image.py', 'deb', mount_error_dict(['icon'], [[ErrorMessage.NOT_IMAGE.value[1]]])), ]) def test_icon_extension(self, name, icon, extensions, errors_dict): platform = Platform(name=name, icon=icon, extensions=extensions) validation_test(platform, errors_dict) def test_str(self): platform = PlatformFactory.build() assert str(platform) == '{} (.deb)'.format(platform.name) @pytest.mark.django_db def test_update_relationships(self, platform): platform.extensions = 'deb' package = PackageFactory() package.package.file = 'package.deb' platform.save() package.save() assert package.platforms.last().pk == platform.pk platform2 = PlatformFactory(extensions='deb') platform2.save() assert package.platforms.last().pk == platform2.pk assert package.platforms.count() == 2
class TestPlatform: @pytest.mark.django_db @pytest.mark.parametrize('field, value, errors_dict', [ ('name', '', mount_error_dict(['name'], [[ErrorMessage.BLANK]])), ('name', None, mount_error_dict(['name'], [[ErrorMessage.NULL]])), ]) def test_field_validation(self, field, value, errors_dict): platform = PlatformFactory.build() setattr(platform, field, value) validation_test(platform, errors_dict) def test_str(self): platform = PlatformFactory.build() assert str(platform) == '{} (.deb)'.format(platform.name) @pytest.mark.django_db def test_update_relationships(self, platform): platform.extensions = 'deb' package = PackageFactory() package.package.file = 'package.deb' platform.save() package.save() assert package.platforms.last().pk == platform.pk platform2 = PlatformFactory(extensions='deb') platform2.save() assert package.platforms.last().pk == platform2.pk assert package.platforms.count() == 2
class TestGame: @pytest.fixture def package(self): return PackageFactory() @pytest.mark.django_db @pytest.mark.parametrize( ('name, cover_image, version, ' + 'official_repository, errors_dict'), [ ('game_name', 'test_image.ppm', '1.0', 'http://a.com', mount_error_dict(['cover_image'], [[ErrorMessage.IMAGE_EXTENSION]])), ('game_name', 'test_image.py', '1.0', 'http://a.com', mount_error_dict(['cover_image'], [[ErrorMessage.NOT_IMAGE.value[1]]])), ]) def test_cover_image_extension(self, name, cover_image, version, official_repository, errors_dict): game = Game(name=name, cover_image=cover_image, version=version, official_repository=official_repository) validation_test(game, errors_dict) @pytest.mark.django_db def test_create_game_with_valid_atributtes(self, game): game = Game.objects.get(pk=game.pk) assert game == game @pytest.mark.django_db def test_str_game(self): game = GameFactory.build(version=None, name="Game") assert str(game) == "Game" game.version = "1.1" assert str(game) == "Game v1.1"
class TestGame: def test_game_sum_downloads(self): assert Game.PACKAGE_SUM_QUERY == "SELECT SUM(game_package.downloads) "\ "FROM game_package WHERE game_packag"\ "e.game_id = game_game.id" @pytest.fixture def package(self): return PackageFactory() @pytest.mark.django_db @pytest.mark.parametrize( ('name, cover_image, ' + 'official_repository, errors_dict'), [ ('game_name', 'test_image.ppm', 'http://a.com', mount_error_dict(['cover_image'], [[ErrorMessage.IMAGE_EXTENSION]])), ('game_name', 'test_image.py', 'http://a.com', mount_error_dict(['cover_image'], [[ErrorMessage.NOT_IMAGE.value[1]]])), ]) def test_cover_image_extension(self, name, cover_image, official_repository, errors_dict): game = Game(name=name, cover_image=cover_image, official_repository=official_repository) validation_test(game, errors_dict) @pytest.mark.django_db def test_create_game_with_valid_atributtes(self, game): game = Game.objects.get(pk=game.pk) assert game == game
class TestAward: error_message_year_future = 'We believe the award was not won in the\ future!' @staticmethod def parametrized_str(attribute): error_message_max_length = 'Certifique-se de que o valor tenha no '\ 'máximo 100 caracteres (ele possui 101).' return [ ('', 2016, 'Unb-Gama', mount_error_dict([attribute], [[ErrorMessage.BLANK]])), (None, 2016, 'Unb-Gama', mount_error_dict([attribute], [[ErrorMessage.NULL]])), ('a' * 101, 2016, 'Unb-Gama', mount_error_dict([attribute], [[error_message_max_length]])), ] @pytest.mark.django_db @pytest.mark.parametrize("name, year, place, errors_dict", parametrized_str.__func__('name')) def test_name_validation(self, name, year, place, errors_dict): award = Award(name=name, place=place, year=year) validation_test(award, errors_dict) @pytest.mark.django_db @pytest.mark.parametrize("name, year, place, errors_dict", [ ('award_name', 1900, 'Unb-Gama', mount_error_dict(["year"], [[ErrorMessage.YEAR_PAST]])), ('award_name', 2018, 'Unb-Gama', mount_error_dict(["year"], [[error_message_year_future]])), ('award_name', None, 'Unb-Gama', mount_error_dict(["year"], [[ErrorMessage.NULL]])), ('award_name', '', 'Unb-Gama', mount_error_dict(["year"], [[ErrorMessage.NOT_INTEGER]])), ]) def test_year_validation(self, name, year, place, errors_dict): award = Award(name=name, place=place, year=year) validation_test(award, errors_dict) @pytest.mark.django_db @pytest.mark.parametrize("place, year, name, errors_dict", parametrized_str.__func__('place')) def test_place_validation(self, place, year, name, errors_dict): award = Award(name=name, place=place, year=year) validation_test(award, errors_dict) @pytest.mark.django_db def test_award_save(self, award_creation): award = Award.objects.get(pk=award_creation.pk) assert award == award_creation @pytest.mark.django_db def test_str_award(self, award_creation): assert str(award_creation) == "UnB (%d): %s" % (now(), "award")
def parametrized_str(attribute, text): error_message_max_length = "Valor "\ "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'" \ " não é uma opção válida." return [ ('', text, mount_error_dict([attribute], [[ErrorMessage.BLANK]])), (None, text, mount_error_dict([attribute], [[ErrorMessage.NULL]])), ('a' * 31, text, mount_error_dict([attribute], [[error_message_max_length]])), ]
def parametrized_str(attribute): error_message_max_length = 'Certifique-se de que o valor tenha no '\ 'máximo 100 caracteres (ele possui 101).' return [ ('', 2016, 'Unb-Gama', mount_error_dict([attribute], [[ErrorMessage.BLANK]])), (None, 2016, 'Unb-Gama', mount_error_dict([attribute], [[ErrorMessage.NULL]])), ('a' * 101, 2016, 'Unb-Gama', mount_error_dict([attribute], [[error_message_max_length]])), ]
class TestCreditValidation: error_message_max_length = 'Certifique-se de que o valor tenha no '\ 'máximo 100 caracteres (ele possui 101).' @pytest.mark.django_db @pytest.mark.parametrize("name, errors_dict", [ ('', mount_error_dict(["name"], [[ErrorMessage.BLANK]])), (None, mount_error_dict(["name"], [[ErrorMessage.NULL]])), ('a' * 101, mount_error_dict(["name"], [[error_message_max_length]])), ]) def test_name_validation(self, name, errors_dict): credit = CreditFactory.build(name=name) validation_test(credit, errors_dict)
def test_soundtrack_invalid_extension(self, game_created): soundtrack = SoundtrackFactory.build(soundtrack='soundtrack.mp4', game=game_created) validation_test( soundtrack, mount_error_dict(['soundtrack'], [[ErrorMessage.SOUNDTRACK_EXTENSION]]))
def test_package(self): PlatformFactory() package = PackageFactory.build(game=GameFactory()) with patch("game.validators._get_size", return_value=1 + 1024**3): validation_test( package, mount_error_dict(["package"], [[ErrorMessage.FILE_TOO_BIG]]))
class TestInformationValidation: error_message_min_value = "A game description must have at least 50 \ characters!" error_message_year_future = 'We believe the game was not won ' \ 'in the future!' @pytest.fixture def game(self): return GameFactory() @pytest.mark.django_db @pytest.mark.parametrize("launch_year, errors_dict", [ (None, mount_error_dict(["launch_year"], [[ErrorMessage.NULL]])), ("", mount_error_dict(["launch_year"], [[ErrorMessage.NOT_INTEGER]])), (1961, mount_error_dict(["launch_year"], [[ErrorMessage.YEAR_PAST]])), (now() + 1, mount_error_dict(["launch_year"], [[error_message_year_future]])), ]) def test_launch_year_validation(self, launch_year, errors_dict, game): information = InformationFactory.build(launch_year=launch_year, game=game) validation_test(information, errors_dict) @pytest.mark.django_db @pytest.mark.parametrize("description, errors_dict", [ (None, mount_error_dict(["description"], [[ErrorMessage.NULL]])), ("", mount_error_dict(["description"], [[ErrorMessage.BLANK]])), ('short', mount_error_dict(["description"], [[error_message_min_value]])), ]) def test_description_validation(self, description, errors_dict, game): information = InformationFactory.build(description=description, game=game) validation_test(information, errors_dict)
class TestDeveloperAvatar: @pytest.mark.django_db @pytest.mark.parametrize(('avatar', 'errors_dict'), [ ('avatar.ppm', mount_error_dict(['avatar'], [[ErrorMessage.IMAGE_EXTENSION]])), ('avatar.py', mount_error_dict(['avatar'], [[ ErrorMessage.NOT_IMAGE.value[0], ErrorMessage.NOT_IMAGE.value[1] ]])), ]) def test_avatar_valid_extension(self, avatar, errors_dict): developer = DeveloperFactory.build(avatar=avatar, ) validation_test(developer, errors_dict) @pytest.mark.django_db def test_avatar_invalid_extension(self): developer = DeveloperFactory() assert Developer.objects.last() == developer
class TestMediaImage: @pytest.mark.django_db @pytest.mark.parametrize('image, errors_dict', [ ('image.ppm', mount_error_dict(['image'], [[ErrorMessage.IMAGE_EXTENSION]])), ('image.py', mount_error_dict(['image'], [[ ErrorMessage.NOT_IMAGE.value[0], ErrorMessage.NOT_IMAGE.value[1] ]])), ]) def test_image_invalid_extension(self, image, errors_dict, game_created): image = ImageFactory.build(image=image, game=game_created) @pytest.mark.django_db def test_image_valid_extension(self, game_created): image = ImageFactory.build(game=game_created) image.save() assert Image.objects.last() == image
class TestPackage: '''Only package extensions which have platforms that can play it are allowed. ''' @pytest.mark.django_db @pytest.mark.parametrize('package_file, message', [('package.py', PACKAGE_EXTENSION_ERROR), ('package.deb', PACKAGE_EXTENSION_ERROR), ('package.exe', PACKAGE_EXTENSION_ERROR)]) def test_invalid_package_extensions(self, package_file, game, message): package = Package(package=package_file, game=game) with pytest.raises(ValidationError) as validation_error: package.save() assert validation_error.value.message == message @pytest.mark.django_db @pytest.mark.parametrize(('extension'), [ ('deb'), ('exe'), ]) def test_valid_package_extensions(self, extension): PlatformFactory(extensions=extension) package = PackageFactory.build(game=GameFactory()) package.package.name = package.package.name.replace('deb', extension) package.save() assert package == Package.objects.last() @pytest.mark.django_db def test_package_str(self, platform): package = PackageFactory() assert str(package) == "{} (.deb)".format(package.game.name) @pytest.mark.django_db def test_package(self, platform): package = PackageFactory.build(game=GameFactory()) with patch("game.validators._get_size", return_value=1 + 5 * 1024**3): validation_test( package, mount_error_dict(["package"], [[ErrorMessage.FILE_TOO_BIG]])) ERROR_MESSAGE = "Valor 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' " \ "não é uma opção válida." @pytest.mark.django_db @pytest.mark.parametrize('architecture, errors_dict', [ ('a' * 41, mount_error_dict(['architecture'], [[ERROR_MESSAGE]])), ]) def test_architecture_validation(self, architecture, errors_dict, platform): package = PackageFactory.build(architecture=architecture, game=GameFactory()) validation_test(package, errors_dict)
class TestGenreValidation: error_message_min_value = 'A genre description must have \ at least 20 characters!' short_description = "short description" error_message_max_length = 'Certifique-se de que o valor tenha no '\ 'máximo 100 caracteres (ele possui 101).' @pytest.mark.django_db @pytest.mark.parametrize("description, errors_dict", [ (None, mount_error_dict(["description"], [[ErrorMessage.NULL]])), ("", mount_error_dict(["description"], [[ErrorMessage.BLANK]])), (short_description, mount_error_dict(["description"], [[error_message_min_value]])), ]) def test_description_validation(self, description, errors_dict): genre = GenreFactory.build(description=description) validation_test(genre, errors_dict) @pytest.mark.django_db @pytest.mark.parametrize("name, errors_dict", [ ('', mount_error_dict(["name"], [[ErrorMessage.BLANK]])), (None, mount_error_dict(["name"], [[ErrorMessage.NULL]])), ('a' * 101, mount_error_dict(["name"], [[error_message_max_length]])), ]) def test_name_validation(self, name, errors_dict): genre = GenreFactory.build(name=name) validation_test(genre, errors_dict)
def test_video_invalid_extension(self, game_created): video = VideoFactory.build(video='video.jpg', game=game_created) validation_test( video, mount_error_dict(['video'], [[ErrorMessage.VIDEO_EXTENSION]]))