import unittest
from wrpylib.wrvalidators import *
-# TODO: optional converter
+# optional converter
# ------------------
+class TestOpt(unittest.TestCase):
+ def test_from_str(self):
+ self.assertEqual(None, opt_from_str('', str_from_str))
+ self.assertEqual('abc', opt_from_str('abc', str_from_str))
+ self.assertEqual(None, opt_from_str('', int_from_str))
+ self.assertEqual(4, opt_from_str('4', int_from_str))
+
+ def test_to_str(self):
+ self.assertEqual('', opt_to_str(None, str_to_str))
+ self.assertEqual('abc', opt_to_str('abc', str_to_str))
+ self.assertEqual('', opt_to_str(None, int_to_str))
+ self.assertEqual('4', opt_to_str(4, int_to_str))
+
+
# "no" converter
# --------------
class TestNoGermanConverter(unittest.TestCase):
def test_from_str(self):
- self.assertEqual((True, 'abc'), no_german_from_str('abc'))
- self.assertEqual((False, None), no_german_from_str('Nein'))
+ self.assertEqual((True, 'abc'), no_german_from_str('abc', req_str_from_str))
+ self.assertEqual((False, None), no_german_from_str('Nein', req_str_from_str))
with self.assertRaises(ValueError):
- no_german_from_str('')
+ no_german_from_str('', req_str_from_str)
def test_to_str(self):
- self.assertEqual('abc', no_german_to_str((True, 'abc')))
- self.assertEqual('Nein', no_german_to_str((False, None)))
+ self.assertEqual('abc', no_german_to_str((True, 'abc'), str_to_str))
+ self.assertEqual('Nein', no_german_to_str((False, None), str_to_str))
# "optional"/"no" converter
class TestOptNoGerman(unittest.TestCase):
def test_from_str(self):
- self.assertEqual((True, 'abc'), opt_no_german_from_str('abc'))
- self.assertEqual((False, None), opt_no_german_from_str('Nein'))
- self.assertEqual((None, None), opt_no_german_from_str(''))
+ self.assertEqual((True, 'abc'), opt_no_german_from_str('abc', str_from_str))
+ self.assertEqual((False, None), opt_no_german_from_str('Nein', str_from_str))
+ self.assertEqual((None, None), opt_no_german_from_str('', str_from_str))
def test_to_str(self):
- self.assertEqual('abc', opt_no_german_to_str((True, 'abc')))
- self.assertEqual('Nein', opt_no_german_to_str((False, None)))
- self.assertEqual('', opt_no_german_to_str((None, None)))
+ self.assertEqual('abc', opt_no_german_to_str((True, 'abc'), str_to_str))
+ self.assertEqual('Nein', opt_no_german_to_str((False, None), str_to_str))
+ self.assertEqual('', opt_no_german_to_str((None, None), str_to_str))
-# TODO: choice converter
+# choice converter
# ----------------
+class TestCoice(unittest.TestCase):
+ def setUp(self):
+ self.choices = ['abc', 'def', 'ghi']
-# TODO: dict converter
-# --------------
+ def from_str(self):
+ self.assertEqual('abc', choice_from_str('abc', self.choices))
+ self.assertEqual('ghi', choice_from_str('ghi', self.choices))
+ with self.assertRaises(ValueError):
+ choice_from_str('jkl', self.choices)
+
+
+# dictkey converter
+# -----------------
+
+class TestDictkey(unittest.TestCase):
+ def setUp(self):
+ self.choices = {'abc': '1', 'def': '2', 'ghi': '3'}
+
+ def test_from_str(self):
+ self.assertEqual('abc', dictkey_from_str('1', self.choices))
+ self.assertEqual('ghi', dictkey_from_str('3', self.choices))
+ with self.assertRaises(ValueError):
+ dictkey_from_str('4', self.choices)
+
+ def test_to_str(self):
+ self.assertEqual('1', dictkey_to_str('abc', self.choices))
+ self.assertEqual('3', dictkey_to_str('ghi', self.choices))
# enum/"list" converter
class TestEnumConverter(unittest.TestCase):
def test_from_str(self):
- self.assertEqual([], enum_from_str(''))
- self.assertEqual(['abc'], enum_from_str('abc'))
- self.assertEqual(['abc', 'def'], enum_from_str('abc; def'))
- self.assertEqual(['abc', 'def', 'ghi'], enum_from_str('abc; def;ghi'))
+ self.assertEqual([], enum_from_str('', str_from_str))
+ self.assertEqual(['abc'], enum_from_str('abc', str_from_str))
+ self.assertEqual(['abc', 'def'], enum_from_str('abc; def', str_from_str))
+ self.assertEqual(['abc', 'def', 'ghi'], enum_from_str('abc; def;ghi', str_from_str))
def test_to_str(self):
- self.assertEqual('abc; def; ghi', enum_to_str(['abc', 'def', 'ghi']))
- self.assertEqual('abc', enum_to_str(['abc']))
- self.assertEqual('', enum_to_str(['']))
- self.assertEqual('', enum_to_str([]))
+ self.assertEqual('abc; def; ghi', enum_to_str(['abc', 'def', 'ghi'], str_to_str))
+ self.assertEqual('abc', enum_to_str(['abc'], str_to_str))
+ self.assertEqual('', enum_to_str([''], str_to_str))
+ self.assertEqual('', enum_to_str([], str_to_str))
# value/comment converter
class TestValueCommentConverter(unittest.TestCase):
def test_from_str(self):
- self.assertEqual(('abc', 'defg'), value_comment_from_str('abc (defg)'))
- self.assertEqual(('abc', ''), value_comment_from_str('abc ()'))
- self.assertEqual(('', 'def'), value_comment_from_str('(def)'))
- self.assertEqual(('ab', '(cd)'), value_comment_from_str('ab((cd))'))
- self.assertEqual(('ab', '(c(d)[(]))'), value_comment_from_str('ab((c(d)[(])))'))
- self.assertEqual(('ab(', 'cd'), value_comment_from_str('ab((cd)'))
- self.assertEqual(('abcd', 'ef'), value_comment_from_str('abcd (ef) '))
- self.assertEqual(('abc', ''), value_comment_from_str('abc', comment_optional=True))
+ self.assertEqual(('abc', 'defg'), value_comment_from_str('abc (defg)', str_from_str, str_from_str))
+ self.assertEqual(('abc', ''), value_comment_from_str('abc ()', str_from_str, str_from_str))
+ self.assertEqual(('', 'def'), value_comment_from_str('(def)', str_from_str, str_from_str))
+ self.assertEqual(('ab', '(cd)'), value_comment_from_str('ab((cd))', str_from_str, str_from_str))
+ self.assertEqual(('ab', '(c(d)[(]))'), value_comment_from_str('ab((c(d)[(])))', str_from_str, str_from_str))
+ self.assertEqual(('ab(', 'cd'), value_comment_from_str('ab((cd)', str_from_str, str_from_str))
+ self.assertEqual(('abcd', 'ef'), value_comment_from_str('abcd (ef) ', str_from_str, str_from_str))
+ self.assertEqual(('abc', ''), value_comment_from_str('abc', str_from_str, str_from_str, comment_optional=True))
with self.assertRaises(ValueError):
- value_comment_from_str('abc (')
+ value_comment_from_str('abc (', str_from_str, str_from_str)
with self.assertRaises(ValueError):
- value_comment_from_str('abc )')
+ value_comment_from_str('abc )', str_from_str, str_from_str)
with self.assertRaises(ValueError):
- value_comment_from_str('abc (def)g')
+ value_comment_from_str('abc (def)g', str_from_str, str_from_str)
with self.assertRaises(ValueError):
- value_comment_from_str('abc (b))')
+ value_comment_from_str('abc (b))', str_from_str, str_from_str)
with self.assertRaises(ValueError):
- value_comment_from_str('abc')
+ value_comment_from_str('abc', str_from_str, str_from_str)
def test_to_str(self):
- self.assertEqual('abc (defg)', value_comment_to_str(('abc', 'defg')))
- self.assertEqual('abc ()', value_comment_to_str(('abc', '')))
- self.assertEqual('(def)', value_comment_to_str(('', 'def')))
- self.assertEqual('ab ((cd))', value_comment_to_str(('ab', '(cd)')))
- self.assertEqual('ab ((c(d)[(])))', value_comment_to_str(('ab', '(c(d)[(]))')))
- self.assertEqual('ab( (cd)', value_comment_to_str(('ab(', 'cd')))
- self.assertEqual('abcd (ef)', value_comment_to_str(('abcd', 'ef')))
- self.assertEqual('abc', value_comment_to_str(('abc', ''), comment_optional=True))
+ self.assertEqual('abc (defg)', value_comment_to_str(('abc', 'defg'), str_to_str, str_to_str))
+ self.assertEqual('abc ()', value_comment_to_str(('abc', ''), str_to_str, str_to_str))
+ self.assertEqual('(def)', value_comment_to_str(('', 'def'), str_to_str, str_to_str))
+ self.assertEqual('ab ((cd))', value_comment_to_str(('ab', '(cd)'), str_to_str, str_to_str))
+ self.assertEqual('ab ((c(d)[(])))', value_comment_to_str(('ab', '(c(d)[(]))'), str_to_str, str_to_str))
+ self.assertEqual('ab( (cd)', value_comment_to_str(('ab(', 'cd'), str_to_str, str_to_str))
+ self.assertEqual('abcd (ef)', value_comment_to_str(('abcd', 'ef'), str_to_str, str_to_str))
+ self.assertEqual('abc', value_comment_to_str(('abc', ''), str_to_str, str_to_str, comment_optional=True))
# string converter
self.assertEqual('', opt_uint_to_str(None))
-# TODO: bool converter
+# bool converter
# --------------
+class TestBoolGerman(unittest.TestCase):
+ def test_from_str(self):
+ self.assertEqual(True, bool_german_from_str('Ja'))
+ self.assertEqual(True, opt_bool_german_from_str('Ja'))
+ self.assertEqual(False, bool_german_from_str('Nein'))
+ self.assertEqual(False, opt_bool_german_from_str('Nein'))
+ self.assertEqual(None, opt_bool_german_from_str(''))
+ with self.assertRaises(ValueError):
+ bool_german_from_str('Vielleicht')
+ opt_bool_german_from_str('Vielleicht')
+ bool_german_from_str('')
+
+ def test_to_str(self):
+ self.assertEqual('Ja', bool_german_to_str(True))
+ self.assertEqual('Ja', opt_bool_german_to_str(True))
+ self.assertEqual('Nein', bool_german_to_str(False))
+ self.assertEqual('Nein', opt_bool_german_to_str(False))
+ self.assertEqual('', opt_bool_german_to_str(None))
+
# tristate converter
# ------------------
self.assertEqual(None, emails_from_str(''))
self.assertEqual([], emails_from_str('Nein'))
self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com'))
- # self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
+ self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)'))
self.assertEqual([(('info@example.com', False), None), (('home@example.com', False), 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)'))
with self.assertRaises(ValueError):
self.assertEqual([], opt_phone_comment_enum_from_str('Nein'))
self.assertEqual([('+43-512-123456', 'untertags')], opt_phone_comment_enum_from_str('+43-512-123456 (untertags)'))
self.assertEqual([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')], opt_phone_comment_enum_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'))
+ self.assertEqual([('+43-512-1234', None), ('+43-664-123456', 'Sommer')], opt_phone_comment_enum_from_str('+43-512-1234; +43-664-123456 (Sommer)', True))
with self.assertRaises(ValueError):
opt_phone_comment_enum_from_str('+43-512-123456+ (untertags)')
with self.assertRaises(ValueError):
self.assertEqual('Nein', opt_phone_comment_enum_to_str([]))
self.assertEqual('+43-512-123456 (untertags)', opt_phone_comment_enum_to_str([('+43-512-123456', 'untertags')]))
self.assertEqual('+43-512-1234 (untertags); +43-664-123456 (Alm)', opt_phone_comment_enum_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]))
+ self.assertEqual('+43-512-1234; +43-664-123456 (Sommer)', opt_phone_comment_enum_to_str([('+43-512-1234', None), ('+43-664-123456', 'Sommer')], True))
# longitude/latitude converter
self.assertEqual(self.value, rodelbahnbox_to_str(value))
-# TODO: Gasthausbox converter
+# Gasthausbox converter
# ---------------------
class TestGasthausbox(unittest.TestCase):
- def test_GasthausboxDictValidator(self):
- v = wrpylib.wrvalidators.GasthausboxDictValidator()
- other = collections.OrderedDict([
- ('Position', '47.295549 N 9.986970 E'),
- ('Höhe', '1250'),
- ('Betreiber', ''),
- ('Sitzplätze', ''),
- ('Übernachtung', ''),
- ('Rauchfrei', 'Nein'),
- ('Rodelverleih', ''),
- ('Handyempfang', 'A1; T-Mobile/Telering'),
- ('Homepage', 'http://www.bergkristallhuette.com/'),
- ('E-Mail', 'bergkristallhuette@gmx.at'),
- ('Telefon', '+43-664-1808482'),
- ('Bild', 'Bergkritsallhütte 2009-02-07.JPG'),
- ('Rodelbahnen', '[[Bergkristallhütte]]')])
- python = v.to_python(other, None)
- other2 = v.from_python(python, None)
- assert other == other2
+ def setUp(self):
+ self.maxDiff = None
+ self.value = \
+"""{{Gasthausbox
+| Position = 47.123456 N 11.123456 E
+| Höhe = 1808
+| Betreiber = Max Mustermann
+| Sitzplätze = 50
+| Übernachtung = 20 Matrazenlager, 3 Doppelzimmer
+| Rauchfrei = Ja
+| Rodelverleih = 2 Euro (Ausweis erforderlich)
+| Handyempfang = A1; T-Mobile A
+| Homepage = http://www.birgitzeralm.at/
+| E-Mail = Nein
+| Telefon = +43-664-5487520; +43-512-123456 (wenn geschlossen)
+| Bild = Gasthaus_Birgitzer_Alm_03.jpg
+| Rodelbahnen = [[Birgitzer Alm (vom Adelshof)]]; [[Birgitzer Alm (von Birgitz)]]
+}}"""
+
+ def test_from_str(self):
+ value = gasthausbox_from_str(self.value)
+ self.assertEqual(LonLat(11.123456, 47.123456), value['Position'])
+ self.assertEqual(1808, value['Höhe'])
+ self.assertEqual('Max Mustermann', value['Betreiber'])
+ self.assertEqual(50, value['Sitzplätze'])
+ self.assertEqual((True, '20 Matrazenlager, 3 Doppelzimmer'), value['Übernachtung'])
+ self.assertEqual((True, '2 Euro (Ausweis erforderlich)'), value['Rodelverleih'])
+ self.assertEqual(1.0, value['Rauchfrei'])
+ self.assertEqual([('A1', None), ('T-Mobile A', None)], value['Handyempfang'])
+ self.assertEqual((True, 'http://www.birgitzeralm.at/'), value['Homepage'])
+ self.assertEqual([], value['E-Mail'])
+ self.assertEqual([('+43-664-5487520', None), ('+43-512-123456', 'wenn geschlossen')], value['Telefon'])
+ self.assertEqual('Gasthaus_Birgitzer_Alm_03.jpg', value['Bild'])
+ self.assertEqual(['[[Birgitzer Alm (vom Adelshof)]]', '[[Birgitzer Alm (von Birgitz)]]'], value['Rodelbahnen'])
+
+ def test_to_str(self):
+ value = OrderedDict([
+ ('Position', LonLat(11.123456, 47.123456)),
+ ('Höhe', 1808),
+ ('Betreiber', 'Max Mustermann'),
+ ('Sitzplätze', 50),
+ ('Übernachtung', (True, '20 Matrazenlager, 3 Doppelzimmer')),
+ ('Rodelverleih', (True, '2 Euro (Ausweis erforderlich)')),
+ ('Rauchfrei', 1.0),
+ ('Handyempfang', [('A1', None), ('T-Mobile A', None)]),
+ ('Homepage', (True, 'http://www.birgitzeralm.at/')),
+ ('E-Mail', []),
+ ('Telefon', [('+43-664-5487520', None), ('+43-512-123456', 'wenn geschlossen')]),
+ ('Bild', 'Gasthaus_Birgitzer_Alm_03.jpg'),
+ ('Rodelbahnen', ['[[Birgitzer Alm (vom Adelshof)]]', '[[Birgitzer Alm (von Birgitz)]]'])])
+ self.assertEqual(self.value, gasthausbox_to_str(value))