2
2
from pyexcel_io .djangobook import DjangoModelReader , DjangoModelWriter , DjangoBookReader , DjangoBookWriter
3
3
4
4
5
+ class Package :
6
+ def __init__ (self , raiseException = False , ** keywords ):
7
+ self .keywords = keywords
8
+ self .raiseException = raiseException
9
+
10
+ def get_content (self ):
11
+ return self .keywords
12
+
13
+ def save (self ):
14
+ if self .raiseException :
15
+ raise Exception ("test exception" )
16
+ else :
17
+ pass
18
+
19
+
5
20
class Attributable :
6
21
def __init__ (self , adict ):
7
22
self .mydict = adict
8
23
9
24
def __getattr__ (self , field ):
10
25
return self .mydict [field ]
11
26
27
+
12
28
class Objects :
13
29
def __init__ (self ):
14
30
self .objs = []
15
31
16
32
def bulk_create (self , objs , batch_size ):
17
- self .objs = objs
33
+ self .objs = [ o . get_content () for o in objs ]
18
34
self .batch_size = batch_size
19
35
20
36
def all (self ):
21
37
return [Attributable (o ) for o in self .objs ]
22
38
39
+
23
40
class Field :
24
41
def __init__ (self , name ):
25
42
self .attname = name
26
43
44
+
27
45
class Meta :
28
46
def __init__ (self ):
29
47
self .model_name = "test"
@@ -33,13 +51,54 @@ def update(self, data):
33
51
for f in data :
34
52
self .concrete_fields .append (Field (f ))
35
53
54
+
36
55
class FakeDjangoModel :
37
56
def __init__ (self ):
38
57
self .objects = Objects ()
39
58
self ._meta = Meta ()
40
59
41
60
def __call__ (self , ** keywords ):
42
- return keywords
61
+ return Package (** keywords )
62
+
63
+
64
+ class ExceptionObjects (Objects ):
65
+ def bulk_create (self , objs , batch_size ):
66
+ raise Exception ("faked exception" )
67
+
68
+
69
+ class FakeExceptionDjangoModel (FakeDjangoModel ):
70
+ def __init__ (self , raiseException = False ):
71
+ self .objects = ExceptionObjects ()
72
+ self ._meta = Meta ()
73
+ self .raiseException = raiseException
74
+
75
+ def __call__ (self , ** keywords ):
76
+ return Package (raiseExcpetion = self .raiseException ,
77
+ ** keywords )
78
+
79
+
80
+ class TestException :
81
+ def setUp (self ):
82
+ self .data = [
83
+ ["X" , "Y" , "Z" ],
84
+ [1 , 2 , 3 ],
85
+ [4 , 5 , 6 ]
86
+ ]
87
+ self .result = [
88
+ {'Y' : 2 , 'X' : 1 , 'Z' : 3 },
89
+ {'Y' : 5 , 'X' : 4 , 'Z' : 6 }
90
+ ]
91
+
92
+ def test_sheet_save_to_django_model (self ):
93
+ model = FakeExceptionDjangoModel ()
94
+ writer = DjangoModelWriter ([model , self .data [0 ], None , None ])
95
+ writer .write_array (self .data [1 :])
96
+ writer .close ()
97
+ # now raise excpetion
98
+ model = FakeExceptionDjangoModel (raiseException = True )
99
+ writer = DjangoModelWriter ([model , self .data [0 ], None , None ])
100
+ writer .write_array (self .data [1 :])
101
+ writer .close ()
43
102
44
103
45
104
class TestSheet :
0 commit comments