summaryrefslogtreecommitdiffstats
path: root/funcweb/funcweb/tests/test_widget_validation.py
blob: d9e2d8ccda83a7c00a2dd177754d18dabe28e727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import unittest
import turbogears
from turbogears import testutil
from funcweb.widget_validation import WidgetSchemaFactory,MinionIntValidator
from turbogears import validators 

class TestWidgetValidator(unittest.TestCase):

    def test_string_validator(self):
        wf = WidgetSchemaFactory(self.get_string_params())
        schema_man=wf.get_ready_schema()
        
        conversion_schema = {
                'max_length':'max',
                'min_length':'min',
                'validator':'regex'
                }

        #do better test here
        for argument_name,arg_options in self.get_string_params().iteritems():
            #print argument_name
            assert hasattr(schema_man,argument_name)==True
            #not very efficient but it si just a test :)
            if argument_name != 'string_mix':
                for arg,value in arg_options.iteritems():
                    #print getattr(schema_man,argument_name)
                    if conversion_schema.has_key(arg):
                        if hasattr(getattr(schema_man,argument_name),conversion_schema[arg]):
                            #print arg,value
                            #couldnt find a way to test it !??
                            if arg != 'validator':
                                assert getattr(getattr(schema_man,argument_name),conversion_schema[arg])==value
                                #print getattr(getattr(schema_man,argument_name),conversion_schema[arg])
            else:
                #just print it to see what is inside because the test will be very hardcoded otherwise
                #print getattr(schema_man,argument_name)
                continue
        print "Happy tests !"


    def test_minion_int_validator(self):
        mv=MinionIntValidator(max_int = 44,min_int=2)
        self.assertRaises(validators.Invalid,mv.to_python,100)
        self.assertRaises(validators.Invalid,mv.to_python,1)
        self.assertRaises(validators.Invalid,mv.to_python,'some_string')
        assert mv.to_python(21) == 21
        
        #dont use the min
        mv=MinionIntValidator(max_int = 44)
        self.assertRaises(validators.Invalid,mv.to_python,100)
        assert mv.to_python(1)==1
        self.assertRaises(validators.Invalid,mv.to_python,'some_string')
        assert mv.to_python(21) == 21
        
        mv=MinionIntValidator(min_int=12)
        self.assertRaises(validators.Invalid,mv.to_python,10)
        assert mv.to_python(14)==14
        self.assertRaises(validators.Invalid,mv.to_python,'some_string')
        assert mv.to_python(21) == 21
        
        mv=MinionIntValidator()
        assert mv.to_python(14)==14
        self.assertRaises(validators.Invalid,mv.to_python,'some_string')
        
        
    def get_string_params(self):
        return {
                'string_default':{
                    'type':'string',
                    'default':'default string',
                    'description':'default description'
                    },
                'string_regex':{
                    'type':'string',
                    'default':'some',
                    'validator':'^[a-z]*$'
                    }
                    ,
                #will be converted to dropdown
                'min_max_string':{
                    'type':'string',
                    'default':'myfirst',
                    'optional':False,
                    'description':'default dropdown list',
                    'max_length':12,
                    'min_length':5
                    },
                'string_mix':{
                    'type':'string',
                    'optional':False,
                    'max_length':123,
                    'min_length':12,
                    'validator':'^[A-Z]+$'
                    }
                }