# Path: bot/plugins/stats/models.py
# class GameSession(models.Model):
#     member = models.ForeignKey('users.Member', on_delete=models.PROTECT)
#     server = models.BigIntegerField(_("server"))
#     game = models.ForeignKey('games.Game', on_delete=models.CASCADE)
#     start = models.DateTimeField(_('start'))
#     stop = models.DateTimeField(_('stop'), null=True, blank=True)
#     duration = models.DurationField(null=True, blank=True)
# 
#     objects = GameSessionQuerySet.as_manager()
# 
#     class Meta:
#         verbose_name = _('game session')
#         verbose_name_plural = _('game sessions')
# 
#     def __str__(self):
#         return self.game.name
# 
#     def save(self, *args, **kwargs):
#         if self.stop and not self.duration:
#             self.duration = self.stop - self.start
#         super().save(*args, **kwargs)
# 
#     def stop_session(self):
#         self.stop = timezone.now()
#         self.save()
# 
# Path: bot/plugins/stats/resources.py
# class GamesPlayedResource(resources.Resource):
# 
#     game = fields.Field(attribute='game__name', column_name='game')
#     time = fields.Field(attribute='time', column_name='time (hours)')
#     num_players = fields.Field(attribute='num_players', column_name='num_players')
# 
#     class Meta:
#         export_order = ['game', 'time', 'num_players']
# 
#     def dehydrate_game(self, obj):
#         return obj['game__name']
# 
#     def dehydrate_time(self, obj):
#         return obj['time'].total_seconds() / 3600
# 
#     def dehydrate_num_players(self, obj):
#         return obj['num_players']
# 
# Path: bot/plugins/stats/tests/factories.py
# class GameSessionFactory(factory.django.DjangoModelFactory):
# 
#     member = factory.SubFactory('bot.users.tests.factories.MemberFactory')
#     game = factory.SubFactory('bot.games.tests.factories.GameFactory')
# 
#     start = factory.fuzzy.FuzzyDateTime(
#         start_dt=timezone.now() - timedelta(hours=1),
#         end_dt=timezone.now() - timedelta(minutes=30)
#     )
#     stop = factory.fuzzy.FuzzyDateTime(start_dt=timezone.now() - timedelta(minutes=30))
#     duration = factory.LazyAttribute(lambda o: o.stop - o.start)
# 
#     class Meta:
#         model = 'stats.GameSession'


from datetime import timedelta
from django.test import TestCase
from ..models import GameSession
from ..resources import GamesPlayedResource
from .factories import GameSessionFactory




class ExportTests(TestCase):

    def test_export_correctly(self):
        GameSessionFactory.create(game__name='Overwatch', duration=timedelta(hours=1))
        GameSessionFactory.create(game__name='Overwatch', duration=timedelta(minutes=30))
        GameSessionFactory.create(game__name='Battlefield 4', duration=timedelta(minutes=30))

# Path: bot/plugins/stats/models.py
# class GameSession(models.Model):
#     member = models.ForeignKey('users.Member', on_delete=models.PROTECT)
#     server = models.BigIntegerField(_("server"))
#     game = models.ForeignKey('games.Game', on_delete=models.CASCADE)
#     start = models.DateTimeField(_('start'))
#     stop = models.DateTimeField(_('stop'), null=True, blank=True)
#     duration = models.DurationField(null=True, blank=True)
# 
#     objects = GameSessionQuerySet.as_manager()
# 
#     class Meta:
#         verbose_name = _('game session')
#         verbose_name_plural = _('game sessions')
# 
#     def __str__(self):
#         return self.game.name
# 
#     def save(self, *args, **kwargs):
#         if self.stop and not self.duration:
#             self.duration = self.stop - self.start
#         super().save(*args, **kwargs)
# 
#     def stop_session(self):
#         self.stop = timezone.now()
#         self.save()
# 
# Path: bot/plugins/stats/resources.py
# class GamesPlayedResource(resources.Resource):
# 
#     game = fields.Field(attribute='game__name', column_name='game')
#     time = fields.Field(attribute='time', column_name='time (hours)')
#     num_players = fields.Field(attribute='num_players', column_name='num_players')
# 
#     class Meta:
#         export_order = ['game', 'time', 'num_players']
# 
#     def dehydrate_game(self, obj):
#         return obj['game__name']
# 
#     def dehydrate_time(self, obj):
#         return obj['time'].total_seconds() / 3600
# 
#     def dehydrate_num_players(self, obj):
#         return obj['num_players']
# 
# Path: bot/plugins/stats/tests/factories.py
# class GameSessionFactory(factory.django.DjangoModelFactory):
# 
#     member = factory.SubFactory('bot.users.tests.factories.MemberFactory')
#     game = factory.SubFactory('bot.games.tests.factories.GameFactory')
# 
#     start = factory.fuzzy.FuzzyDateTime(
#         start_dt=timezone.now() - timedelta(hours=1),
#         end_dt=timezone.now() - timedelta(minutes=30)
#     )
#     stop = factory.fuzzy.FuzzyDateTime(start_dt=timezone.now() - timedelta(minutes=30))
#     duration = factory.LazyAttribute(lambda o: o.stop - o.start)
# 
#     class Meta:
#         model = 'stats.GameSession'


# Path: bot/plugins/stats/tests/test_game_stats_export.py
from datetime import timedelta
from django.test import TestCase
from ..models import GameSession
from ..resources import GamesPlayedResource
from .factories import GameSessionFactory





class ExportTests(TestCase):

    def test_export_correctly(self):
        GameSessionFactory.create(game__name='Overwatch', duration=timedelta(hours=1))
        GameSessionFactory.create(game__name='Overwatch', duration=timedelta(minutes=30))
        GameSessionFactory.create(game__name='Battlefield 4', duration=timedelta(minutes=30))

        GameSessionFactory.create(game__name='Battlefield 4', duration=timedelta(minutes=30))