]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - tests/test_wrvalidators.py
Reordered functions.
[philipp/winterrodeln/wrpylib.git] / tests / test_wrvalidators.py
index 3b6a29978f4fdae2b2080d5c1e6143c72163919c..034b986ad6830360bfc7e28876bc04fdf1983904 100644 (file)
 # -*- coding: iso-8859-15 -*-
 import collections
 import wrpylib.wrvalidators
-import formencode
 import unittest
 from wrpylib.wrvalidators import *
 
+# TODO: optional converter
+# ------------------
+
+# "no" converter
+# --------------
+
+class TestNoGermanConverter(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual(no_german_from_str('abc'), (True, 'abc'))
+        self.assertEqual(no_german_from_str('Nein'), (False, None))
+        with self.assertRaises(ValueError):
+            no_german_from_str('')
+
+    def test_to_str(self):
+        self.assertEqual(no_german_to_str((True, 'abc')), 'abc')
+        self.assertEqual(no_german_to_str((False, None)), 'Nein')
+
+
+# "optional"/"no" converter
+# -------------------------
+
+class TestOptNoGerman(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual(opt_no_german_from_str('abc'), (True, 'abc'))
+        self.assertEqual(opt_no_german_from_str('Nein'), (False, None))
+        self.assertEqual(opt_no_german_from_str(''), (None, None))
+
+    def test_to_str(self):
+        self.assertEqual(opt_no_german_to_str((True, 'abc')), 'abc')
+        self.assertEqual(opt_no_german_to_str((False, None)), 'Nein')
+        self.assertEqual(opt_no_german_to_str((None, None)), '')
+
+
+# TODO: choice converter
+# ----------------
+
+
+# TODO: dict converter
+# --------------
+
+
+# TODO: enum/"list" converter
+# ---------------------
+
+
+# TODO: value/comment converter
+# -----------------------
+
+
+# string converter
+# ----------------
+
+class TestStr(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual('', str_from_str(''))
+        self.assertEqual('abc', str_from_str('abc'))
+
+    def test_to_str(self):
+        self.assertEqual('', str_to_str(''))
+        self.assertEqual('abc', str_to_str('abc'))
+
+
+class TestReqStr(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual('abc', req_str_from_str('abc'))
+        self.assertEqual(' ', req_str_from_str(' '))
+        with self.assertRaises(ValueError):
+            req_str_from_str('')
+
+
+class TestOptStr(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual('abc', opt_str_from_str('abc'))
+        self.assertEqual(' ', opt_str_from_str(' '))
+        self.assertEqual(None, opt_str_from_str(''))
+
+    def test_to_str(self):
+        self.assertEqual('abc', opt_str_to_str('abc'))
+        self.assertEqual(' ', opt_str_to_str(' '))
+        self.assertEqual('', opt_str_to_str(None))
+
+
+# TODO: optional no or string converter
+# -------------------------------
+
+
+# integer converter
+# -----------------
 
 class TestInt(unittest.TestCase):
     def test_from_str(self):
-        self.assertEqual(int_from_str('42'), 42)
-        self.assertEqual(int_from_str('+42'), 42)
-        self.assertEqual(int_from_str('-20'), -20)
-        self.assertEqual(int_from_str('0', min=0), 0)
-        self.assertEqual(int_from_str('10', max=10), 10)
+        self.assertEqual(42, int_from_str('42'))
+        self.assertEqual(42, int_from_str('+42'))
+        self.assertEqual(-20, int_from_str('-20'))
+        self.assertEqual(0, int_from_str('0', min=0))
+        self.assertEqual(10, int_from_str('10', max=10))
         with self.assertRaises(ValueError):
             int_from_str('abc')
         with self.assertRaises(ValueError):
@@ -28,17 +115,17 @@ class TestInt(unittest.TestCase):
             int_from_str('0d')
 
     def test_to_str(self):
-        self.assertEqual(int_to_str(20), '20')
-        self.assertEqual(int_to_str(-20), '-20')
-        self.assertEqual(int_to_str(0), '0')
+        self.assertEqual('20', int_to_str(20))
+        self.assertEqual('-20', int_to_str(-20))
+        self.assertEqual('0', int_to_str(0))
 
 
 class TestOptInt(unittest.TestCase):
     def test_from_str(self):
-        self.assertEqual(opt_int_from_str('42'), 42)
-        self.assertEqual(opt_int_from_str('+42'), 42)
-        self.assertEqual(opt_int_from_str('-20'), -20)
-        self.assertEqual(opt_int_from_str(''), None)
+        self.assertEqual(42, opt_int_from_str('42'))
+        self.assertEqual(42, opt_int_from_str('+42'))
+        self.assertEqual(-20, opt_int_from_str('-20'))
+        self.assertEqual(None, opt_int_from_str(''))
         with self.assertRaises(ValueError):
             opt_int_from_str('abc')
         with self.assertRaises(ValueError):
@@ -47,10 +134,28 @@ class TestOptInt(unittest.TestCase):
             opt_int_from_str('0d')
 
     def test_to_str(self):
-        self.assertEqual(opt_int_to_str(20), '20')
-        self.assertEqual(opt_int_to_str(-20), '-20')
-        self.assertEqual(opt_int_to_str(0), '0')
-        self.assertEqual(opt_int_to_str(None), '')
+        self.assertEqual('20', opt_int_to_str(20))
+        self.assertEqual('-20', opt_int_to_str(-20))
+        self.assertEqual('0', opt_int_to_str(0))
+        self.assertEqual('', opt_int_to_str(None))
+
+
+class TestOptUInt(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual(42, opt_uint_from_str('42'))
+        self.assertEqual(0, opt_uint_from_str('0'))
+        self.assertEqual(None, opt_uint_from_str(''))
+        with self.assertRaises(ValueError):
+            opt_uint_from_str('-1')
+
+    def test_to_str(self):
+        self.assertEqual('20', opt_uint_to_str(20))
+        self.assertEqual('0', opt_uint_to_str(0))
+        self.assertEqual('', opt_uint_to_str(None))
+
+
+# bool converter
+# --------------
 
 
 class TestEnumConverter(unittest.TestCase):
@@ -173,30 +278,6 @@ class TestValueCommentConverter(unittest.TestCase):
         self.assertEqual(value_comment_to_str(('abc', ''), comment_optional=True), 'abc')
 
 
-class TestNoGermanConverter(unittest.TestCase):
-    def test_from_str(self):
-        self.assertEqual(no_german_from_str('abc'), (True, 'abc'))
-        self.assertEqual(no_german_from_str('Nein'), (False, None))
-        with self.assertRaises(ValueError):
-            no_german_from_str('')
-
-    def test_to_str(self):
-        self.assertEqual(no_german_to_str((True, 'abc')), 'abc')
-        self.assertEqual(no_german_to_str((False, None)), 'Nein')
-
-
-class TestOptNoGerman(unittest.TestCase):
-    def test_from_str(self):
-        self.assertEqual(opt_no_german_from_str('abc'), (True, 'abc'))
-        self.assertEqual(opt_no_german_from_str('Nein'), (False, None))
-        self.assertEqual(opt_no_german_from_str(''), (None, None))
-
-    def test_to_str(self):
-        self.assertEqual(opt_no_german_to_str((True, 'abc')), 'abc')
-        self.assertEqual(opt_no_german_to_str((False, None)), 'Nein')
-        self.assertEqual(opt_no_german_to_str((None, None)), '')
-
-
 class TestLiftGermanValidator(unittest.TestCase):
     def test_from_str(self):
         self.assertEqual(lift_german_from_str(''), None)
@@ -249,11 +330,11 @@ class TestOptStrOptCommentEnum(unittest.TestCase):
             opt_str_opt_comment_enum_from_str('Talstation (unten); ; Mittelstation')
 
     def test_to_str(self):
-        self.assertEqual(sledrental_to_str(None), '')
-        self.assertEqual(sledrental_to_str([]), 'Nein')
-        self.assertEqual(sledrental_to_str([('Talstation', None)]), 'Talstation')
-        self.assertEqual(sledrental_to_str([('Talstation', 'unten')]), 'Talstation (unten)')
-        self.assertEqual(sledrental_to_str([('Talstation', 'unten'), ('Mittelstation', None)]), 'Talstation (unten); Mittelstation')
+        self.assertEqual(opt_str_opt_comment_enum_to_str(None), '')
+        self.assertEqual(opt_str_opt_comment_enum_to_str([]), 'Nein')
+        self.assertEqual(opt_str_opt_comment_enum_to_str([('Talstation', None)]), 'Talstation')
+        self.assertEqual(opt_str_opt_comment_enum_to_str([('Talstation', 'unten')]), 'Talstation (unten)')
+        self.assertEqual(opt_str_opt_comment_enum_to_str([('Talstation', 'unten'), ('Mittelstation', None)]), 'Talstation (unten); Mittelstation')
 
 
 class TestNoOrStr(unittest.TestCase):
@@ -357,20 +438,20 @@ class TestPhoneNumber(unittest.TestCase):
 
 class TestTelefonauskunft(unittest.TestCase):
     def test_from_str(self):
-        self.assertEqual(telefonauskunft_from_str(''), None)
-        self.assertEqual(telefonauskunft_from_str('Nein'), [])
-        self.assertEqual(telefonauskunft_from_str('+43-512-123456 (untertags)'), [('+43-512-123456', 'untertags')])
-        self.assertEqual(telefonauskunft_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'), [('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')])
+        self.assertEqual(opt_phone_comment_enum_from_str(''), None)
+        self.assertEqual(opt_phone_comment_enum_from_str('Nein'), [])
+        self.assertEqual(opt_phone_comment_enum_from_str('+43-512-123456 (untertags)'), [('+43-512-123456', 'untertags')])
+        self.assertEqual(opt_phone_comment_enum_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'), [('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')])
         with self.assertRaises(ValueError):
-            telefonauskunft_from_str('+43-512-123456+ (untertags)')
+            opt_phone_comment_enum_from_str('+43-512-123456+ (untertags)')
         with self.assertRaises(ValueError):
-            telefonauskunft_from_str('+43-512-123456')
+            opt_phone_comment_enum_from_str('+43-512-123456')
 
     def test_to_str(self):
-        self.assertEqual(telefonauskunft_to_str(None), '')
-        self.assertEqual(telefonauskunft_to_str([]), 'Nein')
-        self.assertEqual(telefonauskunft_to_str([('+43-512-123456', 'untertags')]), '+43-512-123456 (untertags)')
-        self.assertEqual(telefonauskunft_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
+        self.assertEqual(opt_phone_comment_enum_to_str(None), '')
+        self.assertEqual(opt_phone_comment_enum_to_str([]), 'Nein')
+        self.assertEqual(opt_phone_comment_enum_to_str([('+43-512-123456', 'untertags')]), '+43-512-123456 (untertags)')
+        self.assertEqual(opt_phone_comment_enum_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
 
 
 class TestEmail(unittest.TestCase):
@@ -458,31 +539,31 @@ class TestBox(unittest.TestCase):
     def test_from_str(self):
         value = '{{MyTemplate|apple=2|banana=5}}'
         converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
-        result = box_from_str(value, 'MyTemplate', converter_dict)
+        result = wikibox_from_str(value, 'MyTemplate', converter_dict)
         self.assertEqual(result['apple'], 2)
         self.assertEqual(result['banana'], 5)
 
         value = '{{MyTemplate\n | apple = 2 \n| banana = 5 }}'
-        result = box_from_str(value, 'MyTemplate', converter_dict)
+        result = wikibox_from_str(value, 'MyTemplate', converter_dict)
         self.assertEqual(result['apple'], 2)
         self.assertEqual(result['banana'], 5)
 
         with self.assertRaises(ValueError):
-            box_from_str(value, 'myTemplate', converter_dict)
+            wikibox_from_str(value, 'myTemplate', converter_dict)
         with self.assertRaises(ValueError):
             value = '{{MyTemplate|apple=2|banana=five}}'
-            box_from_str(value, 'MyTemplate', converter_dict)
+            wikibox_from_str(value, 'MyTemplate', converter_dict)
         with self.assertRaises(ValueError):
             value = '{{MyTemplate|apple=2}}'
-            box_from_str(value, 'MyTemplate', converter_dict)
+            wikibox_from_str(value, 'MyTemplate', converter_dict)
         with self.assertRaises(ValueError):
             value = '{{MyTemplate|apple=2|banana=5|cherry=6}}'
-            box_from_str(value, 'MyTemplate', converter_dict)
+            wikibox_from_str(value, 'MyTemplate', converter_dict)
 
     def test_to_str(self):
         value = OrderedDict([('apple', 2), ('banana', 5)])
         converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
-        result = box_to_str(value, 'MyTemplate', converter_dict)
+        result = wikibox_to_str(value, 'MyTemplate', converter_dict)
         self.assertEqual(result, '{{MyTemplate|apple=2|banana=5}}')
 
 
@@ -581,16 +662,7 @@ class TestWrValidators(unittest.TestCase):
         assert v.from_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
 
 
-    def test_PhoneCommentListNeinLoopNone(self):
-        v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
-        assert v.to_python('') == None
-        assert v.to_python('Nein') == 'Nein'
-        assert v.to_python('+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == '+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
-        assert v.from_python(None) == ''
-        assert v.from_python('Nein') == 'Nein'
-        assert v.from_python('+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == '+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
-
-
+class TestGasthausbox(unittest.TestCase):
     def test_GasthausboxDictValidator(self):
         v = wrpylib.wrvalidators.GasthausboxDictValidator()
         other = collections.OrderedDict([