|
| 1 | +// Test-specific seed script that creates a test user |
| 2 | +// This is based on seed_db.rs but ensures testuser exists with known credentials |
| 3 | +// Run with: cargo run --example seed_db_test |
| 4 | + |
| 5 | +use tiny_racing::database::*; |
| 6 | + |
| 7 | +const TEST_PLAYER: &str = "testuser"; |
| 8 | +const TEST_PASSWORD: &str = "testpass123"; |
| 9 | +const TEST_EMAIL: &str = "testuser@test.com"; |
| 10 | + |
| 11 | +#[tokio::main] |
| 12 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 13 | + // Get database URL from environment or use test default |
| 14 | + let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| { |
| 15 | + "postgresql://tiny_racing_test:test_password@localhost:5433/tiny_racing_test".to_string() |
| 16 | + }); |
| 17 | + |
| 18 | + println!("Connecting to test database..."); |
| 19 | + let db = Database::new(&database_url).await?; |
| 20 | + |
| 21 | + println!("Running migrations..."); |
| 22 | + db.migrate().await?; |
| 23 | + println!("Migrations completed!"); |
| 24 | + |
| 25 | + // Ensure test user exists |
| 26 | + println!("\n=== Ensuring Test User Exists ==="); |
| 27 | + let test_player = match get_player_by_username(db.pool(), TEST_PLAYER).await? { |
| 28 | + Some(player) => { |
| 29 | + println!("Test user '{}' already exists (ID: {})", player.username, player.id); |
| 30 | + player |
| 31 | + } |
| 32 | + None => { |
| 33 | + println!("Creating test user '{}'...", TEST_PLAYER); |
| 34 | + let player = create_player( |
| 35 | + db.pool(), |
| 36 | + CreatePlayerRequest { |
| 37 | + username: TEST_PLAYER.to_string(), |
| 38 | + password: TEST_PASSWORD.to_string(), |
| 39 | + email: Some(TEST_EMAIL.to_string()), |
| 40 | + }, |
| 41 | + ) |
| 42 | + .await?; |
| 43 | + println!("Created test user: {} (ID: {})", player.username, player.id); |
| 44 | + player |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + // Assign team #1 to test user if not already assigned |
| 49 | + println!("\n=== Assigning Team to Test User ==="); |
| 50 | + let team = get_team_by_number(db.pool(), 1).await?; |
| 51 | + if let Some(team) = team { |
| 52 | + if team.player_id.is_none() { |
| 53 | + // Update team to assign to test user |
| 54 | + // Note: You may need to add an update_team function to the database module |
| 55 | + println!("Team #1 ({}) is not assigned to any player", team.name); |
| 56 | + println!("Note: Team assignment may need to be done via API or database update"); |
| 57 | + } else if team.player_id == Some(test_player.id) { |
| 58 | + println!("Team #1 ({}) is already assigned to test user", team.name); |
| 59 | + } else { |
| 60 | + println!("Team #1 ({}) is assigned to a different player", team.name); |
| 61 | + } |
| 62 | + } else { |
| 63 | + println!("Team #1 not found - seed_db.rs should be run first"); |
| 64 | + } |
| 65 | + |
| 66 | + println!("\nTest database setup complete!"); |
| 67 | + println!("Test user credentials:"); |
| 68 | + println!(" Username: {}", TEST_PLAYER); |
| 69 | + println!(" Password: {}", TEST_PASSWORD); |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
0 commit comments