Browse Source

moved dbo conversion to dataaccess; added model conversion; tests

tags/v0.2.0
Robin Thoni 7 years ago
parent
commit
9412dd7cb9

+ 17
- 66
src/Business/lu-webapi-crud-business.js View File

@@ -14,105 +14,56 @@
14 14
 
15 15
                 Business.dataAccess = dataAccess;
16 16
 
17
-                Business.defaultDbo = {
18
-                    id: null,
19
-                    toString: function()
20
-                    {
21
-                        return this.id;
22
-                    }
23
-                };
24
-
25
-                Business.extendDeep = function extendDeep(dst) {
26
-                    angular.forEach(arguments, function(obj) {
27
-                        if (obj !== dst) {
28
-                            angular.forEach(obj, function(value, key) {
29
-                                if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
30
-                                    extendDeep(dst[key], value);
31
-                                } else {
32
-                                    dst[key] = value;
33
-                                }
34
-                            });
35
-                        }
36
-                    });
37
-                    return dst;
38
-                };
39
-
40
-                Business._initDbo = function(dbo)
41
-                {
42
-                    if (dbo == null) {
43
-                        return null;
44
-                    }
45
-                    dbo = Business.extendDeep({}, Business.defaultDbo, dbo);
46
-                    if (dbo.createdAt != null) {
47
-                        dbo.createdAt = new Date(dbo.createdAt);
48
-                    }
49
-                    if (dbo.updatedAt != null) {
50
-                        dbo.updatedAt = new Date(dbo.updatedAt);
51
-                    }
52
-                    return dbo;
53
-                };
54
-
55 17
                 Business.initDbo = function(dbo)
56 18
                 {
57
-                    return Business._initDbo(dbo);
19
+                    return Business.dataAccess.initDbo(dbo);
58 20
                 };
59 21
 
60 22
                 Business.initListDbo = function(list)
61 23
                 {
62
-                    var d = [];
63
-                    for (var i = 0; i < list.length; ++i) {
64
-                        d.push(Business.initDbo(list[i]));
65
-                    }
66
-                    return d;
24
+                    return Business.dataAccess.initListDbo(list);
67 25
                 };
68 26
 
69 27
                 Business.initPaginatedDbo = function(data)
70 28
                 {
71
-                    var d = {
72
-                        count: data.count,
73
-                        data: Business.initListDbo(data.data)
74
-                    };
75
-                    return d;
29
+                    return Business.dataAccess.initPaginatedDbo(data);
76 30
                 };
77 31
 
78 32
                 Business.thenInitDbo = function(promise)
79 33
                 {
80
-                    var deferred = $q.defer();
81
-                    promise.then(function(data)
82
-                    {
83
-                        deferred.resolve(Business.initDbo(data));
84
-                    }, deferred.reject);
85
-                    return deferred.promise;
34
+                    return Business.dataAccess.thenInitDbo(promise);
35
+                };
36
+
37
+                Business.thenInitListDbo = function(promise)
38
+                {
39
+                    return Business.dataAccess.thenInitListDbo(promise);
86 40
                 };
87 41
 
88 42
                 Business.thenInitPaginatedDbo = function(promise)
89 43
                 {
90
-                    var deferred = $q.defer();
91
-                    promise.then(function(data)
92
-                    {
93
-                        deferred.resolve(Business.initPaginatedDbo(data));
94
-                    }, deferred.reject);
95
-                    return deferred.promise;
44
+                    return Business.dataAccess.thenInitPaginatedDbo(promise);
96 45
                 };
97 46
 
47
+
48
+
98 49
                 Business.getSingleById = function (id, luBusyGroups) {
99
-                    return Business.thenInitDbo(Business.dataAccess.getSingleById(id, luBusyGroups));
50
+                    return Business.dataAccess.getSingleById(id, luBusyGroups);
100 51
                 };
101 52
 
102 53
                 Business.getMultiple = function (orderBy, filter, page, perPage, luBusyGroups) {
103
-                    return Business.thenInitPaginatedDbo(Business.dataAccess.getMultiple(orderBy, filter, page, perPage, luBusyGroups));
54
+                    return Business.dataAccess.getMultiple(orderBy, filter, page, perPage, luBusyGroups);
104 55
                 };
105 56
 
