Skip to content

Commit 02d26aa

Browse files
authored
[book-store] Add additional tests (#2174)
* [book-store] Add additional tests Adds tests that requires use of booth groups of 4 and 5 volumes. Closes #2141, which had gone stale. * [book-store] Update from latest template. * Crank the handle on Travis
1 parent d99090c commit 02d26aa

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"cases": [
3+
{
4+
"property": "total",
5+
"description": "Two groups of four and a group of five",
6+
"comments": ["Suggested grouping, [[1,2,3,4],[1,2,3,5],[1,2,3,4,5]]. Solutions can pass all the other tests if they just try to group without using any groups of 5. This test case breaks that since it requires both 4-groups and 5-groups."],
7+
"input": {
8+
"basket": [1,1,1,2,2,2,3,3,3,4,4,5,5]
9+
},
10+
"expected": 8120
11+
},
12+
{
13+
"property": "total",
14+
"description": "Shuffled book order",
15+
"comments": ["Suggested grouping, [[1,2,3,4],[1,2,3,5],[1,2,3,4,5]]. All the other tests give the books in sorted order. Robust solutions should be able to handle any order"],
16+
"input": {
17+
"basket": [1,2,3,4,5,1,2,3,4,5,1,2,3]
18+
},
19+
"expected": 8120
20+
}
21+
]
22+
}
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
{%- import "generator_macros.j2" as macros with context -%}
2-
{{ macros.header() }}
3-
4-
class {{ exercise | camel_case }}Test(unittest.TestCase):
5-
{% for casegroup in cases %}{% for case in casegroup["cases"] -%}
2+
{% macro test_case(case) -%}
63
{%- set input = case["input"] -%}
74
def test_{{ case["description"] | to_snake }}(self):
85
basket = {{ input["basket"] }}
96
self.assertEqual({{ case["property"] }}(basket), {{ case["expected"] }})
7+
{%- endmacro %}
8+
{{ macros.header() }}
9+
10+
class {{ exercise | camel_case }}Test(unittest.TestCase):
11+
{% for casegroup in cases %}
12+
{% for case in casegroup["cases"] -%}
13+
{{ test_case(case) }}
14+
{% endfor %}
15+
{% endfor %}
16+
17+
{% if additional_cases | length -%}
18+
19+
# Additional tests for this track
1020

11-
{% endfor %}{% endfor %}
21+
{% for case in additional_cases -%}
22+
{{ test_case(case) }}
23+
{% endfor %}
24+
{%- endif %}
1225

13-
{{ macros.footer() }}
26+
{{ macros.footer() }}

exercises/book-store/book_store_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ def test_four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three(
6868
basket = [1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]
6969
self.assertEqual(total(basket), 10240)
7070

71+
# Additional tests for this track
72+
73+
def test_two_groups_of_four_and_a_group_of_five(self):
74+
basket = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5]
75+
self.assertEqual(total(basket), 8120)
76+
77+
def test_shuffled_book_order(self):
78+
basket = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3]
79+
self.assertEqual(total(basket), 8120)
80+
7181

7282
if __name__ == "__main__":
7383
unittest.main()

exercises/book-store/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def _total(books):
2323
def total(books):
2424
if not books:
2525
return 0
26-
return _total(sorted(books))
26+
return _total(sorted(books))

0 commit comments

Comments
 (0)