from django.test import TestCase
from players.models import Player, PlayerStats
from players.utils import update_player_stats

class PlayerStatsTestCase(TestCase):
    def setUp(self):
        self.player = Player.objects.create(player_id='1', first_name='John', last_name='Doe')
        self.stats = {
            'season_year': 2023,
            'week': 1,
            'pass_yards': 300,
            'pass_tds': 2,
            'rush_yards': 50,
            'rush_tds': 1,
            'receiving_yards': 100,
            'receiving_tds': 1,
            'receptions': 6,
            'fumbles': 0,
            'total_points': 35.0,
        }

    def test_update_player_stats(self):
        # Call the update_player_stats function with the test data
        update_player_stats(self.player.player_id, self.stats)

        # Retrieve the PlayerStats instance that should have been created/updated
        player_stats = PlayerStats.objects.get(player=self.player, season_year=self.stats['season_year'], week=self.stats['week'])

        # Assert that the PlayerStats instance was created/updated correctly
        self.assertEqual(player_stats.player, self.player)
        self.assertEqual(player_stats.season_year, self.stats['season_year'])
        self.assertEqual(player_stats.week, self.stats['week'])
        self.assertEqual(player_stats.pass_yards, self.stats['pass_yards'])
        self.assertEqual(player_stats.pass_tds, self.stats['pass_tds'])
        self.assertEqual(player_stats.rush_yards, self.stats['rush_yards'])
        self.assertEqual(player_stats.rush_tds, self.stats['rush_tds'])
        self.assertEqual(player_stats.receiving_yards, self.stats['receiving_yards'])
        self.assertEqual(player_stats.receiving_tds, self.stats['receiving_tds'])
        self.assertEqual(player_stats.receptions, self.stats['receptions'])
        self.assertEqual(player_stats.fumbles, self.stats['fumbles'])
        self.assertEqual(player_stats.total_points, self.stats['total_points'])
