Commits

Gora Khargosh committed 3255009

Tests using unittest2 and coverage.

Signed-off-by: Gora Khargosh <gora.khargosh@gmail.com>

Comments (0)

Files changed (7)

+# .coveragerc to control coverage.py
+[run]
+branch = False
+
+source =
+    mom
+
+omit =
+    /*/tests/*
+
+[report]
 view-doc: doc
 	@bin/python -c "import webbrowser; webbrowser.open('docs/build/html/index.html')"
 
-test: docs
+test:
 	@bin/coverage erase
-	@bin/python-tests tests/run_tests.py
+	@bin/coverage run bin/unit2 discover
+	@bin/coverage report -m
 	@bin/coverage html -d htmlcov
 
 lint:
 recipe = zc.recipe.egg
 interpreter = python-tests
 eggs =
-    nose
     coverage
-    pythoscope
+    unittest2
     ${buildout:eggs}
 
 [docs]

nose.cfg

-[nosetests]
-verbosity=3
-with-doctest=1
-#with-color=1
-#with-noseexclude=1
-#exclude-dir-file=tests/nose-exclude.txt
     "pyasn1 >=0.0.13b",
 ]
 if sys.version_info < (2, 6, 0):
-    install_requires.append("simplejson >= 2.1.6")
+    install_requires.append("simplejson >=2.1.6")
 
 setup(
     name="mom",

tests/__init__.py

Empty file added.

tests/test_mom_builtins.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest2
+from unittest2 import TestCase as BaseTestCase
+
+from mom.security.random import generate_random_bytes
+from mom.builtins import \
+    is_unicode, \
+    is_bytes, \
+    is_bytes_or_unicode, \
+    to_utf8_if_unicode, \
+    to_unicode_if_bytes, \
+    unicode_to_utf8, \
+    bytes_to_unicode
+
+
+random_bytes = generate_random_bytes(100)
+utf8_bytes = '\xc2\xae'
+unicode_string = u'\u00ae'
+
+
+class Test_is_bytes(BaseTestCase):
+    def test_accepts_bytes(self):
+        self.assertTrue(is_bytes(random_bytes))
+
+    def test_rejects_non_bytes(self):
+        self.assertFalse(is_bytes(unicode_string))
+        self.assertFalse(is_bytes(False))
+        self.assertFalse(is_bytes(5))
+        self.assertFalse(is_bytes(None))
+        self.assertFalse(is_bytes([]))
+        self.assertFalse(is_bytes(()))
+        self.assertFalse(is_bytes([]))
+        self.assertFalse(is_bytes(object))
+
+
+class Test_is_unicode(BaseTestCase):
+
+    def test_accepts_unicode(self):
+        self.assertTrue(is_unicode(unicode_string))
+
+    def test_rejects_non_unicode(self):
+        self.assertFalse(is_unicode(random_bytes))
+        self.assertFalse(is_unicode(False))
+        self.assertFalse(is_unicode(5))
+        self.assertFalse(is_unicode(None))
+        self.assertFalse(is_unicode([]))
+        self.assertFalse(is_unicode(()))
+        self.assertFalse(is_unicode({}))
+        self.assertFalse(is_unicode(object))
+
+
+class Test_is_bytes_or_unicode(BaseTestCase):
+    def test_accepts_any_string(self):
+        self.assertTrue(is_bytes_or_unicode(random_bytes))
+        self.assertTrue(is_bytes_or_unicode(unicode_string))
+
+    def test_rejects_non_string(self):
+        self.assertFalse(is_bytes_or_unicode(False))
+        self.assertFalse(is_bytes_or_unicode(5))
+        self.assertFalse(is_bytes_or_unicode(None))
+        self.assertFalse(is_bytes_or_unicode([]))
+        self.assertFalse(is_bytes_or_unicode(()))
+        self.assertFalse(is_bytes_or_unicode({}))
+        self.assertFalse(is_bytes_or_unicode(object))
+
+
+class Test_to_utf8_if_unicode(BaseTestCase):
+    def test_encodes_unicode_strings(self):
+        self.assertEqual(to_utf8_if_unicode(unicode_string), utf8_bytes)
+
+    def test_does_not_encode_else_to_utf8(self):
+        self.assertEqual(to_utf8_if_unicode(utf8_bytes), utf8_bytes)
+        self.assertEqual(to_utf8_if_unicode(None), None)
+        self.assertEqual(to_utf8_if_unicode(False), False)
+        self.assertEqual(to_utf8_if_unicode(5), 5)
+        self.assertEqual(to_utf8_if_unicode([]), [])
+        self.assertEqual(to_utf8_if_unicode(()), ())
+        self.assertEqual(to_utf8_if_unicode({}), {})
+        self.assertEqual(to_utf8_if_unicode(object), object)
+
+
+class Test_to_unicode_if_bytes(BaseTestCase):
+    def test_encodes_bytes_to_unicode(self):
+        self.assertEqual(to_unicode_if_bytes(utf8_bytes), unicode_string)
+
+    def test_does_not_encode_else_to_unicode(self):
+        self.assertEqual(to_unicode_if_bytes(unicode_string), unicode_string)
+        self.assertEqual(to_unicode_if_bytes(None), None)
+        self.assertEqual(to_unicode_if_bytes(False), False)
+        self.assertEqual(to_unicode_if_bytes(5), 5)
+        self.assertEqual(to_unicode_if_bytes([]), [])
+        self.assertEqual(to_unicode_if_bytes(()), ())
+        self.assertEqual(to_unicode_if_bytes({}), {})
+        self.assertEqual(to_unicode_if_bytes(object), object)
+
+
+class Test_to_unicode(BaseTestCase):
+    def test_converts_bytes_to_unicode(self):
+        self.assertEqual(bytes_to_unicode(utf8_bytes), unicode_string)
+
+    def test_does_not_encode_unicode_and_None_to_unicode(self):
+        self.assertEqual(bytes_to_unicode(unicode_string), unicode_string)
+        self.assertEqual(bytes_to_unicode(None), None)
+
+    def test_raises_error_when_not_string_or_None(self):
+        self.assertRaises(AssertionError, bytes_to_unicode, 5)
+        self.assertRaises(AssertionError, bytes_to_unicode, False)
+        self.assertRaises(AssertionError, bytes_to_unicode, True)
+        self.assertRaises(AssertionError, bytes_to_unicode, [])
+        self.assertRaises(AssertionError, bytes_to_unicode, ())
+        self.assertRaises(AssertionError, bytes_to_unicode, {})
+        self.assertRaises(AssertionError, bytes_to_unicode, object)
+
+class Test_to_utf8(BaseTestCase):
+    def test_encodes_only_unicode_to_utf8(self):
+        self.assertEqual(unicode_to_utf8(unicode_string), utf8_bytes)
+
+    def test_does_not_encode_bytes_or_None_to_utf8(self):
+        self.assertEqual(unicode_to_utf8(None), None)
+        self.assertEqual(unicode_to_utf8(utf8_bytes), utf8_bytes)
+
+    def test_raises_error_when_not_string_or_None(self):
+        self.assertRaises(AssertionError, unicode_to_utf8, 5)
+        self.assertRaises(AssertionError, unicode_to_utf8, False)
+        self.assertRaises(AssertionError, unicode_to_utf8, True)
+        self.assertRaises(AssertionError, unicode_to_utf8, [])
+        self.assertRaises(AssertionError, unicode_to_utf8, ())
+        self.assertRaises(AssertionError, unicode_to_utf8, {})
+        self.assertRaises(AssertionError, unicode_to_utf8, object)
+
+
+if __name__ == "__main__":
+    unittest2.main()
+