Skip to content

Commit

Permalink
fix: Coverage of resolve_changed_variable_names
Browse files Browse the repository at this point in the history
  • Loading branch information
christokur committed Feb 5, 2023
1 parent 9145296 commit f6ada27
Showing 1 changed file with 215 additions and 21 deletions.
236 changes: 215 additions & 21 deletions tests/test_generate_context_v2.py
Expand Up @@ -233,9 +233,9 @@ def context_data_serializer():

context_with_default = (
{
'context_file': 'tests/test-generate-context-v2/test.json',
'context_file': 'tests/test-generate-context-v2/test.json',
'default_context': {
'full_name': 'James Patrick Morgan',
'full_name': 'James Patrick Morgan',
'this_key_ignored': 'not_in_context',
},
},
Expand All @@ -244,15 +244,15 @@ def context_data_serializer():

context_with_extra = (
{
'context_file': 'tests/test-generate-context-v2/test.json',
'context_file': 'tests/test-generate-context-v2/test.json',
'extra_context': {'email': 'jpm@chase.bk'},
},
{"test": expected_file1_v2},
)

context_choices_with_default = (
{
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'default_context': {'license': 'Apache2'},
},
{
Expand Down Expand Up @@ -295,24 +295,24 @@ def context_data_serializer():
)
context_with_default_and_extra = (
{
'context_file': 'tests/test-generate-context-v2/test.json',
'context_file': 'tests/test-generate-context-v2/test.json',
'default_context': {'full_name': 'Alpha Gamma Five'},
'extra_context': {'email': 'agamma5@universe.open'},
'extra_context': {'email': 'agamma5@universe.open'},
},
{"test": expected_file1_v3},
)

context_choices_with_default_not_in_choices = (
{
'context_file': 'tests/test-generate-context-v2/test.json',
'context_file': 'tests/test-generate-context-v2/test.json',
'default_context': {'orientation': 'landscape'},
},
{"test": expected_file1_v0},
)

context_choices_with_default_misses = (
{
'context_file': 'tests/test-generate-context-v2/test_choices-miss.json',
'context_file': 'tests/test-generate-context-v2/test_choices-miss.json',
'default_context': {'license': 'Cherokee'},
},
{
Expand Down Expand Up @@ -356,7 +356,7 @@ def context_data_serializer():

context_choices_with_extra_misses = (
{
'context_file': 'tests/test-generate-context-v2/test_choices-miss.json',
'context_file': 'tests/test-generate-context-v2/test_choices-miss.json',
'extra_context': {'license': 'MIT'},
},
{
Expand Down Expand Up @@ -421,7 +421,7 @@ def test_generate_context(input_params, expected_context):
def context_data_value_errors():
context_choices_with_default_value_error = (
{
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'default_context': [{'license': 'MIT'}],
},
{
Expand Down Expand Up @@ -457,9 +457,9 @@ def context_data_value_errors():
)
context_choices_with_extra_value_error = (
{
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'context_file': 'tests/test-generate-context-v2/test_choices.json',
'default_context': {'license': 'Apache2'},
'extra_context': [{'name': 'license', 'default': 'MIT'}],
'extra_context': [{'name': 'license', 'default': 'MIT'}],
},
{
"test_choices": OrderedDict(
Expand Down Expand Up @@ -557,10 +557,10 @@ def test_generate_context_extra_ctx_list_item_dict_missing_name_field():
xtra_context = [
{
"shouldbename": "author_name",
"default": "Robert Lewis",
"prompt": "What's the author's name?",
"description": "Please enter the author's full name.",
"type": "string",
"default": "Robert Lewis",
"prompt": "What's the author's name?",
"description": "Please enter the author's full name.",
"type": "string",
}
]
with pytest.raises(ValueError) as excinfo:
Expand All @@ -582,11 +582,11 @@ def test_generate_context_extra_ctx_list_item_dict_no_name_field_match():
"""
xtra_context = [
{
"name": "author_name",
"default": "Robert Lewis",
"prompt": "What's the author's name?",
"name": "author_name",
"default": "Robert Lewis",
"prompt": "What's the author's name?",
"description": "Please enter the author's full name.",
"type": "string",
"type": "string",
}
]
with pytest.raises(ValueError) as excinfo:
Expand All @@ -610,7 +610,7 @@ def test_raise_exception_when_attempting_to_remove_mandatory_field():
"""
xtra_context = [
{
'name': 'director_name',
'name': 'director_name',
'default': '<<REMOVE::FIELD>>',
},
]
Expand All @@ -623,3 +623,197 @@ def test_raise_exception_when_attempting_to_remove_mandatory_field():
)

assert "Cannot remove mandatory 'default' field" in str(excinfo.value)


def gen_context_data_inputs_expected_var():
# Test the ability to change the variable's name field (since it is used
# to identify the variable to be modifed) with extra context and to remove
# a key from the context via the removal token: '<<REMOVE::FIELD>>'
context_with_valid_extra_2 = (
{
'context_file': 'tests/test-generate-context-v2/representative.json',
'extra_context': [
{
'name': 'director_credit::producer_credit',
'prompt': 'Is there a producer credit on this film?',
'description': 'There are usually a lot of producers...',
},
{
'name': 'director_name',
'skip_if': '<<REMOVE::FIELD>>',
},
],
},
OrderedDict(
[
(
"representative", OrderedDict(
[
('version', '2.0'),
(
"template", OrderedDict(
[
("name", "cc-representative"),
(
"variables",
[
OrderedDict(
[
("name", "producer_credit"),
("default", True),
(
"prompt",
"Is there a producer credit on this film?",
),
(
"description",
"There are usually a lot of producers...",
),
("type", "boolean"),
]
),
OrderedDict(
[
("name", "director_name"),
("default", "Allan Smithe"),
("prompt", "What's the Director's full name?"),
("prompt_user", True),
(
"description",
"The default director is not proud of their work, we hope you are.",
),
("hide_input", False),
(
"choices",
[
"Allan Smithe",
"Ridley Scott",
"Victor Fleming",
"John Houston",
],
),
("validation", "^[a-z][A-Z]+$"),
("validation_flags", ["verbose", "ascii"]),
("type", "string"),
]
),
],
),
]
)
),
]
)
)
]
)
)
# Test the ability to change the variable's name field (since it is used
# to identify the variable to be modifed) with extra context and to also
# test that any other references in other variables that might use the
# original variable name get updated as well.
context_with_valid_extra_2_B = (
{
'context_file': 'tests/test-generate-context-v2/representative_2B.json',
'extra_context': [
{
'name': 'director_credit::producer_credit',
'prompt': 'Is there a producer credit on this film?',
'description': 'There are usually a lot of producers...',
},
],
},
OrderedDict(
[
(
"representative_2B", OrderedDict(
[
('version', '2.0'),
(
'requires', OrderedDict(
[
('cookiecutter', '>1'),
('python', '>=3.0')
]
)
),
(
"template", OrderedDict(
[
("name", "cc-representative"),
(
"variables",
[
OrderedDict(
[
("name", "producer_credit"),
("default", True),
(
"prompt",
"Is there a producer credit on this film?",
),
(
"description",
"There are usually a lot of producers...",
),
("type", "boolean"),
]
),
OrderedDict(
[
("name", "director_name"),
("default", "Allan Smithe"),
("prompt", "What's the Director's full name?"),
("prompt_user", True),
(
"description",
"The default director is not proud of their work, we hope you are.",
),
("hide_input", False),
(
"choices",
[
"Allan Smithe",
"Ridley Scott",
"Victor Fleming",
"John Houston",
"{{cookiecutter.producer_credit}}",
],
),
("validation", "^[a-z][A-Z]+$"),
("validation_flags", ["verbose", "ascii"]),
(
"skip_if",
"{{cookiecutter.producer_credit == False}}",
),
("type", "string"),
]
),
],
),
]
)
)
]
)
)
]
)
)

yield context_with_valid_extra_2
yield context_with_valid_extra_2_B


@pytest.mark.usefixtures('clean_system')
@pytest.mark.parametrize(
'input_params, expected_context', gen_context_data_inputs_expected_var()
)
def test_generate_context_with_extra_context_dictionary_var(
input_params, expected_context, monkeypatch
):
"""
Test the generated context with extra content overwrite to multiple fields,
with creation of new fields NOT allowed.
"""
assert generate.generate_context(**input_params) == expected_context

0 comments on commit f6ada27

Please sign in to comment.