<commit_before>#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import string

import mock
import netaddr

from neutron_lib._i18n import _
from neutron_lib.api import converters
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.tests import _base as base


def dummy_validator(data, valid_values=None):
    pass


class TestAttributeValidation(base.BaseTestCase):

    def _construct_dict_and_constraints(self):
        """Constructs a test dictionary and a definition of constraints.

        :return: A (dictionary, constraint) tuple
        """

        constraints = {'key1': {'type:values': ['val1', 'val2'],
                                'required': True},
                       'key2': {'type:string': None,
                                'required': False},
                       'key3': {'type:dict': {'k4': {'type:string': None,
                                                     'required': True}},
                                'required': True}}

        dictionary = {'key1': 'val1',
                      'key2': 'a string value',
                      'key3': {'k4': 'a string value'}}

        return dictionary, constraints

    def test_type_prefixing(self):
        validators.add_validator('type:prefixed_type', dummy_validator)
        validators.add_validator('unprefixed_type', dummy_validator)
        self.assertEqual(dummy_validator,
                         validators.get_validator('type:prefixed_type'))
        self.assertEqual(dummy_validator,
                         validators.get_validator('prefixed_type'))
        self.assertEqual(dummy_validator,
                         validators.get_validator('type:unprefixed_type'))
        self.assertEqual(dummy_validator,
                         validators.get_validator('unprefixed_type'))

    def test_adding_validator(self):
        validators.add_validator('new_type', dummy_validator)
        self.assertIn('type:new_type', validators.validators)
        self.assertEqual(dummy_validator,
                         validators.validators['type:new_type'])

    def test_get_validator_default(self):
        self.assertEqual(dummy_validator,
                         validators.get_validator('nope',
                                                  default=dummy_validator))

    def test_fail_adding_duplicate_validator(self):
        self.assertRaises(KeyError,
                          validators.add_validator,
                          'dict', lambda x: x)

    def test_success_adding_duplicate_validator(self):
        validators.add_validator('dummy', dummy_validator)
        validators.add_validator('dummy', dummy_validator)
        self.assertEqual(dummy_validator, validators.get_validator('dummy'))

    def test_is_attr_set(self):
        data = constants.ATTR_NOT_SPECIFIED
        self.assertIs(validators.is_attr_set(data), False)

        data = None
        self.assertIs(validators.is_attr_set(data), False)

        data = "I'm set"
        self.assertIs(validators.is_attr_set(data), True)

    def test_validate_values(self):
        # Check that validation is not performed if valid_values is not set
        msg = validators.validate_values(4)
        self.assertIsNone(msg)

        # Check that value is within valid_values
        msg = validators.validate_values(4, [4, 6])
        self.assertIsNone(msg)

        # Check that value is within valid_values
        msg = validators.validate_values(4, (4, 6))
        self.assertIsNone(msg)

        # Check that value is within valid_values with strings
        msg = validators.validate_values("1", ["2", "1", "4", "5"])
        self.assertIsNone(msg)

        # Check that value is not compatible for comparision
        response = "'valid_values' does not support membership operations"
        self.assertRaisesRegex(TypeError, response,
                               validators.validate_values, data=None,
                               valid_values=True)

    def test_validate_values_display(self):
        # Check that value is NOT within valid_values and report values
        msg = validators.validate_values(7, [4, 6],
                                         valid_values_display="[4, 6]")
        self.assertEqual("7 is not in [4, 6]", msg)

        # Check that value is NOT within valid_values and report values
        msg = validators.validate_values(7, (4, 6),
                                         valid_values_display="(4, 6)")
        self.assertEqual("7 is not in (4, 6)", msg)

        # Check values with a range function showing a custom string
        msg = validators.validate_values(8, range(8),
                                         valid_values_display="[0..7]")
        self.assertEqual("8 is not in [0..7]", msg)

        # Check that value is not within valid_values and custom string<commit_msg>Fix typo for comparision => comparison<commit_after>#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import string

import mock
import netaddr

