-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c46c252
commit 9bd8641
Showing
7 changed files
with
156 additions
and
90 deletions.
There are no files selected for viewing
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
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,17 @@ | ||
from extras.validators import CustomValidator | ||
|
||
|
||
class DataSourceValidator(CustomValidator): | ||
""" | ||
This validator prevents creation of more than one Data Source with "device_config_default" mark set to True | ||
""" | ||
|
||
def validate(self, instance): | ||
DataSource = type(instance) | ||
if DataSource.__name__ != "DataSource": | ||
return | ||
if not instance.cf.get("device_config_default"): | ||
return | ||
default_datasources = DataSource.objects.filter(custom_field_data__device_config_default=True) | ||
if default_datasources.exclude(pk=instance.pk).count() > 0: | ||
self.fail("Default repository already exists", field="cf_device_config_default") |
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
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
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,21 @@ | ||
from functools import partial | ||
|
||
import pytest | ||
from core.models import DataSource | ||
from django.core.exceptions import ValidationError | ||
|
||
from validity.custom_validators import DataSourceValidator | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_device_validator(create_custom_fields): | ||
validator = DataSourceValidator() | ||
GitDataSource = partial(DataSource, source_url="http://ab.io/d", type="git") | ||
data_source1 = GitDataSource(name="ds1", custom_field_data={"device_config_default": True}) | ||
validator.validate(data_source1) | ||
data_source1.save() | ||
data_source2 = GitDataSource(name="ds2", custom_field_data={"device_config_default": True}) | ||
with pytest.raises(ValidationError): | ||
validator.validate(data_source2) | ||
data_source3 = GitDataSource(name="ds3", custom_field_data={"device_config_default": False}) | ||
validator.validate(data_source3) |
This file was deleted.
Oops, something went wrong.
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,97 @@ | ||
import textwrap | ||
|
||
import pytest | ||
from django.core.exceptions import ValidationError | ||
from factories import CompTestDSFactory, NameSetDSFactory, SelectorFactory, SerializerDSFactory | ||
|
||
|
||
class BaseTestClean: | ||
factory: type | ||
right_kwargs: list[dict] = [] | ||
wrong_kwargs: list[dict] | ||
|
||
@pytest.mark.django_db | ||
def test_clean_right(self, subtests): | ||
for i, kwargs in enumerate(self.right_kwargs): | ||
with subtests.test(id=i): | ||
model = self.factory(**kwargs) | ||
model.clean() | ||
|
||
@pytest.mark.django_db | ||
def test_clean_wrong(self, subtests): | ||
for i, kwargs in enumerate(self.wrong_kwargs): | ||
with subtests.test(id=i): | ||
model = self.factory(**kwargs) | ||
with pytest.raises(ValidationError): | ||
model.clean() | ||
|
||
|
||
class TestDBNameSet(BaseTestClean): | ||
factory = NameSetDSFactory | ||
right_definition = textwrap.dedent( | ||
""" | ||
from collections import Counter | ||
__all__ = ['A', 'Counter', 'func'] | ||
def func(): | ||
pass | ||
class A: | ||
pass | ||
""" | ||
) | ||
|
||
right_kwargs = [ | ||
{"definitions": right_definition, "data_source": None, "data_file": None}, | ||
{"definitions": "", "contents": right_definition}, | ||
] | ||
wrong_kwargs = [ | ||
{"definitions": "a = 10", "data_source": None, "data_file": None}, | ||
{"definitions": "some invalid syntax", "data_source": None, "data_file": None}, | ||
{"definitions": "def some_func(): pass", "data_source": None, "data_file": None}, | ||
{"definitions": right_definition, "data_source": None}, | ||
{"definitions": right_definition, "data_file": None}, | ||
] | ||
|
||
|
||
class TestSelector(BaseTestClean): | ||
factory = SelectorFactory | ||
|
||
wrong_kwargs = [{"name_filter": "qwerty", "dynamic_pairs": "NAME"}, {"name_filter": "invalidregex))))"}] | ||
|
||
|
||
class TestSerializer(BaseTestClean): | ||
factory = SerializerDSFactory | ||
|
||
right_kwargs = [ | ||
{ | ||
"extraction_method": "TTP", | ||
"ttp_template": "interface {{ interface }}", | ||
"data_source": None, | ||
"data_file": None, | ||
}, | ||
{"extraction_method": "YAML", "data_source": None, "data_file": None}, | ||
{"extraction_method": "TTP", "ttp_template": "", "contents": "interface {{ interface }}"}, | ||
] | ||
wrong_kwargs = [ | ||
{"extraction_method": "TTP", "ttp_template": "", "data_source": None, "data_file": None}, | ||
{"extraction_method": "TTP", "ttp_template": "qwerty"}, | ||
{"extraction_method": "TTP", "ttp_template": "qwerty", "data_source": None}, | ||
{"extraction_method": "TTP", "ttp_template": "qwerty", "data_file": None}, | ||
{"extraction_method": "YAML"}, | ||
] | ||
|
||
|
||
class TestCompTest(BaseTestClean): | ||
factory = CompTestDSFactory | ||
|
||
right_kwargs = [{"expression": "a==1", "data_source": None, "data_file": None}, {}] | ||
wrong_kwargs = [ | ||
{"expression": "a == b"}, | ||
{"expression": "", "data_source": None, "data_file": None}, | ||
{"expression": "a==b", "data_source": None}, | ||
{"expression": "a==b", "data_file": None}, | ||
{"expression": "a = 10 + 15", "data_source": None, "data_file": None}, | ||
{"expression": "import itertools; a==b", "data_source": None, "data_file": None}, | ||
] |