11import pytest
2- import numpy as np
32
43from project_selection import select_proposals_to_fund
54
65
76@pytest .mark .parametrize (
87 "proposals, errmessage" ,
98 [
10- ([("A" , 2 , 0 , 3 )], r"Malformed proposal" ),
119 (
1210 [("A" , 3 , 1 )],
13- r'If proposal "A" were funded it would receive more than the funding limit this year.' ,
11+ r'If proposal "A" were funded it would receive more than '
12+ 'the per-project funding limit this year.' ,
1413 ),
1514 (
1615 [("A" , 2 , 1 )],
17- r'If proposal "A" were funded it would receive more than the funding limit this year.' ,
16+ r'If proposal "A" were funded it would receive more than '
17+ 'the per-project funding limit this year.' ,
1818 ),
1919 ([("A" , 0.5 , 1 ), ("A" , 0.5 , 1 )], r"Proposal names are not unique" ),
2020 ],
2121)
2222def test_select_proposals_wrong_input (proposals , errmessage ):
23- np .random .seed (2025 )
2423 budget = 5
2524 funding_limit = 2
2625 with pytest .raises (ValueError , match = errmessage ):
27- select_proposals_to_fund (budget , funding_limit , proposals )
26+ select_proposals_to_fund (budget , funding_limit , proposals , seed = 2025 )
27+
28+
29+ def test_malformed ():
30+ with pytest .raises (TypeError ):
31+ select_proposals_to_fund (5 , 2 , [("A" , 2 , 0 , 3 )])
2832
2933
3034def test_select_proposals_all_funds (capfd ):
31- np .random .seed (2025 )
3235 budget = 5
3336 funding_limit = 2
3437 proposals = [("A" , 2 , 0 ), ("B" , 1 , 0 ), ("C" , 1 , 0 ), ("D" , 0.5 , 0 ), ("E" , 0.5 , 0 )]
@@ -55,15 +58,17 @@ def test_select_proposals_all_funds(capfd):
5558 'Fund "C" for $1 bringing its project\' s annual total to $1.\n '
5659 'Fund "A" for $2 bringing its project\' s annual total to $2.\n '
5760 )
58- result = select_proposals_to_fund (budget , funding_limit , proposals )
61+ result = select_proposals_to_fund (budget ,
62+ funding_limit ,
63+ proposals ,
64+ seed = 2025 )
5965 captured = capfd .readouterr ()
6066
6167 assert set (result ) == expected_result
6268 assert captured .out == expected_captured
6369
6470
6571def test_select_proposals_more_than_funds (capfd ):
66- np .random .seed (2025 )
6772 budget = 5
6873 funding_limit = 2
6974 proposals = [
@@ -103,15 +108,17 @@ def test_select_proposals_more_than_funds(capfd):
103108 'Fund "B" for $1 bringing its project\' s annual total to $1.\n '
104109 'Fund "A" for $2 bringing its project\' s annual total to $2.\n '
105110 )
106- result = select_proposals_to_fund (budget , funding_limit , proposals )
111+ result = select_proposals_to_fund (budget ,
112+ funding_limit ,
113+ proposals ,
114+ seed = 2025 )
107115 captured = capfd .readouterr ()
108116
109117 assert set (result ) == expected_result
110118 assert captured .out == expected_captured
111119
112120
113121def test_select_proposals_more_than_funds_under (capfd ):
114- np .random .seed (2025 )
115122 budget = 4.1
116123 funding_limit = 2
117124 proposals = [
@@ -150,15 +157,17 @@ def test_select_proposals_more_than_funds_under(capfd):
150157 'Fund "D" for $0.5 bringing its project\' s annual total to $0.5.\n '
151158 'Fund "B" for $1 bringing its project\' s annual total to $1.\n '
152159 )
153- result = select_proposals_to_fund (budget , funding_limit , proposals )
160+ result = select_proposals_to_fund (budget ,
161+ funding_limit ,
162+ proposals ,
163+ seed = 2025 )
154164 captured = capfd .readouterr ()
155165
156166 assert set (result ) == expected_result
157167 assert captured .out == expected_captured
158168
159169
160170def test_select_proposals_more_than_funds_eqweight_zero (capfd ):
161- np .random .seed (2025 )
162171 budget = 6
163172 funding_limit = 2
164173 proposals = [
@@ -198,15 +207,17 @@ def test_select_proposals_more_than_funds_eqweight_zero(capfd):
198207 'Fund "C" for $1 bringing its project\' s annual total to $1.\n '
199208 'Fund "A" for $1 bringing its project\' s annual total to $1.\n '
200209 )
201- result = select_proposals_to_fund (budget , funding_limit , proposals )
210+ result = select_proposals_to_fund (budget ,
211+ funding_limit ,
212+ proposals ,
213+ seed = 2025 )
202214 captured = capfd .readouterr ()
203215
204216 assert set (result ) == expected_result
205217 assert captured .out == expected_captured
206218
207219
208220def test_select_proposals_more_than_funds_eqweight_under (capfd ):
209- np .random .seed (2025 )
210221 budget = 5.4
211222 funding_limit = 2
212223 proposals = [
@@ -245,15 +256,17 @@ def test_select_proposals_more_than_funds_eqweight_under(capfd):
245256 'Fund "D" for $1 bringing its project\' s annual total to $1.\n '
246257 'Fund "C" for $1 bringing its project\' s annual total to $1.\n '
247258 )
248- result = select_proposals_to_fund (budget , funding_limit , proposals )
259+ result = select_proposals_to_fund (budget ,
260+ funding_limit ,
261+ proposals ,
262+ seed = 2025 )
249263 captured = capfd .readouterr ()
250264
251265 assert set (result ) == expected_result
252266 assert captured .out == expected_captured
253267
254268
255269def test_select_proposals_more_than_funds_eqweight_over (capfd ):
256- np .random .seed (2025 )
257270 budget = 6.6
258271 funding_limit = 2
259272 proposals = [
@@ -294,7 +307,10 @@ def test_select_proposals_more_than_funds_eqweight_over(capfd):
294307 'Fund "A" for $1 bringing its project\' s annual total to $1.\n '
295308 'Fund "F" for $1 bringing its project\' s annual total to $1.\n '
296309 )
297- result = select_proposals_to_fund (budget , funding_limit , proposals )
310+ result = select_proposals_to_fund (budget ,
311+ funding_limit ,
312+ proposals ,
313+ seed = 2025 )
298314 captured = capfd .readouterr ()
299315
300316 assert set (result ) == expected_result
0 commit comments