|
| 1 | +use gittype::domain::models::{Challenge, DifficultyLevel}; |
1 | 2 | use gittype::infrastructure::database::daos::ChallengeDao; |
2 | 3 | use gittype::infrastructure::database::database::Database; |
3 | 4 |
|
4 | 5 | #[test] |
5 | | -fn challenge_dao_new_creates_dao() { |
| 6 | +fn test_new_creates_dao() { |
6 | 7 | let db = Database::new().expect("Failed to create database"); |
7 | 8 | let _dao = ChallengeDao::new(&db); |
8 | | - // Test passes if construction succeeds |
| 9 | +} |
| 10 | + |
| 11 | +#[test] |
| 12 | +fn test_ensure_challenge_creates_new_challenge() { |
| 13 | + let db = Database::new().unwrap(); |
| 14 | + db.init().unwrap(); |
| 15 | + let dao = ChallengeDao::new(&db); |
| 16 | + |
| 17 | + let challenge = Challenge::new("test-challenge-1".to_string(), "fn test() {}".to_string()) |
| 18 | + .with_language("rust".to_string()) |
| 19 | + .with_source_info("src/test.rs".to_string(), 1, 10) |
| 20 | + .with_difficulty_level(DifficultyLevel::Easy); |
| 21 | + |
| 22 | + let conn = db.get_connection(); |
| 23 | + let tx = conn.unchecked_transaction().unwrap(); |
| 24 | + |
| 25 | + let rowid = dao |
| 26 | + .ensure_challenge_in_transaction(&tx, &challenge) |
| 27 | + .unwrap(); |
| 28 | + tx.commit().unwrap(); |
| 29 | + |
| 30 | + assert!(rowid > 0, "Should return positive rowid"); |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn test_ensure_challenge_returns_existing_challenge() { |
| 35 | + let db = Database::new().unwrap(); |
| 36 | + db.init().unwrap(); |
| 37 | + let dao = ChallengeDao::new(&db); |
| 38 | + |
| 39 | + let challenge = Challenge::new("test-challenge-2".to_string(), "fn test() {}".to_string()) |
| 40 | + .with_language("rust".to_string()); |
| 41 | + |
| 42 | + // Insert first time |
| 43 | + let conn = db.get_connection(); |
| 44 | + let tx1 = conn.unchecked_transaction().unwrap(); |
| 45 | + let rowid1 = dao |
| 46 | + .ensure_challenge_in_transaction(&tx1, &challenge) |
| 47 | + .unwrap(); |
| 48 | + tx1.commit().unwrap(); |
| 49 | + |
| 50 | + // Insert second time - should return same rowid |
| 51 | + let tx2 = conn.unchecked_transaction().unwrap(); |
| 52 | + let rowid2 = dao |
| 53 | + .ensure_challenge_in_transaction(&tx2, &challenge) |
| 54 | + .unwrap(); |
| 55 | + tx2.commit().unwrap(); |
| 56 | + |
| 57 | + assert_eq!( |
| 58 | + rowid1, rowid2, |
| 59 | + "Should return same rowid for existing challenge" |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn test_ensure_challenge_with_different_difficulties() { |
| 65 | + let db = Database::new().unwrap(); |
| 66 | + db.init().unwrap(); |
| 67 | + let dao = ChallengeDao::new(&db); |
| 68 | + |
| 69 | + let difficulties = vec![ |
| 70 | + DifficultyLevel::Easy, |
| 71 | + DifficultyLevel::Normal, |
| 72 | + DifficultyLevel::Hard, |
| 73 | + ]; |
| 74 | + |
| 75 | + let conn = db.get_connection(); |
| 76 | + for (i, difficulty) in difficulties.iter().enumerate() { |
| 77 | + let challenge = Challenge::new( |
| 78 | + format!("test-challenge-diff-{}", i), |
| 79 | + format!("fn test_{}() {{}}", i), |
| 80 | + ) |
| 81 | + .with_difficulty_level(difficulty.clone()); |
| 82 | + |
| 83 | + let tx = conn.unchecked_transaction().unwrap(); |
| 84 | + let rowid = dao |
| 85 | + .ensure_challenge_in_transaction(&tx, &challenge) |
| 86 | + .unwrap(); |
| 87 | + tx.commit().unwrap(); |
| 88 | + |
| 89 | + assert!( |
| 90 | + rowid > 0, |
| 91 | + "Should create challenge with difficulty {:?}", |
| 92 | + difficulty |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn test_ensure_challenge_with_comment_ranges() { |
| 99 | + let db = Database::new().unwrap(); |
| 100 | + db.init().unwrap(); |
| 101 | + let dao = ChallengeDao::new(&db); |
| 102 | + |
| 103 | + let challenge = Challenge::new( |
| 104 | + "test-challenge-comments".to_string(), |
| 105 | + "// comment\nfn test() {}".to_string(), |
| 106 | + ) |
| 107 | + .with_comment_ranges(vec![(0, 10)]); |
| 108 | + |
| 109 | + let conn = db.get_connection(); |
| 110 | + let tx = conn.unchecked_transaction().unwrap(); |
| 111 | + let rowid = dao |
| 112 | + .ensure_challenge_in_transaction(&tx, &challenge) |
| 113 | + .unwrap(); |
| 114 | + tx.commit().unwrap(); |
| 115 | + |
| 116 | + assert!(rowid > 0, "Should create challenge with comment ranges"); |
| 117 | +} |
| 118 | + |
| 119 | +#[test] |
| 120 | +fn test_ensure_multiple_challenges_in_transaction() { |
| 121 | + let db = Database::new().unwrap(); |
| 122 | + db.init().unwrap(); |
| 123 | + let dao = ChallengeDao::new(&db); |
| 124 | + |
| 125 | + let challenges: Vec<Challenge> = (0..5) |
| 126 | + .map(|i| { |
| 127 | + Challenge::new( |
| 128 | + format!("multi-challenge-{}", i), |
| 129 | + format!("fn test_{}() {{}}", i), |
| 130 | + ) |
| 131 | + .with_language("rust".to_string()) |
| 132 | + }) |
| 133 | + .collect(); |
| 134 | + |
| 135 | + let conn = db.get_connection(); |
| 136 | + let tx = conn.unchecked_transaction().unwrap(); |
| 137 | + |
| 138 | + let mut rowids = Vec::new(); |
| 139 | + for challenge in &challenges { |
| 140 | + let rowid = dao.ensure_challenge_in_transaction(&tx, challenge).unwrap(); |
| 141 | + rowids.push(rowid); |
| 142 | + } |
| 143 | + |
| 144 | + tx.commit().unwrap(); |
| 145 | + |
| 146 | + // All rowids should be unique and positive |
| 147 | + assert_eq!(rowids.len(), 5, "Should insert 5 challenges"); |
| 148 | + for rowid in &rowids { |
| 149 | + assert!(*rowid > 0, "Rowid should be positive"); |
| 150 | + } |
| 151 | + |
| 152 | + // Check uniqueness |
| 153 | + let unique_rowids: std::collections::HashSet<_> = rowids.iter().collect(); |
| 154 | + assert_eq!(unique_rowids.len(), 5, "All rowids should be unique"); |
9 | 155 | } |
0 commit comments