| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import jsonpatch |
| 17 |
import testtools |
| 18 |
import warlock |
| 19 |
|
| 20 |
from glanceclient.tests import utils |
| 21 |
from glanceclient.v2 import schemas |
| 22 |
|
| 23 |
|
| 24 |
fixtures = { |
| 25 |
'/v2/schemas': { |
| 26 |
'GET': ( |
| 27 |
{}, |
| 28 |
{ |
| 29 |
'image': '/v2/schemas/image', |
| 30 |
'access': '/v2/schemas/image/access', |
| 31 |
}, |
| 32 |
), |
| 33 |
}, |
| 34 |
'/v2/schemas/image': { |
| 35 |
'GET': ( |
| 36 |
{}, |
| 37 |
{ |
| 38 |
'name': 'image', |
| 39 |
'properties': { |
| 40 |
'name': {'type': 'string', |
| 41 |
'description': 'Name of image'}, |
| 42 |
'tags': {'type': 'array'} |
| 43 |
}, |
| 44 |
|
| 45 |
}, |
| 46 |
), |
| 47 |
}, |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
_SCHEMA = schemas.Schema({ |
| 52 |
'name': 'image', |
| 53 |
'properties': { |
| 54 |
'name': {'type': 'string'}, |
| 55 |
'color': {'type': 'string'}, |
| 56 |
'shape': {'type': 'string', 'is_base': False}, |
| 57 |
'tags': {'type': 'array'} |
| 58 |
}, |
| 59 |
}) |
| 60 |
|
| 61 |
|
| 62 |
def compare_json_patches(a, b): |
| 63 |
"""Return 0 if a and b describe the same JSON patch.""" |
| 64 |
return (jsonpatch.JsonPatch.from_string(a) == |
| 65 |
jsonpatch.JsonPatch.from_string(b)) |
| 66 |
|
| 67 |
|
| 68 |
class TestSchemaProperty(testtools.TestCase): |
| 69 |
def test_property_minimum(self): |
| 70 |
prop = schemas.SchemaProperty('size') |
| 71 |
self.assertEqual('size', prop.name) |
| 72 |
|
| 73 |
def test_property_description(self): |
| 74 |
prop = schemas.SchemaProperty('size', description='some quantity') |
| 75 |
self.assertEqual('size', prop.name) |
| 76 |
self.assertEqual('some quantity', prop.description) |
| 77 |
|
| 78 |
def test_property_is_base(self): |
| 79 |
prop1 = schemas.SchemaProperty('name') |
| 80 |
prop2 = schemas.SchemaProperty('foo', is_base=False) |
| 81 |
prop3 = schemas.SchemaProperty('foo', is_base=True) |
| 82 |
self.assertTrue(prop1.is_base) |
| 83 |
self.assertFalse(prop2.is_base) |
| 84 |
self.assertTrue(prop3.is_base) |
| 85 |
|
| 86 |
|
| 87 |
class TestSchema(testtools.TestCase): |
| 88 |
def test_schema_minimum(self): |
| 89 |
raw_schema = {'name': 'Country', 'properties': {}} |
| 90 |
schema = schemas.Schema(raw_schema) |
| 91 |
self.assertEqual('Country', schema.name) |
| 92 |
self.assertEqual([], schema.properties) |
| 93 |
|
| 94 |
def test_schema_with_property(self): |
| 95 |
raw_schema = {'name': 'Country', 'properties': {'size': {}}} |
| 96 |
schema = schemas.Schema(raw_schema) |
| 97 |
self.assertEqual('Country', schema.name) |
| 98 |
self.assertEqual(['size'], [p.name for p in schema.properties]) |
| 99 |
|
| 100 |
def test_raw(self): |
| 101 |
raw_schema = {'name': 'Country', 'properties': {}} |
| 102 |
schema = schemas.Schema(raw_schema) |
| 103 |
self.assertEqual(raw_schema, schema.raw()) |
| 104 |
|
| 105 |
def test_property_is_base(self): |
| 106 |
raw_schema = {'name': 'Country', |
| 107 |
'properties': { |
| 108 |
'size': {}, |
| 109 |
'population': {'is_base': False}}} |
| 110 |
schema = schemas.Schema(raw_schema) |
| 111 |
self.assertTrue(schema.is_base_property('size')) |
| 112 |
self.assertFalse(schema.is_base_property('population')) |
| 113 |
self.assertFalse(schema.is_base_property('foo')) |
| 114 |
|
| 115 |
|
| 116 |
class TestController(testtools.TestCase): |
| 117 |
def setUp(self): |
| 118 |
super(TestController, self).setUp() |
| 119 |
self.api = utils.FakeAPI(fixtures) |
| 120 |
self.controller = schemas.Controller(self.api) |
| 121 |
|
| 122 |
def test_get_schema(self): |
| 123 |
schema = self.controller.get('image') |
| 124 |
self.assertEqual('image', schema.name) |
| 125 |
self.assertEqual(set(['name', 'tags']), |
| 126 |
set([p.name for p in schema.properties])) |
| 127 |
|
| 128 |
|
| 129 |
class TestSchemaBasedModel(testtools.TestCase): |
| 130 |
def setUp(self): |
| 131 |
super(TestSchemaBasedModel, self).setUp() |
| 132 |
self.model = warlock.model_factory(_SCHEMA.raw(), |
| 133 |
base_class=schemas.SchemaBasedModel) |
| 134 |
|
| 135 |
def test_patch_should_replace_missing_core_properties(self): |
| 136 |
obj = { |
| 137 |
'name': 'fred' |
| 138 |
} |
| 139 |
|
| 140 |
original = self.model(obj) |
| 141 |
original['color'] = 'red' |
| 142 |
|
| 143 |
patch = original.patch |
| 144 |
expected = '[{"path": "/color", "value": "red", "op": "replace"}]' |
| 145 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 146 |
|
| 147 |
def test_patch_should_add_extra_properties(self): |
| 148 |
obj = { |
| 149 |
'name': 'fred', |
| 150 |
} |
| 151 |
|
| 152 |
original = self.model(obj) |
| 153 |
original['weight'] = '10' |
| 154 |
|
| 155 |
patch = original.patch |
| 156 |
expected = '[{"path": "/weight", "value": "10", "op": "add"}]' |
| 157 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 158 |
|
| 159 |
def test_patch_should_replace_extra_properties(self): |
| 160 |
obj = { |
| 161 |
'name': 'fred', |
| 162 |
'weight': '10' |
| 163 |
} |
| 164 |
|
| 165 |
original = self.model(obj) |
| 166 |
original['weight'] = '22' |
| 167 |
|
| 168 |
patch = original.patch |
| 169 |
expected = '[{"path": "/weight", "value": "22", "op": "replace"}]' |
| 170 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 171 |
|
| 172 |
def test_patch_should_remove_extra_properties(self): |
| 173 |
obj = { |
| 174 |
'name': 'fred', |
| 175 |
'weight': '10' |
| 176 |
} |
| 177 |
|
| 178 |
original = self.model(obj) |
| 179 |
del original['weight'] |
| 180 |
|
| 181 |
patch = original.patch |
| 182 |
expected = '[{"path": "/weight", "op": "remove"}]' |
| 183 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 184 |
|
| 185 |
def test_patch_should_remove_core_properties(self): |
| 186 |
obj = { |
| 187 |
'name': 'fred', |
| 188 |
'color': 'red' |
| 189 |
} |
| 190 |
|
| 191 |
original = self.model(obj) |
| 192 |
del original['color'] |
| 193 |
|
| 194 |
patch = original.patch |
| 195 |
expected = '[{"path": "/color", "op": "remove"}]' |
| 196 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 197 |
|
| 198 |
def test_patch_should_add_missing_custom_properties(self): |
| 199 |
obj = { |
| 200 |
'name': 'fred' |
| 201 |
} |
| 202 |
|
| 203 |
original = self.model(obj) |
| 204 |
original['shape'] = 'circle' |
| 205 |
|
| 206 |
patch = original.patch |
| 207 |
expected = '[{"path": "/shape", "value": "circle", "op": "add"}]' |
| 208 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 209 |
|
| 210 |
def test_patch_should_replace_custom_properties(self): |
| 211 |
obj = { |
| 212 |
'name': 'fred', |
| 213 |
'shape': 'circle' |
| 214 |
} |
| 215 |
|
| 216 |
original = self.model(obj) |
| 217 |
original['shape'] = 'square' |
| 218 |
|
| 219 |
patch = original.patch |
| 220 |
expected = '[{"path": "/shape", "value": "square", "op": "replace"}]' |
| 221 |
self.assertTrue(compare_json_patches(patch, expected)) |
| 222 |
|
| 223 |
def test_patch_should_replace_tags(self): |
| 224 |
obj = {'name': 'fred', } |
| 225 |
|
| 226 |
original = self.model(obj) |
| 227 |
original['tags'] = ['tag1', 'tag2'] |
| 228 |
|
| 229 |
patch = original.patch |
| 230 |
expected = '[{"path": "/tags", "value": ["tag1", "tag2"], ' \ |
| 231 |
'"op": "replace"}]' |
| 232 |
self.assertTrue(compare_json_patches(patch, expected)) |