forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

offering_subject.rs82 lines, 3.4 KiBHistoryRawDownload
1//! The syllabus topic sets: which of a catalog course's subjects an offering
2//! ([`crate::domain::course_offering::CourseOffering`]) selects for its grade,
3//! and which one a class section ([`crate::domain::class_course::ClassCourse`])
4//! selects for itself. Both are pure membership junctions over `subject` —
5//! there is no row type of their own, so this file carries the model's one
6//! pure rule and the docs.
7//!
8//! The rule: a subject may be selected only for an offering of the course the
9//! subject belongs to. "Lifted" from the course to the instance level, the
10//! same gate guards the class-side selections, because an instance's course
11//! is fixed at attach.
12//!
13//! Resolution is override-or-inherit, never merge, and is **flag-driven**:
14//! [`ClassCourse::subjects_inherited`](crate::domain::class_course::ClassCourse::subjects_inherited)
15//! `TRUE` = the offering's set applies, `FALSE` = the section's own
16//! `class_course_subject` rows are authoritative **including when there are
17//! none** (that is how a section clears its syllabus). The flag flips in the
18//! same statement that writes or deletes a class-side row
19//! ([`crate::db::offering_subject`]); the effective set is
20//! [`crate::service::offering_subject::resolved_for_instance`].
21
22use crate::domain::course::CourseId;
23use crate::domain::subject::Subject;
24use crate::error::ValidationError;
25
26/// The membership precondition: `subject` may be selected for a set whose
27/// course is `offering_course` only when it belongs to that course. The same
28/// 400 the bare-course tag check (`service::subject::in_course`) has always
29/// answered — one condition, one error shape, now enforced at selection time
30/// as well as tag time.
31pub fn ensure_course_member(
32 subject: &Subject,
33 offering_course: &CourseId,
34) -> Result<(), ValidationError> {
35 if subject.get_course() != offering_course {
36 return Err(ValidationError::Invalid {
37 field: "subject_id",
38 reason: "subject belongs to a different course",
39 });
40 }
41 Ok(())
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use crate::domain::subject::{SubjectDescription, SubjectId, SubjectName};
48
49 fn a_subject(course: CourseId) -> Subject {
50 Subject {
51 id: SubjectId::generate(),
52 course,
53 name: SubjectName::try_new("Limits").unwrap(),
54 description: SubjectDescription::try_new("").unwrap(),
55 }
56 }
57
58 /// The precondition refuses a subject of another course, naming the field
59 /// with the exact reason the tag-time check uses — drops the guard and
60 /// this fails on the `Ok`.
61 #[test]
62 fn a_foreign_course_subject_is_refused() {
63 let subject = a_subject(CourseId::from_key(
64 "019732e3-7b00-7000-8000-00000000dead",
65 ));
66 let offering_course = CourseId::from_key("019732e3-7b00-7000-8000-00000000beef");
67 let refused = ensure_course_member(&subject, &offering_course).unwrap_err();
68 assert_eq!(
69 refused.to_string(),
70 "subject_id: subject belongs to a different course"
71 );
72 }
73
74 /// A subject of the offering's own course passes — negates the guard's
75 /// "always refuse" mutation.
76 #[test]
77 fn an_own_course_subject_passes() {
78 let course = CourseId::from_key("019732e3-7b00-7000-8000-00000000dead");
79 let subject = a_subject(course.clone());
80 ensure_course_member(&subject, &course).unwrap();
81 }
82}