Skip to content

Commit

Permalink
Add test for validate_choices method from GitCommand and update imple…
Browse files Browse the repository at this point in the history
…mentation
  • Loading branch information
dl1998 committed Dec 27, 2023
1 parent b81ee63 commit 69c63c8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion sources/options/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class GitOptionDefinition:
type: Union[Type, Tuple]
required: bool = field(default=False)
positional: bool = field(default=False)
position: int = field(default=None)
position: Optional[int] = field(default=None)
choices: Union[list, Type[Enum]] = field(default=None)
separator: str = field(default=' ')

Expand Down
51 changes: 48 additions & 3 deletions tests/unit_tests/pygit/options/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from pygit.options.testing_utils import GitCommandGenerator
from pygit.options.testing_utils import GitModelGenerator
from sources.exceptions import GitException, GitMissingDefinitionException, GitIncorrectOptionValueException, \
GitMissingRequiredOptionsException
from sources.options.options import GitOptionNameAliases, GitOptionNameAlias, GitOptionDefinition, GitOption, \
Expand Down Expand Up @@ -297,7 +297,7 @@ def test_validate_positional_list_positive(self, positional_definitions: List[Tu
for alias in definition[0]:
aliases.append({'name': alias, 'short-option': not len(alias) > 1})
definitions.append({'aliases': aliases, 'positional': True, 'type': definition[1]})
command = GitCommandGenerator.from_dict({
command = GitModelGenerator.generate_git_command({
'command': 'demo-command',
'definitions': definitions
})
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_validate_required_positive(self, option_definitions: List[str], expecte
options = []
for option in option_definitions:
options.append(GitOption(name=option, value='value'))
command = GitCommandGenerator.from_dict({
command = GitModelGenerator.generate_git_command({
'command': 'demo-command',
'definitions': [
{
Expand Down Expand Up @@ -365,6 +365,51 @@ def test_validate_required_positive(self, option_definitions: List[str], expecte
})
assert command.validate_required(options) == expected

@pytest.mark.parametrize("defined_choices,value,with_definition,expected", [
(None, 'first', True, True),
(None, 'first', False, True),
(['one', 'first', 1], 'first', True, True),
(['one', 'first', 1], 1, False, True),
(['one', 'first', 1], 2, True, False),
(['one', 'first', 1], 2, False, False),
], ids=("Correct option without defined choices with definition",
"Correct option without defined choices without definition",
"Correct option with defined choices with definition",
"Correct option with defined choices without definition",
"Incorrect option with defined choices with definition",
"Incorrect option with defined choices without definition"))
def test_validate_choices_positive(self, defined_choices: List, value: Any, with_definition: bool, expected: bool):
"""
Method tests that 'validate_choices' method from 'GitCommand' class returns True, if it is correct option,
otherwise return False.
:param defined_choices: List of choices for the definition.
:type defined_choices: List
:param value: Git option value.
:type value: Any
:param with_definition: If true, then test with definition, otherwise test without definition.
:type with_definition: bool
:param expected: The expected value that shall be returned by method.
:type expected: bool
"""
option = GitOption(name='option', value=value)
command = GitModelGenerator.generate_git_command({
'command': 'demo-command',
'definitions': [
{
'aliases': [
{
'name': 'option',
'short-option': False
}
],
'choices': defined_choices,
'type': (str, int)
}
]
})
assert command.validate_choices(option, command.definitions[0] if with_definition else None) == expected

def test_transform_to_command_positive(self):
"""
Method tests that 'transform_to_command' method from 'GitCommand' class is able to correctly transform
Expand Down
58 changes: 35 additions & 23 deletions tests/unit_tests/pygit/options/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,52 @@ def test_definitions_positive(class_type: Type[GitCommand]):
assert len(missing_options) == 0


class GitCommandGenerator:
class GitModelGenerator:
"""
Class contains methods to generate 'GitCommand' class instance.
"""
@staticmethod
def from_dict(parameters: dict) -> 'GitCommand':
def generate_git_command(configuration: dict) -> 'GitCommand':
"""
Method that create an instance of 'GitCommand' class based on the provided dictionary.
:param parameters: Dictionary that is used to create 'GitCommand' class instance.
:type parameters: dict
:param configuration: Dictionary that is used to create 'GitCommand' class instance.
:type configuration: dict
:return: Instance of the 'GitCommand' class.
"""
command = GitCommand(parameters['command'])
command = GitCommand(configuration['command'])
definitions = []
positional_index = 0
for definition in parameters['definitions']:
aliases = []
for alias in definition['aliases']:
aliases.append(GitOptionNameAlias(name=alias['name'], short_option=alias['short-option']))
option_aliases = GitOptionNameAliases(aliases=aliases)
positional = definition.get('positional', False)
if positional:
position = positional_index
for definition in configuration['definitions']:
if definition.get('positional', False):
definition['position'] = positional_index
positional_index += 1
else:
position = None
option_definition = GitOptionDefinition(name_aliases=option_aliases, type=definition.get('type', str),
positional=positional, position=position)
required = definition.get('required', None)
if required:
option_definition.required = required
choices = definition.get('choices', None)
if choices:
option_definition.choices = choices
option_definition = GitModelGenerator.generate_git_option_definition(definition)
definitions.append(option_definition)
command.definitions = definitions
return command

@staticmethod
def generate_git_option_definition(configuration: dict) -> GitOptionDefinition:
"""
Method that create an instance of 'GitOptionDefinition' class based on the provided dictionary.
:param configuration: Dictionary that is used to create 'GitOptionDefinition' class instance.
:type configuration: dict
:return: Instance of the 'GitOptionDefinition' class.
"""
aliases = []
for alias in configuration['aliases']:
aliases.append(GitOptionNameAlias(name=alias['name'], short_option=alias['short-option']))
option_aliases = GitOptionNameAliases(aliases=aliases)
positional = configuration.get('positional', False)
position = configuration.get('position', None)
option_definition = GitOptionDefinition(name_aliases=option_aliases, type=configuration.get('type', str),
positional=positional, position=position)
required = configuration.get('required', None)
if required:
option_definition.required = required
choices = configuration.get('choices', None)
if choices:
option_definition.choices = choices
return option_definition

0 comments on commit 69c63c8

Please sign in to comment.