Skip to content

[book-store] Add additional tests #2174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions exercises/book-store/.meta/additional_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"cases": [
{
"property": "total",
"description": "Two groups of four and a group of five",
"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."],
"input": {
"basket": [1,1,1,2,2,2,3,3,3,4,4,5,5]
},
"expected": 8120
},
{
"property": "total",
"description": "Shuffled book order",
"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"],
"input": {
"basket": [1,2,3,4,5,1,2,3,4,5,1,2,3]
},
"expected": 8120
}
]
}
25 changes: 19 additions & 6 deletions exercises/book-store/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.header() }}

class {{ exercise | camel_case }}Test(unittest.TestCase):
{% for casegroup in cases %}{% for case in casegroup["cases"] -%}
{% macro test_case(case) -%}
{%- set input = case["input"] -%}
def test_{{ case["description"] | to_snake }}(self):
basket = {{ input["basket"] }}
self.assertEqual({{ case["property"] }}(basket), {{ case["expected"] }})
{%- endmacro %}
{{ macros.header() }}

class {{ exercise | camel_case }}Test(unittest.TestCase):
{% for casegroup in cases %}
{% for case in casegroup["cases"] -%}
{{ test_case(case) }}
{% endfor %}
{% endfor %}

{% if additional_cases | length -%}

# Additional tests for this track

{% endfor %}{% endfor %}
{% for case in additional_cases -%}
{{ test_case(case) }}
{% endfor %}
{%- endif %}

{{ macros.footer() }}
{{ macros.footer() }}
10 changes: 10 additions & 0 deletions exercises/book-store/book_store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ def test_four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three(
basket = [1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]
self.assertEqual(total(basket), 10240)

# Additional tests for this track

def test_two_groups_of_four_and_a_group_of_five(self):
basket = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5]
self.assertEqual(total(basket), 8120)

def test_shuffled_book_order(self):
basket = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3]
self.assertEqual(total(basket), 8120)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion exercises/book-store/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def _total(books):
def total(books):
if not books:
return 0
return _total(sorted(books))
return _total(sorted(books))