66from lametro .search_indexes import LAMetroBillIndex
77
88
9+ # Helper function
10+ def patch_aa (mocker , return_value ):
11+ mocker .patch (
12+ "lametro.models.LAMetroBill.actions_and_agendas" ,
13+ new_callable = mocker .PropertyMock ,
14+ return_value = return_value ,
15+ )
16+
17+
918@pytest .mark .parametrize ("month" , [6 , 7 ])
1019def test_legislative_session (bill , metro_organization , event , mocker , month ):
1120 """
@@ -16,11 +25,12 @@ def test_legislative_session(bill, metro_organization, event, mocker, month):
1625 which returns a dict of prepped data.
1726 https://github.com/django-haystack/django-haystack/blob/4910ccb01c31d12bf22dcb000894eece6c26f74b/haystack/indexes.py#L198
1827 """
28+ # Set up
29+ now = datetime .now ()
1930 org = metro_organization .build ()
2031 event = event .build ()
2132 bill = bill .build ()
22-
23- now = datetime .now ()
33+ index = LAMetroBillIndex ()
2434
2535 # Create test actions and agendas
2636 recent_action = {
@@ -48,66 +58,62 @@ def test_legislative_session(bill, metro_organization, event, mocker, month):
4858 "organization" : org ,
4959 }
5060
61+ def date_to_fy_string (date ):
62+ """
63+ Return the correct fiscal year for a given date, June (6) being the last month of the year.
64+ """
65+ start_year = date .year - 1 if date .month <= 6 else date .year
66+ end_year = date .year if date .month <= 6 else date .year + 1
67+ return "7/1/{0} to 6/30/{1}" .format (start_year , end_year )
68+
5169 # Test indexed value when there are both actions and agendas
52- mock_actions_and_agendas = mocker .PropertyMock (
53- return_value = [recent_action , older_action , recent_agenda , older_agenda ]
54- )
5570
56- mocker .patch (
57- "lametro.models.LAMetroBill.actions_and_agendas" ,
58- new_callable = mock_actions_and_agendas ,
59- )
71+ patch_aa (mocker , [recent_action , older_action , recent_agenda , older_agenda ])
72+ indexed_data = index .prepare (bill )
73+ expected_value = date_to_fy_string (recent_agenda ["date" ])
6074
61- index = LAMetroBillIndex ()
62- expected_fmt = "7/1/{0} to 6/30/{1}"
75+ assert indexed_data ["legislative_session" ] == expected_value
6376
64- indexed_data = index . prepare ( bill )
77+ # Test indexed value when there are just actions
6578
66- if month <= 6 :
67- expected_value = expected_fmt .format (
68- recent_agenda ["date" ].year - 1 , recent_agenda ["date" ].year
69- )
70- else :
71- expected_value = expected_fmt .format (
72- recent_agenda ["date" ].year , recent_agenda ["date" ].year + 1
73- )
79+ patch_aa (mocker , [recent_action , older_action ])
80+ indexed_data = index .prepare (bill )
81+ expected_value = date_to_fy_string (recent_action ["date" ])
7482
7583 assert indexed_data ["legislative_session" ] == expected_value
7684
77- # Test indexed value when there are just actions
78- mock_actions_and_agendas = mocker .PropertyMock (
79- return_value = [recent_action , older_action ]
80- )
8185
82- mocker .patch (
83- "lametro.models.LAMetroBill.actions_and_agendas" ,
84- new_callable = mock_actions_and_agendas ,
85- )
86+ def test_legislative_session_fallback (bill , metro_organization , event , mocker ):
87+ bill = bill .build ()
88+ event = event .build ()
89+ org = metro_organization .build ()
90+ session = bill .legislative_session
91+ index = LAMetroBillIndex ()
92+ now = datetime .now ()
8693
87- indexed_data = index .prepare (bill )
94+ single_action = {
95+ "date" : datetime (now .year , 7 , 1 ),
96+ "description" : "action descripton" ,
97+ "event" : event ,
98+ "organization" : org ,
99+ }
100+
101+ patch_aa (mocker , [single_action ])
88102
89- if month <= 6 :
90- expected_value = expected_fmt .format (
91- recent_action ["date" ].year - 1 , recent_action ["date" ].year
92- )
93- else :
94- expected_value = expected_fmt .format (
95- recent_action ["date" ].year , recent_action ["date" ].year + 1
96- )
103+ indexed_data = index .prepare (bill )
104+ expected_fmt = "7/1/{0} to 6/30/{1}"
105+ expected_value = expected_fmt .format (
106+ session .start_date .split ("-" )[0 ], session .end_date .split ("-" )[0 ]
107+ )
97108
98109 assert indexed_data ["legislative_session" ] == expected_value
99110
100111 # Test indexed value when there are neither actions nor agendas
101- mock_actions_and_agendas = mocker .PropertyMock (return_value = [])
102-
103- mocker .patch (
104- "lametro.models.LAMetroBill.actions_and_agendas" ,
105- new_callable = mock_actions_and_agendas ,
106- )
107112
113+ patch_aa (mocker , [])
108114 indexed_data = index .prepare (bill )
109115
110- assert not indexed_data ["legislative_session" ]
116+ assert indexed_data ["legislative_session" ] == expected_value
111117
112118
113119def test_sponsorships (bill , metro_organization , event , event_related_entity , mocker ):
0 commit comments