|
@@ -0,0 +1,142 @@
|
|
1
|
+"""
|
|
2
|
+ Tests for _LeObject and LeObject
|
|
3
|
+"""
|
|
4
|
+
|
|
5
|
+import unittest
|
|
6
|
+from unittest import TestCase
|
|
7
|
+from unittest.mock import patch
|
|
8
|
+
|
|
9
|
+import EditorialModel
|
|
10
|
+import leapi
|
|
11
|
+import leapi.test.utils
|
|
12
|
+from leapi.lecrud import _LeCrud
|
|
13
|
+
|
|
14
|
+class LeCrudTestCase(TestCase):
|
|
15
|
+ @classmethod
|
|
16
|
+ def setUpClass(cls):
|
|
17
|
+ """ Write the generated code in a temporary directory and import it """
|
|
18
|
+ cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
|
|
19
|
+ @classmethod
|
|
20
|
+ def tearDownClass(cls):
|
|
21
|
+ """ Remove the temporary directory created at class setup """
|
|
22
|
+ leapi.test.utils.cleanup(cls.tmpdir)
|
|
23
|
+
|
|
24
|
+ def test_split_query_filter(self):
|
|
25
|
+ """ Tests the _split_filter() classmethod """
|
|
26
|
+ import dyncode
|
|
27
|
+ query_results = {
|
|
28
|
+ 'Hello = world' : ('Hello', '=', 'world'),
|
|
29
|
+ 'hello <= "world"': ('hello', '<=', '"world"'),
|
|
30
|
+ '_he42_ll-o >= \'world"': ('_he42_ll-o', '>=', '\'world"'),
|
|
31
|
+ 'foo in ["foo", 42, \'bar\']': ('foo', ' in ', '["foo", 42, \'bar\']'),
|
|
32
|
+ ' bar42 < 42': ('bar42', '<', '42'),
|
|
33
|
+ ' _hidden > 1337': ('_hidden', '>', '1337'),
|
|
34
|
+ '_42 not in foobar': ('_42', ' not in ', 'foobar'),
|
|
35
|
+ 'hello in foo':('hello', ' in ', 'foo'),
|
|
36
|
+ "\t\t\thello\t\t\nin\nfoo\t\t\n\t":('hello', ' in ', 'foo'),
|
|
37
|
+ "hello \nnot\tin \nfoo":('hello', ' not in ', 'foo'),
|
|
38
|
+ 'hello != bar':('hello', '!=', 'bar'),
|
|
39
|
+ 'hello = "world>= <= != in not in"': ('hello', '=', '"world>= <= != in not in"'),
|
|
40
|
+ 'superior.parent = 13': ('superior.parent', '=', '13'),
|
|
41
|
+ }
|
|
42
|
+ for query, result in query_results.items():
|
|
43
|
+ res = dyncode.LeCrud._split_filter(query)
|
|
44
|
+ self.assertEqual(res, result, "When parsing the query : '%s' the returned value is different from the expected '%s'"%(query, result))
|
|
45
|
+
|
|
46
|
+ def test_invalid_split_query_filter(self):
|
|
47
|
+ """ Testing the _split_filter() method with invalid queries """
|
|
48
|
+ import dyncode
|
|
49
|
+ invalid_queries = [
|
|
50
|
+ '42 = 42',
|
|
51
|
+ '4hello = foo',
|
|
52
|
+ 'foo == bar',
|
|
53
|
+ 'hello >> world',
|
|
54
|
+ 'hello = ',
|
|
55
|
+ ' = world',
|
|
56
|
+ '=',
|
|
57
|
+ '42',
|
|
58
|
+ '"hello" = world',
|
|
59
|
+ 'foo.bar = 15',
|
|
60
|
+ ]
|
|
61
|
+ for query in invalid_queries:
|
|
62
|
+ with self.assertRaises(ValueError, msg='But the query was not valid : "%s"'%query):
|
|
63
|
+ dyncode.LeCrud._split_filter(query)
|
|
64
|
+
|
|
65
|
+ def test_field_is_relational(self):
|
|
66
|
+ """ Testing the test to check if a field is relational """
|
|
67
|
+ from dyncode import LeCrud
|
|
68
|
+
|
|
69
|
+ test_fields = {
|
|
70
|
+ 'superior.parent': True,
|
|
71
|
+ 'subordinate.parent': True,
|
|
72
|
+ 'foofoo.foo': False,
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ for field, eres in test_fields.items():
|
|
76
|
+ self.assertEqual(LeCrud._field_is_relational(field), eres)
|
|
77
|
+
|
|
78
|
+ def test_check_datas(self):
|
|
79
|
+ """ testing the check_datas* methods """
|
|
80
|
+ from dyncode import Publication, Numero, LeObject
|
|
81
|
+
|
|
82
|
+ datas = { 'titre':'foobar' }
|
|
83
|
+ Numero.check_datas_value(datas, False, False)
|
|
84
|
+ Numero.check_datas_value(datas, True, False)
|
|
85
|
+ with self.assertRaises(leapi.lecrud.LeApiDataCheckError):
|
|
86
|
+ Numero.check_datas_value({}, True)
|
|
87
|
+
|
|
88
|
+ def test_prepare_filters(self):
|
|
89
|
+ """ Testing the _prepare_filters() method """
|
|
90
|
+ from dyncode import Publication, Numero, LeObject, Personnes
|
|
91
|
+
|
|
92
|
+ #Simple filters
|
|
93
|
+ filters = [
|
|
94
|
+ 'lodel_id = 1',
|
|
95
|
+ 'superior.parent > 2'
|
|
96
|
+ ]
|
|
97
|
+
|
|
98
|
+ filt, rfilt = Numero._prepare_filters(filters)
|
|
99
|
+ self.assertEqual(filt, [('lodel_id', '=', '1')])
|
|
100
|
+ self.assertEqual(rfilt, [((leapi.leobject.REL_SUP,'parent'), '>', '2')])
|
|
101
|
+
|
|
102
|
+ #All fields, no relationnal and class given
|
|
103
|
+ filters = []
|
|
104
|
+ res_filt = []
|
|
105
|
+ for field in Numero._fields:
|
|
106
|
+ filters.append('%s=1'%field)
|
|
107
|
+ res_filt.append((field, '=', '1'))
|
|
108
|
+
|
|
109
|
+ filt, rfilt = Publication._prepare_filters(filters)
|
|
110
|
+ self.assertEqual(rfilt, [])
|
|
111
|
+ self.assertEqual(filt, res_filt)
|
|
112
|
+
|
|
113
|
+ #Mixed type filters (tuple and string)
|
|
114
|
+ filters = [
|
|
115
|
+ ('lodel_id', '<=', '0'),
|
|
116
|
+ 'subordinate.parent = 2',
|
|
117
|
+ ]
|
|
118
|
+
|
|
119
|
+ filt, rfilt = Numero._prepare_filters(filters)
|
|
120
|
+ self.assertEqual(filt, [('lodel_id', '<=', '0')])
|
|
121
|
+ self.assertEqual(rfilt, [((leapi.leobject.REL_SUB,'parent'), '=', '2')])
|
|
122
|
+
|
|
123
|
+ def test_prepare_filters_invalid(self):
|
|
124
|
+ """ Testing the _prepare_filters() method """
|
|
125
|
+ from dyncode import LeCrud, Publication, Numero, Personnes, LeObject
|
|
126
|
+
|
|
127
|
+ #Numero fields filters but no letype nor leclass given
|
|
128
|
+ filters = []
|
|
129
|
+ res_filt = []
|
|
130
|
+ for field in Numero._fields:
|
|
131
|
+ filters.append('%s=1'%field)
|
|
132
|
+ res_filt.append((field, '=', '1'))
|
|
133
|
+
|
|
134
|
+ with self.assertRaises(leapi.lecrud.LeApiDataCheckError):
|
|
135
|
+ LeObject._prepare_filters(filters)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+ #simply invalid filters
|
|
139
|
+ filters = ['hello world !']
|
|
140
|
+ with self.assertRaises(ValueError):
|
|
141
|
+ Personnes._prepare_filters(filters)
|
|
142
|
+
|