# Path: bot/games/models.py
# class Game(models.Model):
#     name = models.CharField(_('name'), max_length=255)
#     alias_for = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)
# 
#     objects = GameQuerySet.as_manager()
# 
#     class Meta:
#         verbose_name = _('game')
#         verbose_name_plural = _('games')
# 
#     def __str__(self):
#         return self.name
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self._old_alias_for_id = self.alias_for_id
# 
#     def clean(self):
#         if self.alias_for and self.alias_for.alias_for:
#             raise ValidationError(_("Aliases can only span one level"))
#         if self.alias_for and self.alias_for.name == self.name:
#             raise ValidationError(_("An alias must have a different name"))


from django.test import TestCase
from ..models import Game
# -*- coding: utf-8 -*-



class QuerySetTests(TestCase):

    def test_get_by_name(self):

        gta_v = Game.objects.create(name='GTA V')
        Game.objects.create(name='Grand Theft Auto V', alias_for=gta_v)

        game = Game.objects.get_by_name('gta V')
        self.assertEqual(game, gta_v)

        game2 = Game.objects.get_by_name('Grand Theft Auto V')
        self.assertEqual(game2, gta_v)

        # non-existing game should be created
        overwatch = Game.objects.get_by_name('Overwatch')
        self.assertIsNotNone(overwatch.pk)

    def test_get_by_name_distinct(self):

        bf1 = Game.objects.create(name='Battlefield 1')
        Game.objects.create(name='Battlefield™ 1 Open Beta', alias_for=bf1)

# Path: bot/games/models.py
# class Game(models.Model):
#     name = models.CharField(_('name'), max_length=255)
#     alias_for = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)
# 
#     objects = GameQuerySet.as_manager()
# 
#     class Meta:
#         verbose_name = _('game')
#         verbose_name_plural = _('games')
# 
#     def __str__(self):
#         return self.name
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self._old_alias_for_id = self.alias_for_id
# 
#     def clean(self):
#         if self.alias_for and self.alias_for.alias_for:
#             raise ValidationError(_("Aliases can only span one level"))
#         if self.alias_for and self.alias_for.name == self.name:
#             raise ValidationError(_("An alias must have a different name"))


# Path: bot/games/tests/test_game_queryset.py
from django.test import TestCase
from ..models import Game

# -*- coding: utf-8 -*-



class QuerySetTests(TestCase):

    def test_get_by_name(self):

        gta_v = Game.objects.create(name='GTA V')
        Game.objects.create(name='Grand Theft Auto V', alias_for=gta_v)

        game = Game.objects.get_by_name('gta V')
        self.assertEqual(game, gta_v)

        game2 = Game.objects.get_by_name('Grand Theft Auto V')
        self.assertEqual(game2, gta_v)

        # non-existing game should be created
        overwatch = Game.objects.get_by_name('Overwatch')
        self.assertIsNotNone(overwatch.pk)

    def test_get_by_name_distinct(self):

        bf1 = Game.objects.create(name='Battlefield 1')
        Game.objects.create(name='Battlefield™ 1 Open Beta', alias_for=bf1)

        Game.objects.create(name='Battlefield™ 1', alias_for=bf1)