1
- var extend = require ( 'util' ) . _extend ;
2
- var async = require ( 'async ' ) ;
1
+ const extend = require ( 'util' ) . _extend ;
2
+ const Promise = require ( 'bluebird ' ) ;
3
3
4
4
module . exports = exports = TestDataBuilder ;
5
5
@@ -46,7 +46,7 @@ function TestDataBuilder() {
46
46
* for required properties not listed.
47
47
* @return TestDataBuilder (fluent interface)
48
48
*/
49
- TestDataBuilder . prototype . define = function ( name , Model , properties ) {
49
+ TestDataBuilder . prototype . define = function ( name , Model , properties ) {
50
50
this . _definitions . push ( {
51
51
name : name ,
52
52
model : Model ,
@@ -61,7 +61,7 @@ TestDataBuilder.prototype.define = function(name, Model, properties) {
61
61
* is the name passed to `define()` and {property} is the name of
62
62
* the property to use.
63
63
*/
64
- TestDataBuilder . ref = function ( path ) {
64
+ TestDataBuilder . ref = function ( path ) {
65
65
return new Reference ( path ) ;
66
66
} ;
67
67
@@ -70,36 +70,31 @@ TestDataBuilder.ref = function(path) {
70
70
* the supplied context object.
71
71
* @param {Object.<string, Object> } context The context to object to populate.
72
72
* @param {function(Error) } callback Callback.
73
+ * @return {Promise }
73
74
*/
74
- TestDataBuilder . prototype . buildTo = function ( context , callback ) {
75
+ TestDataBuilder . prototype . buildTo = function ( context , callback ) {
75
76
this . _context = context ;
76
- async . eachSeries (
77
- this . _definitions ,
78
- this . _buildObject . bind ( this ) ,
79
- callback ) ;
77
+ return Promise . mapSeries ( this . _definitions , this . _buildObject . bind ( this ) ) . nodeify ( callback ) ;
80
78
} ;
81
79
82
- TestDataBuilder . prototype . _buildObject = function ( definition , callback ) {
80
+ TestDataBuilder . prototype . _buildObject = function ( definition , callback ) {
83
81
var defaultValues = this . _gatherDefaultPropertyValues ( definition . model ) ;
84
82
var values = extend ( defaultValues , definition . properties || { } ) ;
85
83
var resolvedValues = this . _resolveValues ( values ) ;
86
-
87
- definition . model . create ( resolvedValues , function ( err , result ) {
88
- if ( err ) {
89
- console . error (
90
- 'Cannot build object %j - %s\nDetails: %j' ,
91
- definition ,
92
- err . message ,
93
- err . details ) ;
94
- } else {
95
- this . _context [ definition . name ] = result ;
96
- }
97
-
98
- callback ( err ) ;
99
- } . bind ( this ) ) ;
84
+ var that = this ;
85
+
86
+ return Promise . resolve ( definition . model . create ( resolvedValues ) ) . then ( function ( result ) {
87
+ that . _context [ definition . name ] = result ;
88
+ } ) . catch ( function ( err ) {
89
+ console . error (
90
+ 'Cannot build object %j - %s\nDetails: %j' ,
91
+ definition ,
92
+ err . message ,
93
+ err . details ) ;
94
+ } ) . nodeify ( callback ) ;
100
95
} ;
101
96
102
- TestDataBuilder . prototype . _resolveValues = function ( values ) {
97
+ TestDataBuilder . prototype . _resolveValues = function ( values ) {
103
98
var result = { } ;
104
99
for ( var key in values ) {
105
100
var val = values [ key ] ;
@@ -112,7 +107,7 @@ TestDataBuilder.prototype._resolveValues = function(values) {
112
107
} ;
113
108
114
109
var valueCounter = 0 ;
115
- TestDataBuilder . prototype . _gatherDefaultPropertyValues = function ( Model ) {
110
+ TestDataBuilder . prototype . _gatherDefaultPropertyValues = function ( Model ) {
116
111
var result = { } ;
117
112
Model . forEachProperty ( function createDefaultPropertyValue ( name ) {
118
113
var prop = Model . definition . properties [ name ] ;
@@ -163,11 +158,11 @@ function Reference(path) {
163
158
this . _path = path ;
164
159
}
165
160
166
- Reference . prototype . resolveFromContext = function ( context ) {
161
+ Reference . prototype . resolveFromContext = function ( context ) {
167
162
var elements = this . _path . split ( '.' ) ;
168
163
169
164
var result = elements . reduce (
170
- function ( obj , prop ) {
165
+ function ( obj , prop ) {
171
166
return obj [ prop ] ;
172
167
} ,
173
168
context
0 commit comments