1- from django .test import TestCase
1+ from django .test import TestCase , tag
22from datetime import datetime
3- from dateutil .relativedelta import relativedelta
43from django .utils import timezone
54
65from django .core .exceptions import ValidationError
98from ...factories .response_set_factory import ResponseSetFactory
109from ....models .user import User
1110from ....models .response_set import ResponseSet
11+ from ....models .family_history_lung_cancer_response import FamilyHistoryLungCancerValues
1212
13+
14+ @tag ("ResponseSet" )
1315class TestResponseSet (TestCase ):
1416 def setUp (self ):
1517 self .user = UserFactory ()
@@ -67,20 +69,38 @@ def test_is_invalid_if_another_unsubmitted_response_set_exists(self):
6769 )
6870
6971 def test_is_invalid_if_another_response_set_was_submitted_within_the_recently_submitted_period (self ):
70- user = UserFactory ()
71- user .responseset_set .create (
72- submitted_at = timezone .now () - relativedelta (days = ResponseSet .RECENTLY_SUBMITTED_PERIOD_DAYS - 1 )
73- )
72+ response_set = ResponseSetFactory .create (recently_submitted = True )
7473
7574 with self .assertRaises (ValidationError ) as context :
76- user .responseset_set .create ()
75+ response_set . user .responseset_set .create ()
7776
7877 self .assertEqual (
7978 context .exception .messages [0 ],
8079 "Responses have already been submitted for this user"
8180 )
8281
83- # Query managers
82+
83+ def test_Saving_a_submitted_response_Set_does_not_included_itself_in_unsubmitted_response_sets_validation (self ):
84+ response_set = ResponseSetFactory .create (complete = True )
85+ response_set .submitted_at = timezone .now ()
86+
87+ response_set .full_clean ()
88+
89+
90+ def test_submitted_response_set_is_invalid_if_incomplete (self ):
91+ self .response_set .submitted_at = timezone .now ()
92+
93+ with self .assertRaises (ValidationError ) as context :
94+ self .response_set .full_clean ()
95+
96+ self .assertEqual (
97+ context .exception .messages [0 ],
98+ "Response set must be complete before it can be submitted"
99+ )
100+
101+
102+ # Query managers
103+
84104 def test_objects_returns_all_response_sets (self ):
85105 unsubmitted_response_set = ResponseSetFactory ()
86106 submitted_response_set = ResponseSetFactory (submitted_at = timezone .now ())
@@ -95,9 +115,10 @@ def test_objects_returns_all_response_sets(self):
95115 response_sets ,
96116 )
97117
118+
98119 def test_unsubmitted_returns_only_unsubmitted_response_sets (self ):
99120 unsubmitted_response_set = ResponseSetFactory ()
100- submitted_response_set = ResponseSetFactory (submitted_at = timezone . now () )
121+ submitted_response_set = ResponseSetFactory (recently_submitted = True )
101122
102123 unsubmitted_response_sets = ResponseSet .objects .unsubmitted ().all ()
103124 self .assertIn (
@@ -111,7 +132,7 @@ def test_unsubmitted_returns_only_unsubmitted_response_sets(self):
111132
112133 def test_submitted_returns_only_submitted_response_sets (self ):
113134 unsubmitted_response_set = ResponseSetFactory ()
114- submitted_response_set = ResponseSetFactory (submitted_at = timezone . now () - relativedelta ( years = 1 ) )
135+ submitted_response_set = ResponseSetFactory (not_recently_submitted = True )
115136
116137 submitted_response_sets = ResponseSet .objects .submitted ().all ()
117138 self .assertIn (
@@ -125,14 +146,10 @@ def test_submitted_returns_only_submitted_response_sets(self):
125146
126147 def test_submitted_recently_returns_only_submitted_response_sets_in_the_recently_submitted_period (self ):
127148 recently_submitted_response = ResponseSetFactory (
128- submitted_at = timezone .now () - relativedelta (
129- days = ResponseSet .RECENTLY_SUBMITTED_PERIOD_DAYS - 1
130- )
149+ recently_submitted = True
131150 )
132151 old_submitted_response = ResponseSetFactory (
133- submitted_at = timezone .now () - relativedelta (
134- days = ResponseSet .RECENTLY_SUBMITTED_PERIOD_DAYS + 1
135- )
152+ not_recently_submitted = True
136153 )
137154
138155 recently_submitted_response_sets = ResponseSet .objects .recently_submitted ().all ()
@@ -144,3 +161,29 @@ def test_submitted_recently_returns_only_submitted_response_sets_in_the_recently
144161 old_submitted_response ,
145162 recently_submitted_response_sets ,
146163 )
164+
165+
166+ def test_is_complete_returns_true_if_all_questions_are_answered (self ):
167+ response_set = ResponseSetFactory .create (complete = True )
168+
169+ self .assertTrue (response_set .is_complete ())
170+
171+
172+ def test_is_complete_returns_false_if_a_single_question_is_not_answered (self ):
173+ response_set = ResponseSetFactory .create (complete = True )
174+ response_set .asbestos_exposure_response .delete ()
175+ response_set .refresh_from_db ()
176+
177+ self .assertFalse (response_set .is_complete ())
178+
179+
180+ def test_is_complete_returns_true_if_family_history_cancer_no_and_none_age_diagnosed (self ):
181+ response_set = ResponseSetFactory .create (complete = True )
182+
183+ family_history = response_set .family_history_lung_cancer
184+ family_history .value = FamilyHistoryLungCancerValues .NO
185+ family_history .save ()
186+
187+ response_set .refresh_from_db ()
188+
189+ self .assertTrue (response_set .is_complete ())
0 commit comments