Fix unittests' errors
authorLingchaox Xin <lingchaox.xin@intel.com>
Thu, 30 May 2013 03:19:09 +0000 (11:19 +0800)
committerLingchaox Xin <lingchaox.xin@intel.com>
Fri, 31 May 2013 07:49:51 +0000 (15:49 +0800)
Change-Id: Id18a6f378575ca9a2434a98c716de2457c92719a

common/builddata.py
common/imagedata.py
tests/test_backenddb.py
tests/test_imagedata.py

index aa73238..59b1267 100644 (file)
@@ -110,7 +110,7 @@ class BuildData(object):
             target = self.targets[name]
             content += '<buildtarget name="%s">' % name
 
-            if 'buildconf' in self.targets.keys():
+            if 'buildconf' in target.keys():
                 # buildconf
                 content += '<buildconf>'
                 buildconf = target['buildconf']
index 6055445..b428743 100644 (file)
@@ -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 = '<?xml version="1.0" ?><images>'
+        content = '<?xml version="1.0" ?><image-configs>'
 
         for image in self.images.itervalues():
-            content += '<image>'
+            content += '<config>'
             for field, value in image.iteritems():
                 content += '<%s>%s</%s>' % (field, value, field)
-            content += '</image>'
-        content += '</images>'
+            content += '</config>'
+        content += '</image-configs>'
 
         # make it human readable
         if hreadable:
index f659b50..096b88a 100644 (file)
@@ -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):
index b52b90f..be6b77b 100644 (file)
 """Unit tests for class ImageData"""
 
 import os
+import shutil
 import unittest
 
 from common.imagedata import ImageData, ImageDataError
 
 
 TEST_XML = '''<?xml version="1.0" ?>
-<images>
-  <image>
+<image-configs>
+  <config>
     <name>XX-00</name>
     <description>Image for device XX-00</description>
     <path>images/XX-00/</path>
     <ks>images/XX-00/xx-00.ks</ks>
     <buildtarget>arm</buildtarget>
     <arch>armv7l</arch>
-  </image>
-  <image>
+  </config>
+  <config>
     <name>YY-01</name>
     <description>Image for device YY-01</description>
     <path>images/YY-01/</path>
     <ks>images/YY-01/yy-01.ks</ks>
     <buildtarget>standard</buildtarget>
     <arch>ia32</arch>
-  </image>
-</images>
+  </config>
+</image-configs>
 '''
 
 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, '<test/>')
 
-
-    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)