106 57
                 Business.addDbo = function(data, luBusyGroups) {
107
-                    return Business.thenInitDbo(Business.dataAccess.addDbo(data, luBusyGroups));
58
+                    return Business.dataAccess.addDbo(data, luBusyGroups);
108 59
                 };
109 60
 
110 61
                 Business.editSingleByIdDbo = function(id, data, luBusyGroups) {
111
-                    return Business.thenInitDbo(Business.dataAccess.editSingleByIdDbo(id, data, luBusyGroups));
62
+                    return Business.dataAccess.editSingleByIdDbo(id, data, luBusyGroups);
112 63
                 };
113 64
 
114 65
                 Business.deleteDbo = function(id, luBusyGroups) {
115
-                    return Business.thenInitDbo(Business.dataAccess.deleteDbo(id, luBusyGroups));
66
+                    return Business.dataAccess.deleteDbo(id, luBusyGroups);
116 67
                 };
117 68
 
118 69
                 return Business;

+ 172
- 7
src/DataAccess/lu-webapi-crud-dataaccess.js View File

@@ -5,36 +5,201 @@
5 5
 (function () {
6 6
     'use strict';
7 7
     angular.module('luticate2Utils')
8
-        .factory('luWebApiCrudDataAccess', ['luWebApiDataAccess', function (luWebApiDataAccess) {
8
+        .factory('luWebApiCrudDataAccess', ['luWebApiDataAccess', '$q', function (luWebApiDataAccess, $q) {
9 9
 
10 10
             var luWebApiCrudDataAccess = {};
11 11
 
12 12
             luWebApiCrudDataAccess.create = function (entry_point) {
13 13
                 var DataAccess = luWebApiDataAccess.create(entry_point);
14 14
 
15
+
16
+                DataAccess._initModel = function(model)
17
+                {
18
+                    if (model == null) {
19
+                        return null;
20
+                    }
21
+                    model = angular.copy(model);
22
+                    delete model.id;
23
+                    delete model.updatedAt;
24
+                    delete model.createdAt;
25
+                    delete model.toString;
26
+                    return model;
27
+                };
28
+
29
+                DataAccess.initModel = function(model)
30
+                {
31
+                    return DataAccess._initModel(model);
32
+                };
33
+
34
+                DataAccess.initListModel = function(list)
35
+                {
36
+                    var d = [];
37
+                    for (var i = 0; i < list.length; ++i) {
38
+                        d.push(DataAccess.initModel(list[i]));
39
+                    }
40
+                    return d;
41
+                };
42
+
43
+                DataAccess.initPaginatedModel = function(data)
44
+                {
45
+                    var d = {
46
+                        count: data.count,
47
+                        data: DataAccess.initListModel(data.data)
48
+                    };
49
+                    return d;
50
+                };
51
+
52
+                DataAccess.thenInitModel = function(promise)
53
+                {
54
+                    var deferred = $q.defer();
55
+                    promise.then(function(data)
56
+                    {
57
+                        deferred.resolve(DataAccess.initModel(data));
58
+                    }, deferred.reject);
59
+                    return deferred.promise;
60
+                };
61
+
62
+                DataAccess.thenInitListModel = function(promise)
63
+                {
64
+                    var deferred = $q.defer();
65
+                    promise.then(function(data)
66
+                    {
67
+                        deferred.resolve(DataAccess.initListModel(data));
68
+                    }, deferred.reject);
69
+                    return deferred.promise;
70
+                };
71
+
72
+                DataAccess.thenInitPaginatedModel = function(promise)
73
+                {
74
+                    var deferred = $q.defer();
75
+                    promise.then(function(data)
76
+                    {
77
+                        deferred.resolve(DataAccess.initPaginatedModel(data));
78
+                    }, deferred.reject);
79
+                    return deferred.promise;
80
+                };
81
+
82
+
83
+
84
+
85
+                DataAccess.defaultDbo = {
86
+                    id: null,
87
+                    toString: function()
88
+                    {
89
+                        return this.id;
90
+                    }
91
+                };
92
+
93
+                DataAccess.extendDeep = function extendDeep(dst) {
94
+                    angular.forEach(arguments, function(obj) {
95
+                        if (obj !== dst) {
96
+                            angular.forEach(obj, function(value, key) {
97
+                                if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
98
+                                    extendDeep(dst[key], value);
99
+                                } else {
100
+                                    dst[key] = value;
101
+                                }
102
+                            });
103
+                        }
104
+                    });
105
+                    return dst;
106
+                };
107
+
108
+                DataAccess._initDbo = function(dbo)
109
+                {
110
+                    if (dbo == null) {
111
+                        return null;
112
+                    }
113
+                    dbo = DataAccess.extendDeep({}, DataAccess.defaultDbo, dbo);
114
+                    if (dbo.createdAt != null) {
115
+                        dbo.createdAt = new Date(dbo.createdAt);
116
+                    }
117
+                    if (dbo.updatedAt != null) {
118
+                        dbo.updatedAt = new Date(dbo.updatedAt);
119
+                    }
120
+                    return dbo;
121
+                };
122
+
123
+                DataAccess.initDbo = function(dbo)
124
+                {
125
+                    return DataAccess._initDbo(dbo);
126
+                };
127
+
128
+                DataAccess.initListDbo = function(list)
129
+                {
130
+                    var d = [];
131
+                    for (var i = 0; i < list.length; ++i) {
132
+                        d.push(DataAccess.initDbo(list[i]));
133
+                    }
134
+                    return d;
135
+                };
136
+
137
+                DataAccess.initPaginatedDbo = function(data)
138
+                {
139
+                    var d = {
140
+                        count: data.count,
141
+                        data: DataAccess.initListDbo(data.data)
142
+                    };
143
+                    return d;
144
+                };
145
+
146
+                DataAccess.thenInitDbo = function(promise)
147
+                {
148
+                    var deferred = $q.defer();
149
+                    promise.then(function(data)
150
+                    {
151
+                        deferred.resolve(DataAccess.initDbo(data));
152
+                    }, deferred.reject);
153
+                    return deferred.promise;
154
+                };
155
+
156
+                DataAccess.thenInitListDbo = function(promise)
157
+                {
158
+                    var deferred = $q.defer();
159
+                    promise.then(function(data)
160
+                    {
161
+                        deferred.resolve(DataAccess.initListDbo(data));
162
+                    }, deferred.reject);
163
+                    return deferred.promise;
164
+                };
165
+
166
+                DataAccess.thenInitPaginatedDbo = function(promise)
167
+                {
168
+                    var deferred = $q.defer();
169
+                    promise.then(function(data)
170
+                    {
171
+                        deferred.resolve(DataAccess.initPaginatedDbo(data));
172
+                    }, deferred.reject);
173
+                    return deferred.promise;
174
+                };
175
+                
176
+                
177
+                
15 178
                 DataAccess.getSingleById = function (id, luBusyGroups) {
16
-                    return DataAccess.get(id, null, luBusyGroups);
179
+                    return DataAccess.thenInitDbo(DataAccess.get(id, null, luBusyGroups));
17 180
                 };
18 181
 
19 182
                 DataAccess.getMultiple = function (orderBy, filter, page, perPage, luBusyGroups) {
20
-                    return DataAccess.get('', {
183
+                    return DataAccess.thenInitPaginatedDbo(DataAccess.get('', {
21 184
                         orderBy: orderBy,
22 185
                         filter: filter,
23 186
                         page: page,
24 187
                         perPage: perPage
25
-                    }, luBusyGroups);
188
+                    }, luBusyGroups));
26 189
                 };
27 190
 
28 191
                 DataAccess.addDbo = function(data, luBusyGroups) {
29
-                    return DataAccess.post('', null, data, luBusyGroups);
192
+                    var model = DataAccess.initModel(data);
193
+                    return DataAccess.thenInitDbo(DataAccess.post('', null, model, luBusyGroups));
30 194
                 };
31 195
 
32 196
                 DataAccess.editSingleByIdDbo = function(id, data, luBusyGroups) {
33
-                    return DataAccess.post(id, null, data, luBusyGroups);
197
+                    var model = DataAccess.initModel(data);
198
+                    return DataAccess.thenInitDbo(DataAccess.post(id, null, model, luBusyGroups));
34 199
                 };
35 200
 
36 201
                 DataAccess.deleteDbo = function(id, luBusyGroups) {
37
-                    return DataAccess.delete(id, null, null, luBusyGroups);
202
+                    return DataAccess.thenInitDbo(DataAccess.delete(id, null, null, luBusyGroups));
38 203
                 };
39 204
 
40 205
                 return DataAccess;

tests/Business/lu-webapi-crud-business.spec.js → tests/DataAccess/lu-webapi-crud-dataaccess-dbo.spec.js View File

@@ -2,26 +2,76 @@
2 2
  * Created by robin on 12/11/16.
3 3
  */
4 4
 
5
-describe('lu-webapi-crud-business factory', function() {
6
-    var luWebApiCrudBusiness;
5
+describe('lu-webapi-crud-dataaccess model factory', function() {
6
+    var luWebApiCrudDataAccess;
7 7
     var fakeDataAccess;
8 8
     var $q;
9 9
     var $rootScope;
10 10
 
11
-    // Before each test load our api.users module
12 11
     beforeEach(angular.mock.module('luticate2Utils'));
13 12
 
14
-    // Before each test set our injected Users factory (_Users_) to our local Users variable
15
-    beforeEach(inject(function(_luWebApiCrudBusiness_, _$q_, _$rootScope_) {
16
-        luWebApiCrudBusiness = _luWebApiCrudBusiness_;
17
-        $q = _$q_;
18
-        $rootScope = _$rootScope_;
19
-    }));
13
+    beforeEach(function () {
14
+        module(function ($provide) {
15
+            $provide.value('luWebApiDataAccess', {
16
+                create: function (entry_point) {
17
+                    return {
18
+
19
+                        get: function (url, dataGet, luBusyGroups) {
20
+                            var deferred = $q.defer();
21
+                            if (url == '') {
22
+                                fakeDataAccess.getMultiple(dataGet.orderBy, dataGet.filter, dataGet.page, dataGet.perPage, deferred);
23
+                            }
24
+                            else {
25
+                                fakeDataAccess.getSingleById(url, deferred);
26
+                            }
27
+                            // deferred.resolve({
28
+                            //     get: dataGet,
29
+                            //     post: null,
30
+                            //     url: entry_point + url
31
+                            // });
32
+                            return deferred.promise;
33
+                        },
34
+
35
+                        post: function (url, dataGet, dataPost, luBusyGroups) {
36
+                            var deferred = $q.defer();
37
+                            if (url == '') {
38
+                                fakeDataAccess.addDbo(dataPost, deferred);
39
+                            }
40
+                            else {
41
+                                fakeDataAccess.editSingleByIdDbo(url, dataPost, deferred);
42
+                            }
43
+                            deferred.resolve({
44
+                                get: dataGet,
45
+                                post: dataPost,
46
+                                url: entry_point + url
47
+                            });
48
+                            return deferred.promise;
49
+                        },
50
+
51
+                        put: function (url, dataGet, dataPost, luBusyGroups) {
52
+                            var deferred = $q.defer();
53
+                            deferred.resolve({
54
+                                get: dataGet,
55
+                                post: dataPost,
56
+                                url: entry_point + url
57
+                            });
58
+                            return deferred.promise;
59
+                        },
60
+
61
+                        delete: function (url, dataGet, dataPost, luBusyGroups) {
62
+                            var deferred = $q.defer();
63
+                            fakeDataAccess.deleteDbo(url, deferred);
64
+                            return deferred.promise;
65
+                        }
66
+                    }
67
+                }
68
+            });
69
+        });
70
+    });
20 71
 
21 72
     beforeEach(function () {
22 73
         fakeDataAccess = {
23
-            getSingleById: function(id, luBusyGroups) {
24
-                var deferred = $q.defer();
74
+            getSingleById: function(id, deferred) {
25 75
                 if (id == 1) {
26 76
                     deferred.resolve({
27 77
                         id: id,
@@ -61,43 +111,48 @@ describe('lu-webapi-crud-business factory', function() {
61 111
                 }
62 112
                 return deferred.promise;
63 113
             },
64
-            getMultiple: function (orderBy, filter, page, perPage, luBusyGroups) {
65
-                var deferred = $q.defer();
114
+            getMultiple: function (orderBy, filter, page, perPage, deferred) {
66 115
                 deferred.resolve({
67 116
                     count: 42,
68 117
                     data: [{
69
-                            id: 1,
70
-                            name: "Test.",
71
-                            obj: {
72
-                                value: 4242
73
-                            }
74
-                        }, {
75
-                            id: 2,
76
-                            name: "Test.2",
77
-                            obj: {
78
-                                anotherValue: 2424
79
-                            }
80
-                        }]
118
+                        id: 1,
119
+                        name: "Test.",
120
+                        obj: {
121
+                            value: 4242
122
+                        }
123
+                    }, {
124
+                        id: 2,
125
+                        name: "Test.2",
126
+                        obj: {
127
+                            anotherValue: 2424
128
+                        }
129
+                    }]
81 130
                 });
82 131
                 return deferred.promise;
83 132
             },
84
-            addDbo: function(data, luBusyGroups) {
85
-                return this.getSingleById(1, luBusyGroups);
133
+            addDbo: function(data, deferred) {
134
+                return this.getSingleById(1, deferred);
86 135
             },
87
-            editSingleByIdDbo: function(id, data, luBusyGroups) {
88
-                return this.getSingleById(id, luBusyGroups);
136
+            editSingleByIdDbo: function(id, data, deferred) {
137
+                return this.getSingleById(id, deferred);
89 138
             },
90
-            deleteDbo: function(id, luBusyGroups) {
91
-                return this.getSingleById(id, luBusyGroups);
139
+            deleteDbo: function(id, deferred) {
140
+                return this.getSingleById(id, deferred);
92 141
             }
93 142
         };
94 143
     });
144
+    
145
+    beforeEach(inject(function(_luWebApiCrudDataAccess_, _$q_, _$rootScope_) {
146
+        luWebApiCrudDataAccess = _luWebApiCrudDataAccess_;
147
+        $q = _$q_;
148
+        $rootScope = _$rootScope_;
149
+    }));
95 150
 
96 151
 
97 152
     it('should add a dummy function to getSingleById dbo', function()
98 153
     {
99
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
100
-        business.defaultDbo = {
154
+        var dataAccess = luWebApiCrudDataAccess.create('');
155
+        dataAccess.defaultDbo = {
101 156
             id: null,
102 157
             name: "",
103 158
             dummyFunction: function () {
@@ -106,7 +161,7 @@ describe('lu-webapi-crud-business factory', function() {
106 161
         };
107 162
 
108 163
         var id = 1;
109
-        business.getSingleById(id).then(function (data) {
164
+        dataAccess.getSingleById(id).then(function (data) {
110 165
             expect(data.id).toEqual(id);
111 166
             expect(data.name).toEqual("Test.");
112 167
             expect(data.dummyFunction).not.toBeNull();
@@ -120,8 +175,8 @@ describe('lu-webapi-crud-business factory', function() {
120 175
 
121 176
     it('should test complex extend', function()
122 177
     {
123
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
124
-        business.defaultDbo = {
178
+        var dataAccess = luWebApiCrudDataAccess.create('');
179
+        dataAccess.defaultDbo = {
125 180
             id: null,
126 181
             name: "",
127 182
             obj: {
@@ -131,7 +186,7 @@ describe('lu-webapi-crud-business factory', function() {
131 186
         };
132 187
 
133 188
         var id = 2;
134
-        business.getSingleById(id).then(function (data) {
189
+        dataAccess.getSingleById(id).then(function (data) {
135 190
             expect(data.id).toEqual(id);
136 191
             expect(data.name).toEqual("Test.2");
137 192
             expect(data.obj).not.toBeNull();
@@ -146,8 +201,8 @@ describe('lu-webapi-crud-business factory', function() {
146 201
 
147 202
     it('should add a dummy function to getSingleById dbo', function()
148 203
     {
149
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
150
-        business.defaultDbo = {
204
+        var dataAccess = luWebApiCrudDataAccess.create('');
205
+        dataAccess.defaultDbo = {
151 206
             id: null,
152 207
             name: "",
153 208
             dummyFunction: function () {
@@ -155,7 +210,7 @@ describe('lu-webapi-crud-business factory', function() {
155 210
             }
156 211
         };
157 212
 
158
-        business.getMultiple(null, null, 0, 2).then(function (data) {
213
+        dataAccess.getMultiple(null, null, 0, 2).then(function (data) {
159 214
             expect(data.count).toEqual(42);
160 215
             expect(data.data.length).toEqual(2);
161 216
 
@@ -180,8 +235,8 @@ describe('lu-webapi-crud-business factory', function() {
180 235
 
181 236
     it('should add a dummy function to addDbo dbo', function()
182 237
     {
183
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
184
-        business.defaultDbo = {
238
+        var dataAccess = luWebApiCrudDataAccess.create('');
239
+        dataAccess.defaultDbo = {
185 240
             id: null,
186 241
             name: "",
187 242
             dummyFunction: function () {
@@ -190,7 +245,7 @@ describe('lu-webapi-crud-business factory', function() {
190 245
         };
191 246
 
192 247
         var id = 1;
193
-        business.addDbo(null).then(function (data) {
248
+        dataAccess.addDbo({}).then(function (data) {
194 249
             expect(data.id).toEqual(id);
195 250
             expect(data.name).toEqual("Test.");
196 251
             expect(data.dummyFunction).not.toBeNull();
@@ -204,8 +259,8 @@ describe('lu-webapi-crud-business factory', function() {
204 259
 
205 260
     it('should add a dummy function to editSingleByIdDbo dbo', function()
206 261
     {
207
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
208
-        business.defaultDbo = {
262
+        var dataAccess = luWebApiCrudDataAccess.create('');
263
+        dataAccess.defaultDbo = {
209 264
             id: null,
210 265
             name: "",
211 266
             dummyFunction: function () {
@@ -214,7 +269,7 @@ describe('lu-webapi-crud-business factory', function() {
214 269
         };
215 270
 
216 271
         var id = 1;
217
-        business.editSingleByIdDbo(id, null).then(function (data) {
272
+        dataAccess.editSingleByIdDbo(id, {}).then(function (data) {
218 273
             expect(data.id).toEqual(id);
219 274
             expect(data.name).toEqual("Test.");
220 275
             expect(data.dummyFunction).not.toBeNull();
@@ -228,8 +283,8 @@ describe('lu-webapi-crud-business factory', function() {
228 283
 
229 284
     it('should add a dummy function to deleteDbo dbo', function()
230 285
     {
231
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
232
-        business.defaultDbo = {
286
+        var dataAccess = luWebApiCrudDataAccess.create('');
287
+        dataAccess.defaultDbo = {
233 288
             id: null,
234 289
             name: "",
235 290
             dummyFunction: function () {
@@ -238,7 +293,7 @@ describe('lu-webapi-crud-business factory', function() {
238 293
         };
239 294
 
240 295
         var id = 1;
241
-        business.deleteDbo(id, null).then(function (data) {
296
+        dataAccess.deleteDbo(id, null).then(function (data) {
242 297
             expect(data.id).toEqual(id);
243 298
             expect(data.name).toEqual("Test.");
244 299
             expect(data.dummyFunction).not.toBeNull();
@@ -252,8 +307,8 @@ describe('lu-webapi-crud-business factory', function() {
252 307
 
253 308
     it('should convert createdAt into Date', function()
254 309
     {
255
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
256
-        business.defaultDbo = {
310
+        var dataAccess = luWebApiCrudDataAccess.create('');
311
+        dataAccess.defaultDbo = {
257 312
             id: null,
258 313
             name: "",
259 314
             createdAt: null,
@@ -264,7 +319,7 @@ describe('lu-webapi-crud-business factory', function() {
264 319
         };
265 320
 
266 321
         var id = 3;
267
-        business.getSingleById(id, null).then(function (data) {
322
+        dataAccess.getSingleById(id, null).then(function (data) {
268 323
             expect(data.id).toEqual(id);
269 324
             expect(data.name).toEqual("Test.2");
270 325
             expect(data.dummyFunction).not.toBeNull();
@@ -280,8 +335,8 @@ describe('lu-webapi-crud-business factory', function() {
280 335
 
281 336
     it('should convert createdAt and updatedAt into Date', function()
282 337
     {
283
-        var business = luWebApiCrudBusiness.create(fakeDataAccess);
284
-        business.defaultDbo = {
338
+        var dataAccess = luWebApiCrudDataAccess.create('');
339
+        dataAccess.defaultDbo = {
285 340
             id: null,
286 341
             name: "",
287 342
             createdAt: null,
@@ -292,7 +347,7 @@ describe('lu-webapi-crud-business factory', function() {
292 347
         };
293 348
 
294 349
         var id = 4;
295
-        business.getSingleById(id, null).then(function (data) {
350
+        dataAccess.getSingleById(id, null).then(function (data) {
296 351
             expect(data.id).toEqual(id);
297 352
             expect(data.name).toEqual("Test.2");
298 353
             expect(data.dummyFunction).not.toBeNull();

+ 125
- 0
tests/DataAccess/lu-webapi-crud-dataaccess-model.spec.js View File

@@ -0,0 +1,125 @@
1
+/**
2
+ * Created by robin on 12/11/16.
3
+ */
4
+
5
+describe('lu-webapi-crud-dataAccess dbo factory', function() {
6
+    var luWebApiCrudDataAccess;
7
+    var $q;
8
+    var $rootScope;
9
+
10
+    beforeEach(angular.mock.module('luticate2Utils'));
11
+
12
+    beforeEach(function () {
13
+        module(function ($provide) {
14
+            $provide.value('luWebApiDataAccess', {
15
+                create: function (entry_point) {
16
+                    return {
17
+
18
+                        get: function (url, dataGet, luBusyGroups) {
19
+                            var deferred = $q.defer();
20
+                            deferred.resolve({
21
+                                get: dataGet,
22
+                                post: null,
23
+                                url: entry_point + url
24
+                            });
25
+                            return deferred.promise;
26
+                        },
27
+
28
+                        post: function (url, dataGet, dataPost, luBusyGroups) {
29
+                            var deferred = $q.defer();
30
+                            deferred.resolve({
31
+                                get: dataGet,
32
+                                post: dataPost,
33
+                                url: entry_point + url
34
+                            });
35
+                            return deferred.promise;
36
+                        },
37
+
38
+                        put: function (url, dataGet, dataPost, luBusyGroups) {
39
+                            var deferred = $q.defer();
40
+                            deferred.resolve({
41
+                                get: dataGet,
42
+                                post: dataPost,
43
+                                url: entry_point + url
44
+                            });
45
+                            return deferred.promise;
46
+                        },
47
+
48
+                        delete: function (url, dataGet, dataPost, luBusyGroups) {
49
+                            var deferred = $q.defer();
50
+                            deferred.resolve({
51
+                                get: dataGet,
52
+                                post: dataPost,
53
+                                url: entry_point + url
54
+                            });
55
+                            return deferred.promise;
56
+                        }
57
+                    }
58
+                }
59
+            });
60
+        });
61
+    });
62
+
63
+    beforeEach(inject(function(_luWebApiCrudDataAccess_, _$q_, _$rootScope_) {
64
+        luWebApiCrudDataAccess = _luWebApiCrudDataAccess_;
65
+        $q = _$q_;
66
+        $rootScope = _$rootScope_;
67
+    }));
68
+
69
+
70
+    it('should check that removing fields does not affect dbo', function()
71
+    {
72
+        var dataAccess = luWebApiCrudDataAccess.create('/api/entities/');
73
+
74
+        dataAccess.defaultDbo = {};
75
+
76
+        dataAccess.addDbo({
77
+            someText: "42",
78
+            someInt: 42
79
+        }).then(function (data) {
80
+            expect(data).toEqual({
81
+                get: null,
82
+                post: {
83
+                    someText: "42",
84
+                    someInt: 42
85
+                },
86
+                url: '/api/entities/'
87
+            });
88
+        }, function (error) {
89
+            expect(error).toBeNull();
90
+        });
91
+        $rootScope.$digest();
92
+    });
93
+
94
+    it('should check that removing fields works', function()
95
+    {
96
+        var dataAccess = luWebApiCrudDataAccess.create('/api/entities/');
97
+
98
+        dataAccess.defaultDbo = {};
99
+
100
+        dataAccess.editSingleByIdDbo("xxx-x-x-x-xxx", {
101
+            id: "xxx-x-x-x-xxx",
102
+            someText: "42",
103
+            someInt: 42,
104
+            createdAt: new Date(),
105
+            updatedAt: null,
106
+            toString: function()
107
+            {
108
+                return this.someText;
109
+            }
110
+        }).then(function (data) {
111
+            expect(data).toEqual({
112
+                get: null,
113
+                post: {
114
+                    someText: "42",
115
+                    someInt: 42
116
+                },
117
+                url: '/api/entities/xxx-x-x-x-xxx'
118
+            });
119
+        }, function (error) {
120
+            expect(error).toBeNull();
121
+        });
122
+        $rootScope.$digest();
123
+    });
124
+
125
+});

Loading…
Cancel
Save