from neutron_lib._i18n import _
from neutron_lib.api import converters
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.tests import _base as base


def dummy_validator(data, valid_values=None):
    pass


class TestAttributeValidation(base.BaseTestCase):

    def _construct_dict_and_constraints(self):
        """Constructs a test dictionary and a definition of constraints.

        :return: A (dictionary, constraint) tuple
        """

        constraints = {'key1': {'type:values': ['val1', 'val2'],
                                'required': True},
                       'key2': {'type:string': None,
                                'required': False},
                       'key3': {'type:dict': {'k4': {'type:string': None,
                                                     'required': True}},
                                'required': True}}

        dictionary = {'key1': 'val1',
                      'key2': 'a string value',
                      'key3': {'k4': 'a string value'}}

        return dictionary, constraints

    def test_type_prefixing(self):
        validators.add_validator('type:prefixed_type', dummy_validator)
        validators.add_validator('unprefixed_type', dummy_validator)
        self.assertEqual(dummy_validator,
                         validators.get_validator('type:prefixed_type'))
        self.assertEqual(dummy_validator,
                         validators.get_validator('prefixed_type'))
        self.assertEqual(dummy_validator,
                         validators.get_validator('type:unprefixed_type'))
        self.assertEqual(dummy_validator,
                         validators.get_validator('unprefixed_type'))

    def test_adding_validator(self):
        validators.add_validator('new_type', dummy_validator)
        self.assertIn('type:new_type', validators.validators)
        self.assertEqual(dummy_validator,
                         validators.validators['type:new_type'])

    def test_get_validator_default(self):
        self.assertEqual(dummy_validator,
                         validators.get_validator('nope',
                                                  default=dummy_validator))

    def test_fail_adding_duplicate_validator(self):
        self.assertRaises(KeyError,
                          validators.add_validator,
                          'dict', lambda x: x)

    def test_success_adding_duplicate_validator(self):
        validators.add_validator('dummy', dummy_validator)
        validators.add_validator('dummy', dummy_validator)
        self.assertEqual(dummy_validator, validators.get_validator('dummy'))

    def test_is_attr_set(self):
        data = constants.ATTR_NOT_SPECIFIED
        self.assertIs(validators.is_attr_set(data), False)

        data = None
        self.assertIs(validators.is_attr_set(data), False)

        data = "I'm set"
        self.assertIs(validators.is_attr_set(data), True)

    def test_validate_values(self):
        # Check that validation is not performed if valid_values is not set
        msg = validators.validate_values(4)
        self.assertIsNone(msg)

        # Check that value is within valid_values
        msg = validators.validate_values(4, [4, 6])
        self.assertIsNone(msg)

        # Check that value is within valid_values
        msg = validators.validate_values(4, (4, 6))
        self.assertIsNone(msg)

        # Check that value is within valid_values with strings
        msg = validators.validate_values("1", ["2", "1", "4", "5"])
        self.assertIsNone(msg)

        # Check that value is not compatible for comparison
        response = "'valid_values' does not support membership operations"
        self.assertRaisesRegex(TypeError, response,
                               validators.validate_values, data=None,
                               valid_values=True)

    def test_validate_values_display(self):
        # Check that value is NOT within valid_values and report values
        msg = validators.validate_values(7, [4, 6],
                                         valid_values_display="[4, 6]")
        self.assertEqual("7 is not in [4, 6]", msg)

        # Check that value is NOT within valid_values and report values
        msg = validators.validate_values(7, (4, 6),
                                         valid_values_display="(4, 6)")
        self.assertEqual("7 is not in (4, 6)", msg)

        # Check values with a range function showing a custom string
        msg = validators.validate_values(8, range(8),
                                         valid_values_display="[0..7]")
        self.assertEqual("8 is not in [0..7]", msg)

        # Check that value is not within valid_values and custom string
{'fraction_non_alphanumeric': 0.0879808577213326, 'fraction_numerical': 0.009571139333701455, 'mean_word_length': 2.255542240862792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6430316', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2790, 'n_words': 761}