aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2010-05-26 21:20:06 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2010-05-26 21:20:06 -0300
commit3acfdfe0a418436343ec9f189ad17e034ebdc652 (patch)
tree81d946ae0a54dec8c4362a9ddb72ebc608eb5fcb
parentsmall improvements in the tests for g_octave/description.py (tests/test_descr... (diff)
downloadg-octave-3acfdfe0a418436343ec9f189ad17e034ebdc652.tar.gz
g-octave-3acfdfe0a418436343ec9f189ad17e034ebdc652.tar.bz2
g-octave-3acfdfe0a418436343ec9f189ad17e034ebdc652.zip
Added tests to g_octave/description_tree.py (tests/test_description_tree.py)
-rw-r--r--g_octave/description_tree.py26
-rw-r--r--tests/test_description_tree.py122
2 files changed, 142 insertions, 6 deletions
diff --git a/g_octave/description_tree.py b/g_octave/description_tree.py
index 10ee15f..7f84974 100644
--- a/g_octave/description_tree.py
+++ b/g_octave/description_tree.py
@@ -1,6 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+"""
+ description_tree.py
+ ~~~~~~~~~~~~~~~~~~~
+
+ This module implements a Python object with the content of a directory
+ tree with DESCRIPTION files. The object contains *g_octave.Description*
+ objects for each DESCRIPTION file.
+
+ :copyright: (c) 2009-2010 by Rafael Goncalves Martins
+ :license: GPL-2, see LICENSE for more details.
+"""
+
__all__ = ['DescriptionTree']
from config import Config
@@ -13,17 +25,19 @@ import os
class DescriptionTree(object):
- def __init__(self):
+ def __init__(self, db_path=None):
self.pkg_list = {}
- self.__db_path = os.path.join(conf.db, 'octave-forge')
+ # external db_path used by tests
+ self._db_path = db_path is not None and db_path or \
+ os.path.join(conf.db, 'octave-forge')
- if not os.path.isdir(self.__db_path):
- raise DescriptionTreeException('Invalid db: %s' % self.__db_path)
+ if not os.path.isdir(self._db_path):
+ raise DescriptionTreeException('Invalid db: %s' % self._db_path)
for cat in [i.strip() for i in conf.categories.split(',')]:
- catdir = os.path.join(self.__db_path, cat)
+ catdir = os.path.join(self._db_path, cat)
if os.path.isdir(catdir):
self.pkg_list[cat] = []
pkgs = os.listdir(catdir)
@@ -51,7 +65,7 @@ class DescriptionTree(object):
for pkg in self.pkg_list[cat]:
if pkg['name'] == name and pkg['version'] == version:
pkgfile = os.path.join(
- self.__db_path,
+ self._db_path,
cat,
'%s-%s' % (pkg['name'], pkg['version']),
'DESCRIPTION'
diff --git a/tests/test_description_tree.py b/tests/test_description_tree.py
new file mode 100644
index 0000000..0eb305c
--- /dev/null
+++ b/tests/test_description_tree.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+ test_description_tree.py
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+
+ test suite for the *g_octave.description_tree* module
+
+ :copyright: (c) 2010 by Rafael Goncalves Martins
+ :license: GPL-2, see LICENSE for more details.
+"""
+
+import os
+import shutil
+import tempfile
+import unittest
+
+from g_octave import description, description_tree
+
+
+class TestDescriptionTree(unittest.TestCase):
+
+ _packages = [
+ ('main', 'pkg1', '1.0'),
+ ('main', 'pkg2', '0.1'),
+ ('main', 'pkg2', '0.2'),
+ ('extra', 'pkg1', '1.1'),
+ ('language', 'lang', '0.1'),
+ ]
+
+ def setUp(self):
+ description_file = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)), 'DESCRIPTION'
+ )
+
+ # creating a temporary DESCRIPTION's tree
+ self._tree_dir = tempfile.mkdtemp()
+ for cat, pkg, ver in self._packages:
+ temp_path = os.path.join(self._tree_dir, cat, pkg+'-'+ver)
+ os.makedirs(temp_path)
+ shutil.copy(
+ description_file,
+ os.path.join(temp_path, 'DESCRIPTION')
+ )
+ self._tree = description_tree.DescriptionTree(self._tree_dir)
+
+ def test_temptree(self):
+ for cat, pkg, ver in self._packages:
+ temp_file = os.path.join(
+ self._tree_dir,
+ cat, pkg+'-'+ver,
+ 'DESCRIPTION'
+ )
+ self.assertTrue(os.path.exists(temp_file))
+
+ def test_package_versions(self):
+ versions = {
+ 'pkg1': ['1.0', '1.1'],
+ 'pkg2': ['0.1', '0.2'],
+ 'lang': ['0.1'],
+ }
+ for pkg in versions:
+ ver = self._tree.package_versions(pkg)
+ ver.sort()
+ versions[pkg].sort()
+ self.assertEqual(versions[pkg], ver)
+
+ def test_latest_version(self):
+ versions = {
+ 'pkg1': '1.1',
+ 'pkg2': '0.2',
+ 'lang': '0.1',
+ }
+ for pkg in versions:
+ self.assertEqual(
+ versions[pkg],
+ self._tree.latest_version(pkg)
+ )
+
+ def test_version_compare(self):
+ # TODO: cover a better range of versions
+ versions = [
+ # ((version1, version2), latest_version)
+ (('1', '2'), '2'),
+ (('0.1', '1'), '1'),
+ (('0.1', '0.2'), '0.2'),
+ (('0.0.1', '1'), '1'),
+ (('0.0.1', '0.1'), '0.1'),
+ (('0.0.1', '0.0.2'), '0.0.2'),
+ (('2', '1'), '2'),
+ (('1', '0.1'), '1'),
+ (('0.2', '0.1'), '0.2'),
+ (('1', '0.0.1'), '1'),
+ (('0.1', '0.0.1'), '0.1'),
+ (('0.0.2', '0.0.1'), '0.0.2'),
+ ]
+ for ver, latest in versions:
+ self.assertEqual(self._tree.version_compare(ver), latest)
+
+ def test_description_files(self):
+ for cat, pkg, ver in self._packages:
+ self.assertTrue(
+ isinstance(
+ self._tree[pkg+'-'+ver],
+ description.Description
+ )
+ )
+
+ def tearDown(self):
+ # removing the temp tree
+ shutil.rmtree(self._tree_dir)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(TestDescriptionTree('test_temptree'))
+ suite.addTest(TestDescriptionTree('test_package_versions'))
+ suite.addTest(TestDescriptionTree('test_latest_version'))
+ suite.addTest(TestDescriptionTree('test_version_compare'))
+ suite.addTest(TestDescriptionTree('test_description_files'))
+ return suite