Skip to content

Commit

Permalink
Add unit tests for apps plugin (#14726)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj authored Oct 19, 2024
1 parent 21ae02d commit 4a5e312
Show file tree
Hide file tree
Showing 13 changed files with 1,665 additions and 0 deletions.
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'
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)
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
Loading

0 comments on commit 4a5e312

Please sign in to comment.