Skip to content

Commit

Permalink
tests done
Browse files Browse the repository at this point in the history
  • Loading branch information
amyasnikov committed Nov 26, 2023
1 parent c46c252 commit 9bd8641
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 90 deletions.
5 changes: 4 additions & 1 deletion development/configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import socket

from .configuration_example import *

Expand Down Expand Up @@ -69,4 +68,8 @@ class ContainsAll:
def __contains__(self, v):
return True


INTERNAL_IPS = ContainsAll()


CUSTOM_VALIDATORS = {"core.datasource": ["validity.custom_validators.DataSourceValidator"]}
17 changes: 17 additions & 0 deletions validity/custom_validators.py
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")
18 changes: 16 additions & 2 deletions validity/models/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -40,9 +41,23 @@ class DataSourceMixin(models.Model):
class Meta:
abstract = True

def _validate_db_or_git_filled(self) -> bool: # TODO: add this to self.clean
@property
def _validate_db_or_git_filled(self) -> bool:
return True

def clean(self) -> None:
text_value = getattr(self, self.text_db_field_name)
if text_value and (self.data_source or self.data_file):
raise ValidationError(_(f"You cannot set both: data_source/data_file and {self.text_db_field_name}"))
if self._validate_db_or_git_filled and not text_value and (not self.data_source or not self.data_file):
raise ValidationError(
{
self.text_db_field_name: _(
f"You must set either {self.text_db_field_name} or both: data_source and data_file"
)
}
)

def effective_text_field(self) -> str:
text_db_value = getattr(self, self.text_db_field_name)
if text_db_value:
Expand All @@ -69,7 +84,6 @@ class BaseReadOnlyModel(
WebhooksMixin,
models.Model,
):

created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
last_updated = models.DateTimeField(auto_now=True, blank=True, null=True)

Expand Down
1 change: 1 addition & 0 deletions validity/models/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __str__(self) -> str:
def get_extraction_method_color(self):
return ConfigExtractionChoices.colors.get(self.extraction_method)

@property
def _validate_db_or_git_filled(self) -> bool:
return self.extraction_method == "TTP"

Expand Down
21 changes: 21 additions & 0 deletions validity/tests/test_custom_validators.py
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)
87 changes: 0 additions & 87 deletions validity/tests/test_models/_test_clean.py

This file was deleted.

97 changes: 97 additions & 0 deletions validity/tests/test_models/test_clean.py
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},
]

0 comments on commit 9bd8641

Please sign in to comment.