From 6aae60fb530f7608bc379a57a477e904a816544d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 21 Mar 2013 23:38:40 +0100 Subject: Add Python3 support. --- tests/compat.py | 14 ++++++++++++++ tests/test_base.py | 27 ++++++++++++++++++++++----- tests/test_django.py | 4 +++- tests/test_parsing.py | 4 +++- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 tests/compat.py (limited to 'tests') diff --git a/tests/compat.py b/tests/compat.py new file mode 100644 index 0000000..90f1baa --- /dev/null +++ b/tests/compat.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2012-2013 Raphaël Barrois +# This code is distributed under the two-clause BSD License. + +import sys + +is_python2 = (sys.version_info[0] == 2) + + +try: # pragma: no cover + import unittest2 as unittest +except ImportError: # pragma: no cover + import unittest + diff --git a/tests/test_base.py b/tests/test_base.py index 84119f1..3006ba0 100755 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -5,11 +5,7 @@ """Test the various functions from 'base'.""" -try: # pragma: no cover - import unittest2 as unittest -except ImportError: # pragma: no cover - import unittest - +from .compat import unittest, is_python2 from semantic_version import base @@ -171,6 +167,8 @@ class VersionTestCase(unittest.TestCase): self.assertNotEqual(text, base.Version(text)) partial_versions = { + '1.1': (1, 1, None, None, None), + '2': (2, None, None, None, None), '1.0.0-alpha': (1, 0, 0, ('alpha',), None), '1.0.0-alpha.1': (1, 0, 0, ('alpha', '1'), None), '1.0.0-beta.2': (1, 0, 0, ('beta', '2'), None), @@ -229,6 +227,21 @@ class VersionTestCase(unittest.TestCase): ])) ) + @unittest.skipIf(is_python2, "Comparisons to other objects are broken in Py2.") + def test_invalid_comparisons(self): + v = base.Version('0.1.0') + with self.assertRaises(TypeError): + v < '0.1.0' + with self.assertRaises(TypeError): + v <= '0.1.0' + with self.assertRaises(TypeError): + v > '0.1.0' + with self.assertRaises(TypeError): + v >= '0.1.0' + + self.assertTrue(v != '0.1.0') + self.assertFalse(v == '0.1.0') + class SpecItemTestCase(unittest.TestCase): components = { @@ -345,6 +358,7 @@ class SpecItemTestCase(unittest.TestCase): class CoerceTestCase(unittest.TestCase): examples = { # Dict of target: [list of equivalents] + '0.0.0': ('0', '0.0', '0.0.0', '0.0.0+', '0-', '00000000.00'), '0.1.0': ('0.1', '0.1+', '0.1-', '0.1.0', '0.000001.000000000000'), '0.1.0+2': ('0.1.0+2', '0.1.0.2'), '0.1.0+2.3.4': ('0.1.0+2.3.4', '0.1.0+2+3+4', '0.1.0.2+3+4'), @@ -360,6 +374,9 @@ class CoerceTestCase(unittest.TestCase): v_sample = base.Version.coerce(sample) self.assertEqual(target, v_sample) + def test_invalid(self): + self.assertRaises(ValueError, base.Version.coerce, 'v1') + class SpecTestCase(unittest.TestCase): examples = { diff --git a/tests/test_django.py b/tests/test_django.py index fd4e044..ffdfe58 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -2,6 +2,8 @@ # Copyright (c) 2012-2013 Raphaël Barrois # This code is distributed under the two-clause BSD License. +from __future__ import unicode_literals + try: # pragma: no cover import unittest2 as unittest except ImportError: # pragma: no cover @@ -28,7 +30,7 @@ if django_loaded: # pragma: no cover 'tests.django_test_app', ] ) - from django_test_app import models + from .django_test_app import models from django.core import serializers try: # pragma: no cover diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 01c8ae8..585011d 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -63,7 +63,9 @@ class ComparisonTestCase(unittest.TestCase): self.assertTrue(first_ver == second_ver, '%r != %r' % (first_ver, second_ver)) else: self.assertTrue(first_ver > second_ver, '%r !> %r' % (first_ver, second_ver)) - self.assertEqual(cmp(i, j), semantic_version.compare(first, second)) + + cmp_res = -1 if i < j else (1 if i > j else 0) + self.assertEqual(cmp_res, semantic_version.compare(first, second)) if __name__ == '__main__': # pragma: no cover -- cgit v1.2.1