Add unittests
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 15 Dec 2011 12:19:36 +0000 (13:19 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 15 Dec 2011 12:20:25 +0000 (13:20 +0100)
.gitignore
unittests/info-4.13a-10.fc14.i686.rpm [new file with mode: 0644]
unittests/run_tests.py [new file with mode: 0755]
unittests/test_xml_dump_filelists.py [new file with mode: 0644]
unittests/test_xml_dump_other.py [new file with mode: 0644]
unittests/test_xml_dump_primary.py [new file with mode: 0644]

index 99145b6..cd1529e 100644 (file)
@@ -1,6 +1,5 @@
 *.o
 *.so
-*.py
 *.pyc
 *_wrap.c
 snippets/
diff --git a/unittests/info-4.13a-10.fc14.i686.rpm b/unittests/info-4.13a-10.fc14.i686.rpm
new file mode 100644 (file)
index 0000000..769a791
Binary files /dev/null and b/unittests/info-4.13a-10.fc14.i686.rpm differ
diff --git a/unittests/run_tests.py b/unittests/run_tests.py
new file mode 100755 (executable)
index 0000000..2e7be1a
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""
+Run tests in current directory.
+(This file is 'stolen' from kobo)
+"""
+
+import os
+import sys
+
+
+# prepare environment for tests
+PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+os.environ["PYTHONPATH"] = PROJECT_DIR
+sys.path.insert(0, PROJECT_DIR)
+
+
+from kobo.shortcuts import run
+
+
+if __name__ == '__main__':
+    failed = False
+    for test in sorted(os.listdir(os.path.dirname(__file__))):
+        # run all tests that match the 'test_*.py" pattern
+        if not test.startswith("test_"):
+            continue
+        if not test.endswith(".py"):
+            continue
+
+        print "Executing tests in %-40s" % test,
+        retcode, output = run("python %s" % test, can_fail=True)
+
+        if retcode == 0:
+            print "[   OK   ]"
+            print output
+        else:
+            failed = True
+            print "[ FAILED ]"
+            print output
+
+    if failed:
+        sys.exit(1)
+    sys.exit(0)
diff --git a/unittests/test_xml_dump_filelists.py b/unittests/test_xml_dump_filelists.py
new file mode 100644 (file)
index 0000000..1a9905e
--- /dev/null
@@ -0,0 +1,94 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+
+import os
+import os.path
+import unittest
+import run_tests # set sys.path
+
+import xml_dump
+from repopackage import RepoPackage
+from pprint import pprint
+
+RPM = "info-4.13a-10.fc14.i686.rpm"
+
+
+OUTPUT = """<package pkgid="2d27dabeda8db51fad63073dc442a859c9dcbf8b2526d9fc4a4b3037df27df39" name="info" arch="i686"><version epoch="0" ver="4.13a" rel="10.fc14"/><file>/sbin/install-info</file><file>/usr/bin/info</file><file>/usr/bin/infokey</file><file type="dir">/usr/share/doc/info-4.13a</file><file>/usr/share/doc/info-4.13a/COPYING</file><file>/usr/share/info/dir</file><file>/usr/share/info/info-stnd.info.gz</file><file>/usr/share/info/info.info.gz</file><file>/usr/share/man/man1/info.1.gz</file><file>/usr/share/man/man1/infokey.1.gz</file><file>/usr/share/man/man1/install-info.1.gz</file><file>/usr/share/man/man5/info.5.gz</file></package>
+"""
+
+EMPTY_DICT_OUTPUT = """<package pkgid="" name="" arch=""><version epoch="" ver="" rel=""/></package>
+"""
+
+PARTIAL_OK_OUTPUT = """<package pkgid="" name="" arch=""><version epoch="" ver="" rel=""/><file>/file1</file><file>/file2</file></package>
+"""
+
+WRONG_NUM_VALUES_OUTPUT = """<package pkgid="" name="" arch=""><version epoch="" ver="" rel=""/></package>
+"""
+
+
+class TestXmlDumpFilelists(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    def test_dump(self):
+        pkg = RepoPackage(RPM)
+        pkg_dict = pkg.get_pkg_data()
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(OUTPUT, xml)
+
+    def test_dump_with_empty_input(self):
+        pkg_dict = {}
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_1(self):
+        pkg_dict = {'name': 123}
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_2(self):
+        pkg_dict = {
+            'size_package': "foo",
+            "size_installed": "foo",
+            "size_archive": "foo",
+            "time_file": "foo",
+            "time_build": "foo",
+            "rpm_header_start": "foo",
+            "rpm_header_end": "foo",
+        }
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(WRONG_NUM_VALUES_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_3(self):
+        pkg_dict = {
+            "requires": 123,
+            "provides": 456,
+            "conflicts": 789,
+            "oboletes": 000,
+        }
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_4(self):
+        pkg_dict = {
+            "files": 123,
+            "changelogs": 456,
+        }
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_partialy_ok_input_1(self):
+        pkg_dict = {
+            "files": [('/file1', 'file'), ('/file2', ''), ('/file3',), (), [], [1,2]],
+        }
+        xml = xml_dump.xml_dump_filelists(pkg_dict, None)
+        self.assertEqual(PARTIAL_OK_OUTPUT, xml)
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/unittests/test_xml_dump_other.py b/unittests/test_xml_dump_other.py
new file mode 100644 (file)
index 0000000..f0731df
--- /dev/null
@@ -0,0 +1,121 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+
+import os
+import os.path
+import unittest
+import run_tests # set sys.path
+
+import xml_dump
+from repopackage import RepoPackage
+from pprint import pprint
+
+RPM = "info-4.13a-10.fc14.i686.rpm"
+
+
+OUTPUT = """<package pkgid="2d27dabeda8db51fad63073dc442a859c9dcbf8b2526d9fc4a4b3037df27df39" name="info" arch="i686"><version epoch="0" ver="4.13a" rel="10.fc14"/><changelog author="Vitezslav Crhonek &lt;vcrhonek@redhat.com&gt; - 4.13a-10" date="1283256000">- Fix info crash when using index in help window
+  Resolves: #579263</changelog><changelog author="Jan Gorig &lt;jgorig@redhat.com&gt; - 4.13a-9" date="1263211200">- Fix PowerPC return code bug #531349</changelog><changelog author="Vitezslav Crhonek &lt;vcrhonek@redhat.com&gt; - 4.13a-8" date="1260792000">- Fix memory allocation bug when using old-style --section &quot;Foo&quot; arguments</changelog><changelog author="Vitezslav Crhonek &lt;vcrhonek@redhat.com&gt; - 4.13a-7" date="1251892800">- Fix errors installing texinfo/info with --excludedocs
+  Resolves: #515909
+  Resolves: #515938</changelog><changelog author="Ville Skyttä &lt;ville.skytta@iki.fi&gt; - 4.13a-6" date="1250078400">- Use lzma compressed upstream tarball.</changelog><changelog author="Vitezslav Crhonek &lt;vcrhonek@redhat.com&gt; - 4.13a-5" date="1249473600">- Fix changelog entry and rebuild</changelog><changelog author="Vitezslav Crhonek &lt;vcrhonek@redhat.com&gt; - 4.13a-4" date="1249387200">- Fix data types (fix by Ralf Corsepius)
+  Resolves: #515402</changelog><changelog author="Fedora Release Engineering &lt;rel-eng@lists.fedoraproject.org&gt; - 4.13a-3" date="1248609600">- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild</changelog><changelog author="Fedora Release Engineering &lt;rel-eng@lists.fedoraproject.org&gt; - 4.13a-2" date="1235563200">- Rebuilt for https://fedoraproject.org/wiki/Fedora_11_Mass_Rebuild</changelog><changelog author="Vitezslav Crhonek &lt;vcrhonek@redhat.com&gt; - 4.13-1" date="1227182400">- Update to texinfo-4.13a
+  Resolves: #471194
+  Resolves: #208511</changelog></package>
+"""
+
+EMPTY_DICT_OUTPUT = """<package pkgid="" name="" arch=""><version epoch="" ver="" rel=""/></package>
+"""
+
+WRONG_NUM_VALUES_OUTPUT = """<package pkgid="" name="" arch=""><version epoch="" ver="" rel=""/></package>
+"""
+
+PARTIALY_RIGHT_OUTPUT = """<package pkgid="" name="" arch=""><version epoch="" ver="" rel=""/><changelog author="foo" date="123456">foo</changelog></package>
+"""
+
+
+class TestXmlDumpPrimary(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    def test_dump(self):
+        pkg = RepoPackage(RPM)
+        pkg_dict = pkg.get_pkg_data()
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(OUTPUT, xml)
+
+    def test_dump_with_empty_input(self):
+        pkg_dict = {}
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_1(self):
+        pkg_dict = {'name': 123}
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_2(self):
+        pkg_dict = {
+            'size_package': "foo",
+            "size_installed": "foo",
+            "size_archive": "foo",
+            "time_file": "foo",
+            "time_build": "foo",
+            "rpm_header_start": "foo",
+            "rpm_header_end": "foo",
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(WRONG_NUM_VALUES_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_3(self):
+        pkg_dict = {
+            "requires": 123,
+            "provides": 456,
+            "conflicts": 789,
+            "oboletes": 000,
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_4(self):
+        pkg_dict = {
+            "files": 123,
+            "changelogs": 456,
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_5(self):
+        pkg_dict = {
+            "changelogs": [(123, 123, 123)],
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_6(self):
+        pkg_dict = {
+            "changelogs": [("foo", "foo", "foo")],
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_7(self):
+        pkg_dict = {
+            "changelogs": [(123456, "foo", "foo"), ("foo", "foo", "foo")],
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(PARTIALY_RIGHT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_8(self):
+        pkg_dict = {
+            "changelogs": [(123456L, "foo", "foo"), ("foo", "foo", "foo")],
+        }
+        xml = xml_dump.xml_dump_other(pkg_dict, None)
+        self.assertEqual(PARTIALY_RIGHT_OUTPUT, xml)
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/unittests/test_xml_dump_primary.py b/unittests/test_xml_dump_primary.py
new file mode 100644 (file)
index 0000000..e7f2371
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+
+import os
+import os.path
+import unittest
+import run_tests # set sys.path
+
+import xml_dump
+from repopackage import RepoPackage
+from pprint import pprint
+
+RPM = "info-4.13a-10.fc14.i686.rpm"
+
+
+OUTPUT = """<package type="rpm"><name>info</name><arch>i686</arch><version epoch="0" ver="4.13a" rel="10.fc14"/><checksum pkgid="YES" type="sha256">2d27dabeda8db51fad63073dc442a859c9dcbf8b2526d9fc4a4b3037df27df39</checksum><summary>A stand-alone TTY-based reader for GNU texinfo documentation</summary><description>The GNU project uses the texinfo file format for much of its
+documentation. The info package provides a standalone TTY-based
+browser program for viewing texinfo files.</description><packager>Fedora Project</packager><url>http://www.gnu.org/software/texinfo/</url><time file="1315916291" build="1283257696"/><size package="176520" installed="314734" archive="316556"/><location href="info-4.13a-10.fc14.i686.rpm"/><format><rpm:license>GPLv3+</rpm:license><rpm:vendor>Fedora Project</rpm:vendor><rpm:group>System Environment/Base</rpm:group><rpm:buildhost>x86-04.phx2.fedoraproject.org</rpm:buildhost><rpm:sourcerpm>texinfo-4.13a-10.fc14.src.rpm</rpm:sourcerpm><rpm:header-range start="1384" end="16796"/><rpm:provides><rpm:entry name="config(info)" flags="EQ" epoch="0" ver="4.13a" rel="10.fc14"/><rpm:entry name="info" flags="EQ" epoch="0" ver="4.13a" rel="10.fc14"/><rpm:entry name="info(x86-32)" flags="EQ" epoch="0" ver="4.13a" rel="10.fc14"/></rpm:provides><rpm:requires><rpm:entry name="rpmlib(CompressedFileNames)" flags="LE" epoch="0" ver="3.0.4" rel="1" pre="0"/><rpm:entry name="rpmlib(PayloadFilesHavePrefix)" flags="LE" epoch="0" ver="4.0" rel="1" pre="0"/><rpm:entry name="rpmlib(FileDigests)" flags="LE" epoch="0" ver="4.6.0" rel="1" pre="0"/><rpm:entry name="rpmlib(PayloadIsXz)" flags="LE" epoch="0" ver="5.2" rel="1" pre="0"/></rpm:requires><file>/sbin/install-info</file><file>/usr/bin/info</file><file>/usr/bin/infokey</file></format></package>
+"""
+
+EMPTY_DICT_OUTPUT = """<package type="rpm"><name></name><arch></arch><version epoch="" ver="" rel=""/><checksum pkgid="YES" type=""></checksum><summary></summary><description></description><packager></packager><url></url><time file="0" build="0"/><size package="0" installed="0" archive="0"/><location href=""/><format><rpm:license></rpm:license><rpm:vendor></rpm:vendor><rpm:group></rpm:group><rpm:buildhost></rpm:buildhost><rpm:sourcerpm></rpm:sourcerpm><rpm:header-range start="0" end="0"/></format></package>
+"""
+
+PARTIALY_OK_OUTPUT = """<package type="rpm"><name></name><arch></arch><version epoch="" ver="" rel=""/><checksum pkgid="YES" type=""></checksum><summary></summary><description></description><packager></packager><url></url><time file="123" build="456"/><size package="139" installed="146" archive="143"/><location href=""/><format><rpm:license></rpm:license><rpm:vendor></rpm:vendor><rpm:group></rpm:group><rpm:buildhost></rpm:buildhost><rpm:sourcerpm></rpm:sourcerpm><rpm:header-range start="789" end="0"/></format></package>
+"""
+
+WRONG_NUM_VALUES_OUTPUT = """<package type="rpm"><name></name><arch></arch><version epoch="" ver="" rel=""/><checksum pkgid="YES" type=""></checksum><summary></summary><description></description><packager></packager><url></url><time file="-1" build="-1"/><size package="-1" installed="-1" archive="-1"/><location href=""/><format><rpm:license></rpm:license><rpm:vendor></rpm:vendor><rpm:group></rpm:group><rpm:buildhost></rpm:buildhost><rpm:sourcerpm></rpm:sourcerpm><rpm:header-range start="-1" end="-1"/></format></package>
+"""
+
+
+class TestXmlDumpPrimary(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    def test_dump(self):
+        pkg = RepoPackage(RPM)
+        pkg_dict = pkg.get_pkg_data()
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(OUTPUT, xml)
+
+    def test_dump_with_empty_input(self):
+        pkg_dict = {}
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_partialy_ok_input_1(self):
+        pkg_dict = {
+            'size_package': 139,
+            "size_installed": 146,
+            "size_archive": 143,
+            "time_file": 123,
+            "time_build": 456,
+            "rpm_header_start": 789,
+            "rpm_header_end": 000,
+        }
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(PARTIALY_OK_OUTPUT, xml)
+
+    def test_dump_with_partialy_ok_input_2(self):
+        pkg_dict = {
+            'size_package': 139L,
+            "size_installed": 146L,
+            "size_archive": 143L,
+            "time_file": 123L,
+            "time_build": 456L,
+            "rpm_header_start": 789L,
+            "rpm_header_end": 000L,
+        }
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(PARTIALY_OK_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_1(self):
+        pkg_dict = {'name': 123}
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_2(self):
+        pkg_dict = {
+            'size_package': "foo",
+            "size_installed": "foo",
+            "size_archive": "foo",
+            "time_file": "foo",
+            "time_build": "foo",
+            "rpm_header_start": "foo",
+            "rpm_header_end": "foo",
+        }
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(WRONG_NUM_VALUES_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_3(self):
+        pkg_dict = {
+            "requires": 123,
+            "provides": 456,
+            "conflicts": 789,
+            "oboletes": 000,
+        }
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+    def test_dump_with_wrong_input_4(self):
+        pkg_dict = {
+            "files": 123,
+            "changelogs": 456,
+        }
+        xml = xml_dump.xml_dump_primary(pkg_dict, None)
+        self.assertEqual(EMPTY_DICT_OUTPUT, xml)
+
+
+if __name__ == "__main__":
+    unittest.main()