iro/tests/model_validate.py
author Sandro Knauß <bugs@sandroknauss.de>
Tue, 15 Apr 2025 01:22:19 +0200
changeset 316 63c681b8e92c
parent 294 0e75bd39767d
permissions -rw-r--r--
fix link to about.

# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
# 
# This file is part of Iro.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# #Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from iro.model.schema import Offer
from iro.model.decorators import vRoute, vTyp
from iro.model.pool import data

from iro.error import ValidateException

from ..test_helpers.dbtestcase import DBTestCase
from ..test_helpers.utils import DummyPool

class ModelVaidatorTest(DBTestCase):
    """tests for the model vaidators"""
    def setUp(self):
        DBTestCase.setUp(self)
        self.pool = data.pool
        data.pool = DummyPool()

    def tearDown(self):
        data.pool = self.pool
        self.pool = None
        DBTestCase.tearDown(self)

    def testTyp(self):
        with self.session() as session:
            session.add(Offer(name="t",provider="p",typ="type"))

        with self.session() as session:
            self.assertEqual(vTyp("type",None),"type")
            e = self.assertRaises(ValidateException,vTyp, "sss", None)
            self.assertEqual(str(e),'700: Typ sss is not valid.')

    def testRoute(self):
        with self.session() as session:
            session.add(Offer(name="t",provider="p",typ="type"))
        self.assertEqual(vRoute("t",None,typ="type"),"t")
        self.assertEqual(vRoute(["t","t"],None,typ="type"),["t"])
        e = self.assertRaises(ValidateException,vRoute, "s", None, typ="type")
        self.assertEqual(str(e),'700: Route s is not valid.')

    def testRouteAllow(self):
        with self.session() as session:
            session.add(Offer(name="t",provider="p",typ="type"))
        e = self.assertRaises(ValidateException,vRoute, "t", "foo", typ="type", allowString=False)
        self.assertEqual(str(e),'700: foo must be a list of routes.')
        e = self.assertRaises(ValidateException,vRoute, ["t"], "foo", typ="type", allowList=False)
        self.assertEqual(str(e),'700: foo must be a route - No list of routes.')

    def testRouteProvider(self):
        with self.session() as session:
            session.add(Offer(name="t",provider="p",typ="type"))
        self.assertEqual(vRoute("p",None,typ="type"),"p")

    def testRouteDefault(self):
        self.assertEqual(vRoute("default",None,typ="type"),"default")