1
+ import sys
2
+ from os import path
3
+ sys .path .append ( path .dirname ( path .dirname ( path .abspath (__file__ ) ) ) )
4
+
5
+ from numbers import Number
6
+
7
+ # from helpers import *
8
+
9
+ # from functools import reduce
10
+
11
+ import unittest
12
+ import ramda as R
13
+ import pandas as pd
14
+
15
+ import validium as V
16
+
17
+ # extensions for some missing ramda functions
18
+ R .isinstance = lambda x : lambda y : isinstance (y ,x )
19
+
20
+ class TestEverything (unittest .TestCase ):
21
+
22
+ # use `_test` prefix isntead of `test` (w/o leading underscore) so test runner doesn't use it
23
+ def _test_should_fail (self , fail_validators , foo ):
24
+ for validator in fail_validators :
25
+ with self .assertRaises (AssertionError ):
26
+ validator .validate (foo )
27
+
28
+ def _test_should_pass (self , pass_validators , foo ):
29
+ try :
30
+ for validator in pass_validators :
31
+ validator .validate (foo )
32
+ except :
33
+ self .fail ('validation should have passed but exception was raised' )
34
+
35
+ def test_base (self ):
36
+
37
+ foo = 3.14
38
+
39
+ pass_validators = [
40
+ V .Validator (
41
+ lambda x : isinstance (x , Number ),
42
+ 'must be a number'
43
+ ),
44
+
45
+ V .Validator (
46
+ lambda x : isinstance (x , float ),
47
+ 'must be a float'
48
+ ),
49
+
50
+ V .Validator (
51
+ lambda x : x > 0 and x < 100 ,
52
+ 'must be greater than 0 and less than 100'
53
+ ),
54
+
55
+ V .Validator (
56
+ lambda x : x == 3.14 ,
57
+ 'must equal 3.14'
58
+ ),
59
+ ]
60
+
61
+ self ._test_should_pass (pass_validators , foo )
62
+
63
+ fail_validators = [
64
+
65
+ V .Validator (
66
+ R .isinstance (str ),
67
+ 'must be a string'
68
+ ),
69
+
70
+ V .Validator (R .equals (42 ), 'must equal 42' ),
71
+
72
+ V .Validator (
73
+ lambda x : x < 0 ,
74
+ 'must be less than 0' ,
75
+ )
76
+ ]
77
+
78
+ self ._test_should_fail (fail_validators , foo )
79
+
80
+ def test_list (self ):
81
+
82
+ class Mystery :
83
+ pass
84
+
85
+ bars = [1 , 2 , .14 , None , 'hello' , 'world' ]
86
+
87
+ pass_validators = [
88
+ V .Validator (
89
+ lambda xs : isinstance (xs , list ),
90
+ 'must be a list'
91
+ ),
92
+
93
+ V .Validator (
94
+ lambda xs : len (xs ) == 6 ,
95
+ 'must be of length 6'
96
+ ),
97
+
98
+ V .Validator (
99
+ lambda xs : all ([not isinstance (x , Mystery ) for x in xs ]),
100
+ 'all must not be Mystery'
101
+ ),
102
+
103
+ V .Validator (
104
+ lambda xs : all ([x > 0 for x in filter (lambda x : isinstance (x , Number ), xs )]),
105
+ 'all numbers must be greater than 0'
106
+ ),
107
+
108
+ V .Validator (
109
+ lambda xs : ' ' .join (filter (lambda x : isinstance (x , str ), xs )) == 'hello world' ,
110
+ 'all strings joined with a space must equal "hello world"'
111
+ ),
112
+
113
+ V .Validator (
114
+ R .any (R .equals ('hello' )),
115
+ 'any must equal "hello"'
116
+ ),
117
+
118
+ V .Validator (
119
+ R .all (R .isinstance ((Number , type (None ), str ))),
120
+ 'all must be number, None or str'
121
+ ),
122
+
123
+ V .Validator (
124
+ R .pipe (R .filter (R .isinstance (Number )), R .sum , R .equals (3.14 )),
125
+ 'all numbers summed must equal 3.14'
126
+ ),
127
+
128
+ V .Validator (
129
+ R .pipe (R .filter (R .isinstance (str )), R .length , R .equals (2 )),
130
+ 'the count of str must be 2'
131
+ ),
132
+ ]
133
+
134
+ self ._test_should_pass (pass_validators , bars )
135
+
136
+ fail_validators = [
137
+ V .Validator (
138
+ R .all (R .pipe (R .isinstance (type (None )), R .negate )),
139
+ 'all must not be None'
140
+ ),
141
+
142
+ V .Validator (
143
+ R .any (R .isinstance (dict )),
144
+ 'any must be dict'
145
+ ),
146
+
147
+ V .Validator (
148
+ R .pipe (R .filter (R .isinstance (Number )), R .sum , R .equals (42 )),
149
+ 'all numbers summed must equal 42'
150
+ ),
151
+
152
+ V .Validator (
153
+ R .pipe (R .filter (R .isinstance (str )), R .length , R .equals (4 )),
154
+ 'the count of str must be 4'
155
+ ),
156
+
157
+ ]
158
+
159
+ self ._test_should_fail (fail_validators , bars )
160
+
161
+ unittest .main ()
0 commit comments