-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for apps plugin (#14726)
- Loading branch information
Showing
13 changed files
with
1,665 additions
and
0 deletions.
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
src/middlewared/middlewared/pytest/unit/plugins/apps/test_construct_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
import pytest | ||
|
||
from middlewared.plugins.apps.schema_utils import construct_schema | ||
from middlewared.schema import Dict, ValidationErrors | ||
|
||
|
||
@pytest.mark.parametrize('data, new_values, update', [ | ||
( | ||
{ | ||
'schema': { | ||
'groups': [ | ||
{ | ||
'name': 'Actual Budget Configuration', | ||
'description': 'Configure Actual Budget' | ||
}, | ||
], | ||
'questions': [ | ||
{ | ||
'variable': 'actual_budget', | ||
'label': '', | ||
'group': 'Actual Budget Configuration', | ||
'schema': { | ||
'type': 'dict', | ||
'attrs': [ | ||
{ | ||
'variable': 'additional_envs', | ||
'label': 'Additional Environment Variables', | ||
'description': 'Configure additional environment variables for Actual Budget.', | ||
'schema': { | ||
'type': 'list', | ||
'default': [], | ||
'items': [ | ||
{ | ||
'variable': 'env', | ||
'label': 'Environment Variable', | ||
'schema': { | ||
'type': 'dict', | ||
'attrs': [ | ||
{ | ||
'variable': 'name', | ||
'label': 'Name', | ||
'schema': { | ||
'type': 'string', | ||
'required': True | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
'actual_budget': {'additional_envs': []} | ||
}, | ||
False, | ||
), | ||
]) | ||
def test_construct_schema_update_False(data, new_values, update): | ||
result = construct_schema(data, new_values, update) | ||
assert isinstance(result['verrors'], ValidationErrors) | ||
assert len(result['verrors'].errors) == 0 | ||
assert isinstance(result['dict_obj'], Dict) | ||
assert result['new_values'] == new_values | ||
assert result['schema_name'] == 'app_create' | ||
|
||
|
||
@pytest.mark.parametrize('data, new_values, update', [ | ||
( | ||
{ | ||
'schema': { | ||
'groups': [ | ||
{ | ||
'name': 'Actual Budget Configuration', | ||
'description': 'Configure Actual Budget' | ||
}, | ||
], | ||
'questions': [ | ||
{ | ||
'variable': 'actual_budget', | ||
'label': '', | ||
'group': 'Actual Budget Configuration', | ||
'schema': { | ||
'type': 'dict', | ||
'attrs': [ | ||
{ | ||
'variable': 'additional_envs', | ||
'label': 'Additional Environment Variables', | ||
'description': 'Configure additional environment variables for Actual Budget.', | ||
'schema': { | ||
'type': 'list', | ||
'default': [], | ||
'items': [ | ||
{ | ||
'variable': 'env', | ||
'label': 'Environment Variable', | ||
'schema': { | ||
'type': 'dict', | ||
'attrs': [ | ||
{ | ||
'variable': 'name', | ||
'label': 'Name', | ||
'schema': { | ||
'type': 'string', | ||
'required': True | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
'actual_budget': {'additional_envs': []} | ||
}, | ||
True | ||
) | ||
]) | ||
def test_construct_schema_update_True(data, new_values, update): | ||
result = construct_schema(data, new_values, update) | ||
assert isinstance(result['verrors'], ValidationErrors) | ||
assert len(result['verrors'].errors) == 0 | ||
assert isinstance(result['dict_obj'], Dict) | ||
assert result['new_values'] == new_values | ||
assert result['schema_name'] == 'app_update' | ||
|
||
|
||
@pytest.mark.parametrize('data, update', [ | ||
( | ||
{ | ||
'schema': { | ||
'groups': [ | ||
{ | ||
'name': 'Actual Budget Configuration', | ||
'description': 'Configure Actual Budget' | ||
}, | ||
], | ||
} | ||
}, | ||
True, | ||
), | ||
]) | ||
def test_construct_schema_KeyError(data, update): | ||
with pytest.raises(KeyError): | ||
construct_schema(data, {}, update) | ||
|
||
|
||
@pytest.mark.parametrize('data, new_values, update', [ | ||
( | ||
{ | ||
'schema': { | ||
'groups': [ | ||
{ | ||
'name': 'Actual Budget Configuration', | ||
'description': 'Configure Actual Budget' | ||
}, | ||
], | ||
'questions': [ | ||
{ | ||
'variable': 'actual_budget', | ||
'label': '', | ||
'group': 'Actual Budget Configuration', | ||
'schema': { | ||
'type': 'dict', | ||
'attrs': [ | ||
{ | ||
'variable': 'additional_envs', | ||
'label': 'Additional Environment Variables', | ||
'description': 'Configure additional environment variables for Actual Budget.', | ||
'schema': { | ||
'type': 'list', | ||
'default': [], | ||
'items': [ | ||
{ | ||
'variable': 'env', | ||
'label': 'Environment Variable', | ||
'schema': { | ||
'type': 'dict', | ||
'attrs': [ | ||
{ | ||
'variable': 'name', | ||
'label': 'Name', | ||
'schema': { | ||
'type': 'string', | ||
'required': True | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
'actual_budget': {'additional_envs': 'abc'} | ||
}, | ||
True, | ||
), | ||
]) | ||
def test_construct_schema_ValidationError(data, new_values, update): | ||
result = construct_schema(data, new_values, update) | ||
assert isinstance(result['verrors'], ValidationErrors) | ||
assert len(result['verrors'].errors) > 0 | ||
assert isinstance(result['dict_obj'], Dict) | ||
assert result['new_values'] == new_values | ||
assert result['schema_name'] == 'app_update' if update else 'app_create' |
90 changes: 90 additions & 0 deletions
90
src/middlewared/middlewared/pytest/unit/plugins/apps/test_get_latest_version.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import pytest | ||
|
||
from middlewared.plugins.apps.version_utils import get_latest_version_from_app_versions | ||
from middlewared.service import CallError | ||
|
||
|
||
@pytest.mark.parametrize('versions, should_work, expected', [ | ||
( | ||
{ | ||
'1.1.9': { | ||
'healthy': True, | ||
'supported': True, | ||
'healthy_error': None, | ||
'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.9', | ||
'last_update': '2024-10-02 18:57:15', | ||
'required_features': [ | ||
'definitions/certificate', | ||
'definitions/port', | ||
'normalize/acl', | ||
'normalize/ix_volume' | ||
], | ||
} | ||
}, | ||
True, | ||
'1.1.9' | ||
), | ||
( | ||
{ | ||
'1.1.9': { | ||
'healthy': None, | ||
'supported': True, | ||
'healthy_error': None, | ||
'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.9', | ||
'last_update': '2024-10-02 18:57:15', | ||
'required_features': [ | ||
'definitions/certificate', | ||
'definitions/port', | ||
'normalize/acl', | ||
'normalize/ix_volume' | ||
], | ||
} | ||
}, | ||
False, | ||
None | ||
), | ||
( | ||
{}, | ||
False, | ||
None | ||
), | ||
( | ||
{ | ||
'1.1.9': { | ||
'healthy': None, | ||
'supported': True, | ||
'healthy_error': None, | ||
'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.9', | ||
'last_update': '2024-10-02 18:57:15', | ||
'required_features': [ | ||
'definitions/certificate', | ||
'definitions/port', | ||
'normalize/acl', | ||
'normalize/ix_volume' | ||
], | ||
}, | ||
'2.0.1': { | ||
'healthy': True, | ||
'supported': True, | ||
'healthy_error': None, | ||
'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/2.0.1', | ||
'last_update': '2024-10-02 18:57:15', | ||
'required_features': [ | ||
'definitions/certificate', | ||
'definitions/port', | ||
'normalize/acl', | ||
'normalize/ix_volume' | ||
], | ||
} | ||
}, | ||
True, | ||
'2.0.1' | ||
), | ||
]) | ||
def test_get_latest_version(versions, should_work, expected): | ||
if should_work: | ||
version = get_latest_version_from_app_versions(versions) | ||
assert version == expected | ||
else: | ||
with pytest.raises(CallError): | ||
get_latest_version_from_app_versions(versions) |
56 changes: 56 additions & 0 deletions
56
src/middlewared/middlewared/pytest/unit/plugins/apps/test_get_list_item_from_value.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
from middlewared.plugins.apps.schema_utils import get_list_item_from_value | ||
from middlewared.schema import List | ||
|
||
|
||
@pytest.mark.parametrize('values, question_attr, should_work', [ | ||
( | ||
['val1', 'val2', 'val3'], | ||
List( | ||
items=[ | ||
List({'question1': 'desc1'}), | ||
List({'question2': 'desc2'}), | ||
List({'question3': 'desc3'}) | ||
] | ||
), | ||
True | ||
), | ||
( | ||
None, | ||
List( | ||
items=[ | ||
List({'question1': 'desc1'}), | ||
List({'question2': 'desc2'}), | ||
List({'question3': 'desc3'}) | ||
] | ||
), | ||
True | ||
), | ||
( | ||
[{'val1': 'a'}, {'val2': 'b'}, {'val3': 'c'}], | ||
List( | ||
items=[ | ||
List({'question1': 'desc1'}), | ||
List({'question2': 'desc2'}), | ||
List({'question3': 'desc3'}) | ||
] | ||
), | ||
True | ||
), | ||
( | ||
['val1', 'val1'], | ||
List( | ||
items=[ | ||
List({'question1': 'desc1'}, unique=True), | ||
], | ||
), | ||
False | ||
), | ||
]) | ||
def test_get_list_item_from_value(values, question_attr, should_work): | ||
if should_work: | ||
result = get_list_item_from_value(values, question_attr) | ||
assert result is not None | ||
else: | ||
assert get_list_item_from_value(values, question_attr) is None |
Oops, something went wrong.