X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/blobdiff_plain/c3722db017f5f0022021fa47fedb3a0f3be49dce..3bfa52e935cb33d4a6231b0d0328facbc6299254:/tests/test_wrvalidators.py diff --git a/tests/test_wrvalidators.py b/tests/test_wrvalidators.py index 087d791..eeb4a1c 100644 --- a/tests/test_wrvalidators.py +++ b/tests/test_wrvalidators.py @@ -1,147 +1,349 @@ -#!/usr/bin/python2.6 +#!/usr/bin/python2.7 # -*- coding: iso-8859-15 -*- +import collections import wrpylib.wrvalidators import formencode +import unittest -def test_NoneValidator(): - v = wrpylib.wrvalidators.NoneValidator(wrpylib.wrvalidators.Unicode()) - assert v.to_python(u'') == None - assert v.from_python(None) == u'' - - -def test_UnsignedNone(): - v = wrpylib.wrvalidators.UnsignedNone() - assert v.to_python(u'42') == 42 - assert v.to_python(u'') == None - assert v.from_python(42) == u'42' - assert v.from_python(None) == u'' - - -def test_GeoNone(): - coord = u'47.076207 N 11.453553 E' - v = wrpylib.wrvalidators.GeoNone() - (lat, lon) = v.to_python(coord) - assert lat == 47.076207 - assert lon == 11.453553 - assert v.to_python(u'') == (None, None) - - assert v.from_python((lat, lon)) == coord - assert v.from_python((None, None)) == u'' - - -def test_GermanTristateTuple(): - v = wrpylib.wrvalidators.GermanTristateTuple() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Ja') == (True, False) - assert v.to_python(u'Nein') == (False, True) - assert v.to_python(u'Teilweise') == (True, True) - assert v.from_python((None, None)) == u'' - assert v.from_python((False, True)) == u'Nein' - assert v.from_python((True, False)) == u'Ja' - assert v.from_python((True, True)) == u'Teilweise' - - -def tes_GermanTristateFloat(): - v = wrpylib.wrvalidators.GermanTristateFloat() - assert v.to_python(u'') == None - assert v.to_python(u'Ja') == 1.0 - assert v.to_python(u'Nein') == 0.0 - assert v.to_python(u'Teilweise') == 0.5 - assert v.from_python(None) == u'' - assert v.from_python(0.0) == u'Nein' - assert v.from_python(1.0) == u'Ja' - assert v.from_python(0.5) == u'Teilweise' - - -def test_ValueCommentList(): - v = wrpylib.wrvalidators.ValueCommentList() - assert v.to_python(u'abc') == [(u'abc', None)] - assert v.to_python(u'abc def') == [(u'abc def', None)] - assert v.to_python(u'value (comment)') == [(u'value', u'comment')] - assert v.to_python(u'value (comment)') == [(u'value', u'comment')] - assert v.to_python(u'value1 (comment); value2') == [(u'value1', u'comment'), (u'value2', None)] - assert v.to_python(u'value1 (comment1); value2; value3 (comment3)') == [(u'value1', u'comment1'), (u'value2', None), ('value3', 'comment3')] - assert v.to_python(u'value1 (comment1); value2 (test (not easy))') == [(u'value1', u'comment1'), (u'value2', u'test (not easy)')] - - -def test_UrlNeinNone(): - v = wrpylib.wrvalidators.UrlNeinNone() - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at' - - -def test_ValueCommentListNeinLoopNone(): - v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone() - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1' - - -def test_PhoneCommentListNeinLoopNone(): - v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True) - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' - - -def test_EmailCommentListNeinLoopNone(): - v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone() - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'first@example.com') == u'first@example.com' - assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'first@example.com') == u'first@example.com' - assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com' - - -def test_GermanLift(): - v = wrpylib.wrvalidators.GermanLift() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Nein') == (False, None) - assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)') - assert v.from_python((None, None)) == u'' - assert v.from_python((False, None)) == u'Nein' - assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)' - - -def test_WikiPageListLoopNone(): - v = wrpylib.wrvalidators.WikiPageListLoopNone() - assert v.to_python(u'') == None - assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]' - assert v.from_python(None) == u'' - assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]' - - -def test_BoolUnicodeTupleValidator(): - v = wrpylib.wrvalidators.BoolUnicodeTupleValidator() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Nein') == (False, None) - assert v.to_python(u'any text') == (True, u'any text') - assert v.from_python((None, None)) == u'' - assert v.from_python((False, None)) == u'Nein' - assert v.from_python((True, u'any text')) == u'any text' - - -def test_SledRental(): - v = wrpylib.wrvalidators.SledRental() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Nein') == (False, None) - assert v.to_python(u'Ja') == (True, u'Ja') - assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm') - assert v.from_python((None, None)) == u'' - assert v.from_python((False, None)) == u'Nein' - assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm' - assert v.from_python((True, u'Ja')) == u'Ja' +class TestWrValidators(unittest.TestCase): + def test_OrderedSchema(self): + v = wrpylib.wrvalidators.OrderedSchema() + v.pre_validators = [formencode.Validator()] + v.chained_validators = [formencode.Validator()] + v.add_field(u'c', formencode.Validator()) + v.add_field(u'b', formencode.Validator()) + v.add_field(u'a', formencode.Validator()) + v.add_field(u'd', formencode.Validator()) + other = collections.OrderedDict([('d', 'd'), ('b', 'b'), ('a', 'a'), ('c', 'c')]) + python = v.to_python(other) + assert python.keys() == other.keys() + assert python.values() == other.values() + other2 = v.from_python(python) + assert other.keys() == other2.keys() + assert other.values() == other2.values() + + + def test_NoneValidator(self): + v = wrpylib.wrvalidators.NoneValidator(wrpylib.wrvalidators.Unicode()) + assert v.to_python(u'') == None + assert v.from_python(None) == u'' + + + # test_NeinValidator + + + # test_Unicode + + + # test_UnicodeNone + + + # test_Unsigned + + + def test_UnsignedNone(self): + v = wrpylib.wrvalidators.UnsignedNone() + assert v.to_python(u'42') == 42 + assert v.to_python(u'') == None + assert v.from_python(42) == u'42' + assert v.from_python(None) == u'' + + + # test_UnsignedNeinNone + + + # test_Loop + + + # test_DictValidator + + + # test_GermanBoolNone + + + def test_GermanTristateTuple(self): + v = wrpylib.wrvalidators.GermanTristateTuple() + assert v.to_python(u'') == (None, None) + assert v.to_python(u'Ja') == (True, False) + assert v.to_python(u'Nein') == (False, True) + assert v.to_python(u'Teilweise') == (True, True) + assert v.from_python((None, None)) == u'' + assert v.from_python((False, True)) == u'Nein' + assert v.from_python((True, False)) == u'Ja' + assert v.from_python((True, True)) == u'Teilweise' + + + def test_GermanTristateFloat(self): + v = wrpylib.wrvalidators.GermanTristateFloat() + assert v.to_python(u'') == None + assert v.to_python(u'Ja') == 1.0 + assert v.to_python(u'Nein') == 0.0 + assert v.to_python(u'Teilweise') == 0.5 + assert v.from_python(None) == u'' + assert v.from_python(0.0) == u'Nein' + assert v.from_python(1.0) == u'Ja' + assert v.from_python(0.5) == u'Teilweise' + + + # test_ValueComment + + + # test_SemicolonList + + + def test_ValueCommentList(self): + v = wrpylib.wrvalidators.ValueCommentList() + assert v.to_python(u'abc') == [(u'abc', None)] + assert v.to_python(u'abc def') == [(u'abc def', None)] + assert v.to_python(u'value (comment)') == [(u'value', u'comment')] + assert v.to_python(u'value (comment)') == [(u'value', u'comment')] + assert v.to_python(u'value1 (comment); value2') == [(u'value1', u'comment'), (u'value2', None)] + assert v.to_python(u'value1 (comment1); value2; value3 (comment3)') == [(u'value1', u'comment1'), (u'value2', None), ('value3', 'comment3')] + assert v.to_python(u'value1 (comment1); [[link (linkcomment)]] (not easy)') == [(u'value1', u'comment1'), (u'[[link (linkcomment)]]', u'not easy')] + + + # test_GenericDateTime + + + # test_DateTimeNoSec + + + # test_DateNone + + + # test_Geo + + + def test_GeoNone(self): + coord = u'47.076207 N 11.453553 E' + v = wrpylib.wrvalidators.GeoNone() + (lat, lon) = v.to_python(coord) + assert lat == 47.076207 + assert lon == 11.453553 + assert v.to_python(u'') == (None, None) + + assert v.from_python((lat, lon)) == coord + assert v.from_python((None, None)) == u'' + + + # test_MultiGeo + + + # test_AustrianPhoneNumber + + + # test_AustrianPhoneNumberNone + + + # test_AustrianPhoneNumberCommentLoop + + + # test_GermanDifficulty + + + # test_GermanAvalanches + + + def test_GermanPublicTransport(self): + v = wrpylib.wrvalidators.GermanPublicTransport() + assert v.to_python(u'') is None + assert v.to_python(u'Sehr gut') == 1 + assert v.to_python(u'Gut') == 2 + assert v.to_python(u'Mittelmäßig') == 3 + assert v.to_python(u'Schlecht') == 4 + assert v.to_python(u'Nein') == 5 + assert v.to_python(u'Ja') == 6 + + assert v.from_python(None) == u'' + assert v.from_python(1) == u'Sehr gut' + assert v.from_python(2) == u'Gut' + assert v.from_python(3) == u'Mittelmäßig' + assert v.from_python(4) == u'Schlecht' + assert v.from_python(5) == u'Nein' + assert v.from_python(6) == u'Ja' + assert v.from_python(1l) == u'Sehr gut' + + + # test_GermanTristateFloatComment + + + # test_UnsignedCommentNone + + + # test_GermanCachet + + + # test_url + + + def test_UrlNeinNone(self): + v = wrpylib.wrvalidators.UrlNeinNone() + assert v.to_python(u'') == None + assert v.to_python(u'Nein') == u'Nein' + assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at' + assert v.from_python(None) == u'' + assert v.from_python(u'Nein') == u'Nein' + assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at' + + + def test_ValueCommentListNeinLoopNone(self): + v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone() + assert v.to_python(u'') == None + assert v.to_python(u'Nein') == u'Nein' + assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1' + assert v.from_python(None) == u'' + assert v.from_python(u'Nein') == u'Nein' + assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1' + + + # test_PhoneNumber + + + def test_PhoneCommentListNeinLoopNone(self): + v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True) + assert v.to_python(u'') == None + assert v.to_python(u'Nein') == u'Nein' + assert v.to_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' + assert v.from_python(None) == u'' + assert v.from_python(u'Nein') == u'Nein' + assert v.from_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' + + + def test_MaskedEmail(self): + v = wrpylib.wrvalidators.MaskedEmail() + assert v.to_python(u'') == (None, None) + assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False) + assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True) + assert v.from_python((None, None)) == u'' + assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com' + assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com' + + + def test_EmailCommentListNeinLoopNone(self): + v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone() + assert v.to_python(u'') == None + assert v.to_python(u'Nein') == u'Nein' + assert v.to_python(u'first@example.com') == u'first@example.com' + assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com' + assert v.from_python(None) == u'' + assert v.from_python(u'Nein') == u'Nein' + assert v.from_python(u'first@example.com') == u'first@example.com' + assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com' + testvalue = u'abc.def(at)example.com (comment)' + try: + v.to_python(testvalue) + assert False + except formencode.Invalid: + pass + try: + v.from_python(testvalue) + assert False + except formencode.Invalid: + pass + v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True) + assert v.to_python(testvalue) == testvalue + assert v.from_python(testvalue) == testvalue + + + # test_WikiPage + + + # test_WikiPageList + + + def test_WikiPageListLoopNone(self): + v = wrpylib.wrvalidators.WikiPageListLoopNone() + assert v.to_python(u'') == None + assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]' + assert v.from_python(None) == u'' + assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]' + + + # test_TupleSecondValidator + + + def test_BoolUnicodeTupleValidator(self): + v = wrpylib.wrvalidators.BoolUnicodeTupleValidator() + assert v.to_python(u'') == (None, None) + assert v.to_python(u'Nein') == (False, None) + assert v.to_python(u'any text') == (True, u'any text') + assert v.from_python((None, None)) == u'' + assert v.from_python((False, None)) == u'Nein' + assert v.from_python((True, u'any text')) == u'any text' + + + + + def test_GermanLift(self): + v = wrpylib.wrvalidators.GermanLift() + assert v.to_python(u'') == (None, None) + assert v.to_python(u'Nein') == (False, None) + assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)') + assert v.from_python((None, None)) == u'' + assert v.from_python((False, None)) == u'Nein' + assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)' + + + def test_SledRental(self): + v = wrpylib.wrvalidators.SledRental() + assert v.to_python(u'') == (None, None) + assert v.to_python(u'Nein') == (False, None) + assert v.to_python(u'Ja') == (True, u'Ja') + assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm') + assert v.from_python((None, None)) == u'' + assert v.from_python((False, None)) == u'Nein' + assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm' + assert v.from_python((True, u'Ja')) == u'Ja' + + + def test_RodelbahnboxDictValidator(self): + v = wrpylib.wrvalidators.RodelbahnboxDictValidator() + other = collections.OrderedDict([ + (u'Position', u'47.309820 N 9.986508 E'), + (u'Position oben', u''), + (u'Höhe oben', u'1244'), + (u'Position unten', u''), + (u'Höhe unten', u'806'), + (u'Länge', u'5045'), + (u'Schwierigkeit', u''), + (u'Lawinen', u'gelegentlich'), + (u'Betreiber', u''), + (u'Öffentliche Anreise', u'Ja'), + (u'Aufstieg möglich', u'Ja'), + (u'Aufstieg getrennt', u'Nein'), + (u'Gehzeit', u'105'), + (u'Aufstiegshilfe', u'Nein'), + (u'Beleuchtungsanlage', u'Nein'), + (u'Beleuchtungstage', u''), + (u'Rodelverleih', u'Ja'), + (u'Gütesiegel', u''), + (u'Webauskunft', u''), + (u'Telefonauskunft', u'+43-664-1808482 (Bergkristallhütte)'), + (u'Bild', u'Rodelbahn Bergkristallhütte 2009-03-03.jpg'), + (u'In Übersichtskarte', u'Ja'), + (u'Forumid', u'72')]) + python = v.to_python(other, None) + other2 = v.from_python(python, None) + assert other == other2 + + + def test_GasthausboxDictValidator(self): + v = wrpylib.wrvalidators.GasthausboxDictValidator() + other = collections.OrderedDict([ + (u'Position', u'47.295549 N 9.986970 E'), + (u'Höhe', u'1250'), + (u'Betreiber', u''), + (u'Sitzplätze', u''), + (u'Übernachtung', u''), + (u'Rauchfrei', u'Nein'), + (u'Rodelverleih', u''), + (u'Handyempfang', u'A1; T-Mobile/Telering'), + (u'Homepage', u'http://www.bergkristallhuette.com/'), + (u'E-Mail', u'bergkristallhuette@gmx.at'), + (u'Telefon', u'+43-664-1808482'), + (u'Bild', u'Bergkritsallhütte 2009-02-07.JPG'), + (u'Rodelbahnen', u'[[Bergkristallhütte]]')]) + python = v.to_python(other, None) + other2 = v.from_python(python, None) + assert other == other2