Add unit tests for all generators

generate_json.py does not exist any longer. Using the checks there
and move them into test_negative_generators.py. From now on the checks
are usable for all generators (also valid_generator) using a base class.

In order to check for unknown types it was mandatory to change an
existing exception to a more detailed exception (TypeError).

Partially implements bp unit-tests

Change-Id: I6b5b66f8ae113c1aca471d8338334cd47ff45723
This commit is contained in:
Marc Koderer 2014-04-01 13:31:23 +02:00
parent ca19315fe8
commit f39fbed952
3 changed files with 74 additions and 59 deletions

View File

@ -123,7 +123,7 @@ class BasicGeneratorSet(object):
raise Exception("non-integer list types not supported")
result = []
if schema_type not in self.types_dict:
raise Exception("generator (%s) doesn't support type: %s"
raise TypeError("generator (%s) doesn't support type: %s"
% (self.__class__.__name__, schema_type))
for generator in self.types_dict[schema_type]:
ret = generator(schema)

View File

@ -1,57 +0,0 @@
# Copyright 2014 Deutsche Telekom AG
# All Rights Reserved.
#
# 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.
from tempest.common.generator import negative_generator
import tempest.test
class TestNegativeGenerator(tempest.test.BaseTestCase):
fake_input_str = {"type": "string",
"minLength": 2,
"maxLength": 8,
'results': {'gen_number': 404}}
fake_input_int = {"type": "integer",
"maximum": 255,
"minimum": 1}
fake_input_obj = {"type": "object",
"properties": {"minRam": {"type": "integer"},
"diskName": {"type": "string"},
"maxRam": {"type": "integer", }
}
}
def setUp(self):
super(TestNegativeGenerator, self).setUp()
self.negative = negative_generator.NegativeTestGenerator()
def _validate_result(self, data):
self.assertTrue(isinstance(data, list))
for t in data:
self.assertTrue(isinstance(t, tuple))
def test_generate_invalid_string(self):
result = self.negative.generate(self.fake_input_str)
self._validate_result(result)
def test_generate_invalid_integer(self):
result = self.negative.generate(self.fake_input_int)
self._validate_result(result)
def test_generate_invalid_obj(self):
result = self.negative.generate(self.fake_input_obj)
self._validate_result(result)

View File

@ -16,7 +16,9 @@
import jsonschema
import mock
import tempest.common.generator.base_generator as base_generator
from tempest.common.generator import base_generator
from tempest.common.generator import negative_generator
from tempest.common.generator import valid_generator
from tempest.tests import base
@ -79,3 +81,73 @@ class TestNegativeBasicGenerator(base.TestCase):
self.assertRaises(jsonschema.SchemaError,
self.generator.validate_schema,
self.invalid_json_schema_desc)
class BaseNegativeGenerator(object):
types = ['string', 'integer', 'object']
fake_input_str = {"type": "string",
"minLength": 2,
"maxLength": 8,
'results': {'gen_int': 404}}
fake_input_int = {"type": "integer",
"maximum": 255,
"minimum": 1}
fake_input_obj = {"type": "object",
"properties": {"minRam": {"type": "integer"},
"diskName": {"type": "string"},
"maxRam": {"type": "integer", }
}
}
unkown_type_schema = {
"type": "not_defined"
}
def _validate_result(self, data):
self.assertTrue(isinstance(data, list))
for t in data:
self.assertIsInstance(t, tuple)
self.assertEqual(3, len(t))
self.assertIsInstance(t[0], str)
def test_generate_string(self):
result = self.generator.generate(self.fake_input_str)
self._validate_result(result)
def test_generate_integer(self):
result = self.generator.generate(self.fake_input_int)
self._validate_result(result)
def test_generate_obj(self):
result = self.generator.generate(self.fake_input_obj)
self._validate_result(result)
def test_generator_mandatory_functions(self):
for data_type in self.types:
self.assertIn(data_type, self.generator.types_dict)
def test_generate_with_unknown_type(self):
self.assertRaises(TypeError, self.generator.generate,
self.unkown_type_schema)
class TestNegativeValidGenerator(base.TestCase, BaseNegativeGenerator):
def setUp(self):
super(TestNegativeValidGenerator, self).setUp()
self.generator = valid_generator.ValidTestGenerator()
def test_generate_valid(self):
result = self.generator.generate_valid(self.fake_input_obj)
self.assertIn("minRam", result)
self.assertIsInstance(result["minRam"], int)
self.assertIn("diskName", result)
self.assertIsInstance(result["diskName"], str)
class TestNegativeNegativeGenerator(base.TestCase, BaseNegativeGenerator):
def setUp(self):
super(TestNegativeNegativeGenerator, self).setUp()
self.generator = negative_generator.NegativeTestGenerator()