Merge "Update Image v2 model unittests"
This commit is contained in:
commit
62faa367ef
@ -81,7 +81,7 @@ class Image(AutoMarshallingModel):
|
|||||||
@return: True if Image objects are not equal, False otherwise
|
@return: True if Image objects are not equal, False otherwise
|
||||||
@rtype: bool
|
@rtype: bool
|
||||||
"""
|
"""
|
||||||
return not self == other
|
return not self.__eq__(other)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
values = []
|
values = []
|
||||||
@ -119,7 +119,7 @@ class Image(AutoMarshallingModel):
|
|||||||
'%Y-%m-%dT%H:%M:%SZ')
|
'%Y-%m-%dT%H:%M:%SZ')
|
||||||
for key in ['id', 'self', 'file']:
|
for key in ['id', 'self', 'file']:
|
||||||
json_dict['{0}_'.format(key)] = json_dict[key]
|
json_dict['{0}_'.format(key)] = json_dict[key]
|
||||||
del(json_dict[key])
|
del (json_dict[key])
|
||||||
|
|
||||||
return Image(**json_dict)
|
return Image(**json_dict)
|
||||||
|
|
||||||
@ -131,6 +131,10 @@ class Image(AutoMarshallingModel):
|
|||||||
if self.updated_at:
|
if self.updated_at:
|
||||||
obj_dict['updated_at'] = \
|
obj_dict['updated_at'] = \
|
||||||
self.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
|
self.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||||
|
if self.self_:
|
||||||
|
obj_dict['self'] = self.self_
|
||||||
|
if self.file_:
|
||||||
|
obj_dict['file'] = self.file_
|
||||||
|
|
||||||
obj_dict['id'] = self.id_
|
obj_dict['id'] = self.id_
|
||||||
obj_dict['name'] = self.name
|
obj_dict['name'] = self.name
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
from cloudcafe.images.common.types import ImageVisibility, \
|
from cloudcafe.images.common.types import ImageVisibility, \
|
||||||
ImageStatus, ImageContainerFormat, ImageDiskFormat
|
ImageStatus, ImageContainerFormat, ImageDiskFormat
|
||||||
from cloudcafe.images.v2_0.models.image import Image
|
from cloudcafe.images.v2.models.image import Image
|
||||||
|
|
||||||
|
|
||||||
class TestImage(TestCase):
|
class TestImage(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setup_class(cls):
|
def setup_class(cls):
|
||||||
cls.raw_image_str = open(os.path.join(
|
cls.raw_image_str = open(os.path.join(
|
||||||
@ -41,33 +38,33 @@ class TestImage(TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_positive_equality(self):
|
def test_positive_equality(self):
|
||||||
duplicate_obj = deepcopy(self.image_obj)
|
assert self.image_obj == deepcopy(self.image_obj)
|
||||||
assert duplicate_obj == self.image_obj
|
|
||||||
|
|
||||||
def test_negative_equality(self):
|
def test_negative_equality(self):
|
||||||
different_obj = deepcopy(self.image_obj)
|
different_obj = deepcopy(self.image_obj)
|
||||||
different_obj.created_at = datetime.now()
|
|
||||||
different_obj.updated_at = datetime.now()
|
|
||||||
different_obj.name = 'cirros-fake'
|
different_obj.name = 'cirros-fake'
|
||||||
|
|
||||||
assert different_obj != self.image_obj
|
assert self.image_obj != different_obj
|
||||||
|
|
||||||
def test_string_representation(self):
|
|
||||||
regex = """(?x)\[([\w\d]+:\s(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z|
|
|
||||||
[\w\d\-\.\/\[\]]*)(,\s){0,1})*\]"""
|
|
||||||
matches = re.match(regex, self.image_obj.__repr__())
|
|
||||||
assert matches is not None
|
|
||||||
|
|
||||||
def test_deserialization_from_json(self):
|
def test_deserialization_from_json(self):
|
||||||
deserialized_obj = Image._json_to_obj(self.raw_image_str)
|
deserialized_obj = Image._json_to_obj(self.raw_image_str)
|
||||||
|
|
||||||
print self.image_obj
|
|
||||||
print deserialized_obj
|
|
||||||
assert self.image_obj == deserialized_obj
|
assert self.image_obj == deserialized_obj
|
||||||
|
|
||||||
|
def test_dict_to_obj(self):
|
||||||
|
obj_dict = deepcopy(self.image_obj.__dict__)
|
||||||
|
obj_dict['created_at'] = '2013-05-22T14:24:36Z'
|
||||||
|
obj_dict['updated_at'] = '2013-05-22T14:24:36Z'
|
||||||
|
obj_dict['id'] = obj_dict.pop('id_')
|
||||||
|
obj_dict['self'] = obj_dict.pop('self_')
|
||||||
|
obj_dict['file'] = obj_dict.pop('file_')
|
||||||
|
image_obj = Image._dict_to_obj(obj_dict)
|
||||||
|
|
||||||
|
assert self.image_obj == image_obj
|
||||||
|
|
||||||
def test_serialization_to_json(self):
|
def test_serialization_to_json(self):
|
||||||
serialized_obj = self.image_obj._obj_to_json()
|
serialized_obj = self.image_obj._obj_to_json()
|
||||||
# we do this to overcome the property ordering:
|
# we do this to overcome the property ordering:
|
||||||
deserialized_obj = Image._json_to_obj(serialized_obj)
|
deserialized_obj = Image._json_to_obj(serialized_obj)
|
||||||
|
|
||||||
assert self.image_obj == deserialized_obj
|
assert set(self.image_obj.__dict__) == set(deserialized_obj.__dict__)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user