headless_lms_models/credit_registrations/
testing.rs1use super::claims::RegistrationScope;
5use crate::prelude::*;
6use chrono::TimeDelta;
7
8pub async fn set_state_entered_at_for_testing(
14 conn: &mut PgConnection,
15 id: Uuid,
16 state_entered_at: DateTime<Utc>,
17) -> ModelResult<()> {
18 sqlx::query!(
19 "
20UPDATE credit_registrations
21SET state_entered_at = $2
22WHERE id = $1
23 ",
24 id,
25 state_entered_at,
26 )
27 .execute(conn)
28 .await?;
29 Ok(())
30}
31
32pub async fn set_first_failed_at_for_testing(
38 conn: &mut PgConnection,
39 id: Uuid,
40 first_failed_at: DateTime<Utc>,
41) -> ModelResult<()> {
42 sqlx::query!(
43 "
44UPDATE credit_registrations
45SET first_failed_at = $2
46WHERE id = $1
47 ",
48 id,
49 first_failed_at,
50 )
51 .execute(conn)
52 .await?;
53 Ok(())
54}
55
56pub async fn set_test_exclusive_hold_for_testing(
66 conn: &mut PgConnection,
67 user_id: Uuid,
68 course_id: Option<Uuid>,
69 held_until: DateTime<Utc>,
70) -> ModelResult<()> {
71 sqlx::query!(
72 "
73INSERT INTO credit_registration_test_exclusive_holds (user_id, course_id, held_until)
74VALUES ($1, $2, $3)
75 ",
76 user_id,
77 course_id,
78 held_until,
79 )
80 .execute(conn)
81 .await?;
82 Ok(())
83}
84
85pub async fn expire_enrolment_recheck_allowance_for_testing(
90 conn: &mut PgConnection,
91 id: Uuid,
92 clear_restarts: bool,
93) -> ModelResult<()> {
94 use crate::library::credit_registration::enrolment_check_schedule::{
95 CHECK_REQUEST_MIN_INTERVAL, STUDENT_CHECK_REQUEST_MIN_ROW_AGE,
96 };
97 sqlx::query!(
98 "
99UPDATE credit_registrations
100SET created_at = LEAST(created_at, now() - $4::interval),
101 enrolment_checked_at = enrolment_checked_at - $2::interval,
102 enrolment_check_requested_at = enrolment_check_requested_at - $2::interval,
103 enrolment_check_restart_count = CASE
104 WHEN $3 THEN 0
105 ELSE enrolment_check_restart_count
106 END
107WHERE id = $1
108 ",
109 id,
110 CHECK_REQUEST_MIN_INTERVAL as TimeDelta,
111 clear_restarts,
112 STUDENT_CHECK_REQUEST_MIN_ROW_AGE as TimeDelta,
113 )
114 .execute(conn)
115 .await?;
116 Ok(())
117}
118
119pub async fn make_enrolment_checks_due_for_testing(
123 conn: &mut PgConnection,
124 scope: &RegistrationScope,
125) -> ModelResult<u64> {
126 let res = sqlx::query!(
127 r#"
128UPDATE credit_registrations
129SET next_attempt_at = now()
130WHERE state = 'no_usable_enrolment'
131 AND next_attempt_at > now()
132 AND deleted_at IS NULL
133 AND ($1::uuid IS NULL OR course_id = $1)
134 AND ($2::uuid IS NULL OR user_id = $2)
135 AND (
136 cardinality($3::uuid []) = 0
137 OR id = ANY($3::uuid [])
138 )
139 "#,
140 scope.course_id,
141 scope.user_id,
142 &scope.credit_registration_ids,
143 )
144 .execute(conn)
145 .await?;
146 Ok(res.rows_affected())
147}