|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import django |
| 5 | + |
| 6 | + |
| 7 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pennmobile.settings") |
| 8 | +django.setup() |
| 9 | + |
| 10 | +from django.contrib.auth import get_user_model # noqa: E402 |
| 11 | +from rest_framework.test import APIClient # noqa: E402 |
| 12 | + |
| 13 | +from games.generator import generate_good_game # noqa: E402 |
| 14 | +from games.models import Game, LeaderboardEntry # noqa: E402 |
| 15 | + |
| 16 | + |
| 17 | +User = get_user_model() |
| 18 | +client = APIClient() |
| 19 | + |
| 20 | +print("\n" + "=" * 50) |
| 21 | + |
| 22 | +# TASK 1 ---------------------------------------------------------------------- |
| 23 | +print("--- Testing Task 1: Generate Games & Store in DB ---") |
| 24 | +try: |
| 25 | + game_board, seed, sols = generate_good_game( |
| 26 | + min_total_words=50 |
| 27 | + ) # Using a smaller number so it tests quickly |
| 28 | + print("✅ Game generation script successful! Seed word generated:", seed) |
| 29 | + |
| 30 | + game = Game.get_today() |
| 31 | + if game: |
| 32 | + print(f"✅ Django DB Game model successfully loaded for {game.date}.") |
| 33 | + print(" Board format:", type(game.board), "->", game.board[0], "...") |
| 34 | + else: |
| 35 | + print("❌ No game for today found. Ensure you ran 'python manage.py generate_game'") |
| 36 | +except Exception as e: |
| 37 | + print("❌ Generation/Model script failed:", str(e)) |
| 38 | + |
| 39 | + |
| 40 | +# TASK 2 ---------------------------------------------------------------------- |
| 41 | +print("\n--- Testing Task 2: Models for storing game results (User+Day) ---") |
| 42 | +user, _ = User.objects.get_or_create(username="testuser") |
| 43 | +dummy_user, _ = User.objects.get_or_create(username="dummyuser2") |
| 44 | + |
| 45 | +try: |
| 46 | + entry, created = LeaderboardEntry.objects.get_or_create( |
| 47 | + game=game, user=dummy_user, defaults={"score": 100, "num_words_found": 5} |
| 48 | + ) |
| 49 | + print( |
| 50 | + f"✅ LeaderboardEntry model works for {dummy_user.username}. " |
| 51 | + f"Associated with Game {game.date}." |
| 52 | + ) |
| 53 | + print(f"✅ Relational lookup successful: game.scores.count() = {game.scores.count()}") |
| 54 | + |
| 55 | +except Exception as e: |
| 56 | + print("❌ Leaderboard model creation failed:", str(e)) |
| 57 | + |
| 58 | + |
| 59 | +client.force_authenticate(user=user) |
| 60 | + |
| 61 | +# TASK 3 ---------------------------------------------------------------------- |
| 62 | +print("\n--- Testing Task 3: Fetch Game Route ---") |
| 63 | +response = client.get("/games/word-hunt/today/", format="json") |
| 64 | +print(f"✅ Route Status Code: {response.status_code}") |
| 65 | +print("✅ JSON Data Payload Extract:") |
| 66 | +print(json.dumps(response.data, indent=2)[:200] + "...\n") |
| 67 | + |
| 68 | + |
| 69 | +# TASK 4 ---------------------------------------------------------------------- |
| 70 | +print("--- Testing Task 4: Submit Score Route (valid words) ---") |
| 71 | +valid_words = game.possible_words[:3] if game and game.possible_words else [] |
| 72 | +data = {"words": valid_words} |
| 73 | +response = client.post(f"/games/word-hunt/{game.date}/submit/", data, format="json") |
| 74 | +print(f"✅ Route Status Code: {response.status_code}") |
| 75 | +print("✅ Submit Route Response:") |
| 76 | +print(json.dumps(response.data, indent=2) + "\n") |
| 77 | + |
| 78 | + |
| 79 | +# TASK 5 ---------------------------------------------------------------------- |
| 80 | +print("--- Testing Task 5: Submit Score Route (invalid words) ---") |
| 81 | +data = {"words": [valid_words[0] if valid_words else "cat", "notarealword123"]} |
| 82 | +response2_user, _ = User.objects.get_or_create(username="testuser2") |
| 83 | +client2 = APIClient() |
| 84 | +client2.force_authenticate(user=response2_user) |
| 85 | +response = client2.post(f"/games/word-hunt/{game.date}/submit/", data, format="json") |
| 86 | +print(f"✅ Route Status Code: {response.status_code}") |
| 87 | +print("✅ Validation Error Response:") |
| 88 | +print(json.dumps(response.data, indent=2) + "\n") |
| 89 | + |
| 90 | + |
| 91 | +# TASK 6 ---------------------------------------------------------------------- |
| 92 | +print("--- Testing Task 6: Leaderboard Route ---") |
| 93 | +response = client.get(f"/games/word-hunt/{game.date}/leaderboard/", format="json") |
| 94 | +print(f"✅ Route Status Code: {response.status_code}") |
| 95 | +print("✅ Leaderboard JSON Output (Ranked):") |
| 96 | +print(json.dumps(response.data, indent=2)) |
| 97 | + |
| 98 | +print("=" * 50 + "\n") |
0 commit comments