Spaces:
Sleeping
Sleeping
File size: 6,751 Bytes
b996603 815d7a1 b996603 815d7a1 b996603 815d7a1 b996603 815d7a1 b996603 815d7a1 b996603 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | """
Test data loader for SSC
Run this script to populate the database with sample data for testing
"""
import os
import sys
from datetime import datetime, timedelta
# Add parent directory to path
sys.path.insert(0, os.path.dirname(__file__))
from sqlalchemy.orm import Session
from app.database import SessionLocal, init_db
from app.models import User, PerformanceLog
from app.utils.auth import hash_password
def load_test_data():
"""Load sample data into the database"""
# Initialize database
init_db()
db = SessionLocal()
try:
print("π Loading test data...")
# Create sample players
players = [
{
"name": "Virat Kohli",
"email": "virat@ssc.com",
"password": hash_password("password123"),
"jersey_number": 18,
"bio": "Indian cricket legend",
"runs": 12000,
"matches": 200,
"wickets": 0,
"centuries": 45,
"half_centuries": 50,
"highest_score": 183,
},
{
"name": "Jasprit Bumrah",
"email": "bumrah@ssc.com",
"password": hash_password("password123"),
"jersey_number": 93,
"bio": "Fast bowler extraordinaire",
"runs": 500,
"matches": 120,
"wickets": 150,
"centuries": 0,
"half_centuries": 0,
"highest_score": 34,
},
{
"name": "Rohit Sharma",
"email": "rohit@ssc.com",
"password": hash_password("password123"),
"jersey_number": 45,
"bio": "Opening batsman",
"runs": 9000,
"matches": 150,
"wickets": 5,
"centuries": 31,
"half_centuries": 40,
"highest_score": 264,
"is_premium": True,
"premium_start_date": datetime.utcnow(),
"premium_expiry": datetime.utcnow() + timedelta(days=30),
},
{
"name": "MS Dhoni",
"email": "dhoni@ssc.com",
"password": hash_password("password123"),
"jersey_number": 7,
"bio": "Captain cool",
"runs": 10000,
"matches": 180,
"wickets": 0,
"centuries": 10,
"half_centuries": 90,
"highest_score": 224,
},
{
"name": "Admin User",
"email": "admin@ssc.com",
"password": hash_password("admin123"),
"jersey_number": None,
"bio": "System administrator",
"role": "admin",
"runs": 0,
"matches": 0,
}
]
# Add 20 premium dummy players (mix of batsman and bowler)
premium_now = datetime.utcnow()
for i in range(1, 21):
is_batsman = i <= 10
players.append(
{
"name": f"Premium Player {i:02d}",
"email": f"premium{i:02d}@ssc.com",
"password": hash_password("password123"),
"jersey_number": 100 + i,
"bio": "Premium Batsman" if is_batsman else "Premium Bowler",
"runs": 1200 + (i * 45) if is_batsman else 320 + (i * 15),
"matches": 30 + i,
"wickets": 8 + (i % 6) if is_batsman else 55 + (i * 3),
"centuries": 2 + (i % 4) if is_batsman else 0,
"half_centuries": 6 + (i % 8) if is_batsman else 1 + (i % 3),
"highest_score": 95 + (i * 4) if is_batsman else 42 + (i % 10),
"is_premium": True,
"premium_start_date": premium_now,
"premium_expiry": premium_now + timedelta(days=30),
}
)
# Add 10 regular guest dummy players
for i in range(1, 11):
players.append(
{
"name": f"Guest Player {i:02d}",
"email": f"guest{i:02d}@ssc.com",
"password": hash_password("password123"),
"jersey_number": 200 + i,
"bio": "Guest Regular Player",
"runs": 140 + (i * 25),
"matches": 8 + i,
"wickets": 1 + (i % 4),
"centuries": 0,
"half_centuries": 0,
"highest_score": 28 + (i * 3),
"is_premium": False,
}
)
existing_emails = {
row[0] for row in db.query(User.email).all()
}
inserted_count = 0
for player_data in players:
if player_data["email"] in existing_emails:
continue
player = User(**player_data)
db.add(player)
inserted_count += 1
db.commit()
print(f"β Added {inserted_count} users (skipped existing emails)")
# Add sample performance logs
players = db.query(User).filter(User.role == "player").all()
for player in players[:3]: # Add logs for first 3 players
for i in range(5):
perf_log = PerformanceLog(
user_id=player.id,
match_date=datetime.utcnow() - timedelta(days=i*7),
runs_scored=45 + (i * 10),
wickets_taken=i % 3,
match_type="league",
opponent=f"Team {chr(65 + i)}",
performance_rating=7.5 + (i * 0.3),
notes=f"Good match {i+1}"
)
db.add(perf_log)
db.commit()
print("β Created sample performance logs")
print("\nβ
Test data loaded successfully!")
print("\nTest Accounts:")
print(" π± Regular Player")
print(" Email: virat@ssc.com")
print(" Password: password123")
print("\n π Premium Player")
print(" Email: rohit@ssc.com")
print(" Password: password123")
print("\n π Admin")
print(" Email: admin@ssc.com")
print(" Password: admin123")
except Exception as e:
db.rollback()
print(f"β Error loading test data: {e}")
finally:
db.close()
if __name__ == "__main__":
load_test_data()
|