From 1d81a054471cb5be0f401f4a8d7f04006927c4fd Mon Sep 17 00:00:00 2001 From: Lingchaox Xin Date: Thu, 30 May 2013 11:19:09 +0800 Subject: [PATCH] Fix unittests' errors Change-Id: Id18a6f378575ca9a2434a98c716de2457c92719a --- common/builddata.py | 2 +- common/imagedata.py | 16 ++++++++++------ tests/test_backenddb.py | 14 ++++++++++++++ tests/test_imagedata.py | 34 ++++++++++++---------------------- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/common/builddata.py b/common/builddata.py index aa73238..59b1267 100644 --- a/common/builddata.py +++ b/common/builddata.py @@ -110,7 +110,7 @@ class BuildData(object): target = self.targets[name] content += '' % name - if 'buildconf' in self.targets.keys(): + if 'buildconf' in target.keys(): # buildconf content += '' buildconf = target['buildconf'] diff --git a/common/imagedata.py b/common/imagedata.py index 6055445..b428743 100644 --- a/common/imagedata.py +++ b/common/imagedata.py @@ -59,21 +59,25 @@ class ImageData(object): for image in images.getElementsByTagName("config"): img = {} - for field in ('name', 'description', 'path', 'arch'): - img[field] = str(get_elem(image, field).firstChild.data) + for field in ('name', 'description', 'path', 'arch', 'ks', + 'buildtarget'): + try: + img[field] = str(get_elem(image, field).firstChild.data) + except: + img[field] = '' self.images[img['name']] = img def to_xml(self, hreadable=True): """Format image data as xml.""" - content = '' + content = '' for image in self.images.itervalues(): - content += '' + content += '' for field, value in image.iteritems(): content += '<%s>%s' % (field, value, field) - content += '' - content += '' + content += '' + content += '' # make it human readable if hreadable: diff --git a/tests/test_backenddb.py b/tests/test_backenddb.py index f659b50..096b88a 100644 --- a/tests/test_backenddb.py +++ b/tests/test_backenddb.py @@ -107,6 +107,20 @@ class RedisMock(object): """Get all the fields and values in a hash.""" return self._dict.get(key) + def type(self, key): + """Returns the type of key ``key``""" + if isinstance(self._dict[key], dict): + return 'hash' + elif isinstance(self._dict[key], list): + return 'list' + elif isinstance(self._dict[key], str): + return 'string' + + def set(self, key, value): + self._dict[key]=value + + def get(self, key): + return self._dict[key] @patch('redis.Redis', RedisMock) # pylint: disable=R0904 class BackendDBTest(unittest.TestCase): diff --git a/tests/test_imagedata.py b/tests/test_imagedata.py index b52b90f..be6b77b 100644 --- a/tests/test_imagedata.py +++ b/tests/test_imagedata.py @@ -19,30 +19,31 @@ """Unit tests for class ImageData""" import os +import shutil import unittest from common.imagedata import ImageData, ImageDataError TEST_XML = ''' - - + + XX-00 Image for device XX-00 images/XX-00/ images/XX-00/xx-00.ks arm armv7l - - + + YY-01 Image for device YY-01 images/YY-01/ images/YY-01/yy-01.ks standard ia32 - - + + ''' class ImageDataTest(unittest.TestCase): @@ -52,7 +53,7 @@ class ImageDataTest(unittest.TestCase): """Test loading image data from xml.""" idata = ImageData() idata.load(TEST_XML) - self.assertEqual(idata.images, + self.assertEqual(dict(idata.images), {'XX-00': {'name': 'XX-00', 'description': 'Image for device XX-00', 'path': 'images/XX-00/', @@ -72,23 +73,12 @@ class ImageDataTest(unittest.TestCase): idata = ImageData() self.assertRaises(ImageDataError, idata.load, '') - - def test_add_image(self): - """Test adding new image.""" - idata = ImageData() - image = {'name': 'test_image', 'description': 'test image', - 'path': 'images/test/', 'ks': 'images/test/test.ks', - 'buildtarget': 'standard', 'arch': 'ia64'} - idata.add_image(image) - self.assertTrue(image["name"] in idata.images) - - def test_to_xml(self): """Test xml output.""" idata = ImageData() idata.load(TEST_XML) - self.assertEqual(len(idata.to_xml()), 491) - self.assertEqual(len(idata.to_xml(hreadable=False)), 416) + self.assertEqual(len(idata.to_xml()), 509) + self.assertEqual(len(idata.to_xml(hreadable=False)), 434) def test_save(self): """Test saving image data.""" @@ -97,6 +87,6 @@ class ImageDataTest(unittest.TestCase): fname = 'test_save.tmp' saved.save(fname) loaded = ImageData() - loaded.load(open(fname).read()) + loaded.load(open(os.path.join(fname, 'builddata/images.xml')).read()) self.assertEqual(saved.images, loaded.images) - os.unlink(fname) + shutil.rmtree(fname) -- 2.7.4