17
17
'use strict' ;
18
18
19
19
var assert = require ( 'assert' ) ;
20
- var Snapshot = require ( '../../lib/compute/snapshot.js' ) ;
20
+ var mockery = require ( 'mockery' ) ;
21
+ var nodeutil = require ( 'util' ) ;
22
+
23
+ var ServiceObject = require ( '../../lib/common/service-object.js' ) ;
24
+
25
+ function FakeServiceObject ( ) {
26
+ this . calledWith_ = arguments ;
27
+ ServiceObject . apply ( this , arguments ) ;
28
+ }
29
+
30
+ nodeutil . inherits ( FakeServiceObject , ServiceObject ) ;
21
31
22
32
describe ( 'Snapshot' , function ( ) {
33
+ var Snapshot ;
34
+ var snapshot ;
35
+
23
36
var COMPUTE = { } ;
24
37
var SNAPSHOT_NAME = 'snapshot-name' ;
25
38
26
- var snapshot ;
39
+ before ( function ( ) {
40
+ mockery . registerMock ( '../common/service-object.js' , FakeServiceObject ) ;
41
+ mockery . enable ( {
42
+ useCleanCache : true ,
43
+ warnOnUnregistered : false
44
+ } ) ;
45
+
46
+ Snapshot = require ( '../../lib/compute/snapshot.js' ) ;
47
+ } ) ;
48
+
49
+ after ( function ( ) {
50
+ mockery . deregisterAll ( ) ;
51
+ mockery . disable ( ) ;
52
+ } ) ;
27
53
28
54
beforeEach ( function ( ) {
29
55
snapshot = new Snapshot ( COMPUTE , SNAPSHOT_NAME ) ;
@@ -38,32 +64,55 @@ describe('Snapshot', function() {
38
64
assert . strictEqual ( snapshot . name , SNAPSHOT_NAME ) ;
39
65
} ) ;
40
66
41
- it ( 'should default metadata to an empty object' , function ( ) {
42
- assert . strictEqual ( typeof snapshot . metadata , 'object' ) ;
43
- assert . strictEqual ( Object . keys ( snapshot . metadata ) . length , 0 ) ;
67
+ it ( 'should inherit from ServiceObject' , function ( ) {
68
+ var calledWith = snapshot . calledWith_ [ 0 ] ;
69
+
70
+ assert . strictEqual ( calledWith . parent , COMPUTE ) ;
71
+ assert . strictEqual ( calledWith . baseUrl , '/global/snapshots' ) ;
72
+ assert . strictEqual ( calledWith . id , SNAPSHOT_NAME ) ;
73
+ assert . deepEqual ( calledWith . methods , {
74
+ exists : true ,
75
+ get : true ,
76
+ getMetadata : true
77
+ } ) ;
78
+ } ) ;
79
+
80
+ it ( 'should allow creating for a Disk object snapshot' , function ( done ) {
81
+ var scope = {
82
+ constructor : {
83
+ name : 'Disk'
84
+ } ,
85
+ createSnapshot : function ( ) {
86
+ assert . strictEqual ( this , scope ) ;
87
+ done ( ) ;
88
+ }
89
+ } ;
90
+
91
+ var snapshot = new Snapshot ( scope , SNAPSHOT_NAME ) ;
92
+
93
+ var calledWith = snapshot . calledWith_ [ 0 ] ;
94
+ assert . strictEqual ( calledWith . methods . create , true ) ;
95
+
96
+ calledWith . createMethod ( ) ; // (scope.createSnapshot)
44
97
} ) ;
45
98
} ) ;
46
99
47
100
describe ( 'delete' , function ( ) {
48
- it ( 'should make the correct API request' , function ( done ) {
49
- snapshot . makeReq_ = function ( method , path , query , body ) {
50
- assert . strictEqual ( method , 'DELETE' ) ;
51
- assert . strictEqual ( path , '' ) ;
52
- assert . strictEqual ( query , null ) ;
53
- assert . strictEqual ( body , null ) ;
54
-
101
+ it ( 'should call ServiceObject.delete' , function ( done ) {
102
+ FakeServiceObject . prototype . delete = function ( ) {
103
+ assert . strictEqual ( this , snapshot ) ;
55
104
done ( ) ;
56
105
} ;
57
106
58
- snapshot . delete ( assert . ifError ) ;
107
+ snapshot . delete ( ) ;
59
108
} ) ;
60
109
61
110
describe ( 'error' , function ( ) {
62
111
var error = new Error ( 'Error.' ) ;
63
112
var apiResponse = { a : 'b' , c : 'd' } ;
64
113
65
114
beforeEach ( function ( ) {
66
- snapshot . makeReq_ = function ( method , path , query , body , callback ) {
115
+ FakeServiceObject . prototype . delete = function ( callback ) {
67
116
callback ( error , apiResponse ) ;
68
117
} ;
69
118
} ) ;
@@ -88,7 +137,7 @@ describe('Snapshot', function() {
88
137
var apiResponse = { name : 'operation-name' } ;
89
138
90
139
beforeEach ( function ( ) {
91
- snapshot . makeReq_ = function ( method , path , query , body , callback ) {
140
+ FakeServiceObject . prototype . delete = function ( callback ) {
92
141
callback ( null , apiResponse ) ;
93
142
} ;
94
143
} ) ;
@@ -119,105 +168,4 @@ describe('Snapshot', function() {
119
168
} ) ;
120
169
} ) ;
121
170
} ) ;
122
-
123
- describe ( 'getMetadata' , function ( ) {
124
- it ( 'should make the correct API request' , function ( done ) {
125
- snapshot . makeReq_ = function ( method , path , query , body ) {
126
- assert . strictEqual ( method , 'GET' ) ;
127
- assert . strictEqual ( path , '' ) ;
128
- assert . strictEqual ( query , null ) ;
129
- assert . strictEqual ( body , null ) ;
130
-
131
- done ( ) ;
132
- } ;
133
-
134
- snapshot . getMetadata ( assert . ifError ) ;
135
- } ) ;
136
-
137
- describe ( 'error' , function ( ) {
138
- var error = new Error ( 'Error.' ) ;
139
- var apiResponse = { a : 'b' , c : 'd' } ;
140
-
141
- beforeEach ( function ( ) {
142
- snapshot . makeReq_ = function ( method , path , query , body , callback ) {
143
- callback ( error , apiResponse ) ;
144
- } ;
145
- } ) ;
146
-
147
- it ( 'should execute callback with error and API response' , function ( done ) {
148
- snapshot . getMetadata ( function ( err , metadata , apiResponse_ ) {
149
- assert . strictEqual ( err , error ) ;
150
- assert . strictEqual ( metadata , null ) ;
151
- assert . strictEqual ( apiResponse_ , apiResponse ) ;
152
- done ( ) ;
153
- } ) ;
154
- } ) ;
155
-
156
- it ( 'should not require a callback' , function ( ) {
157
- assert . doesNotThrow ( function ( ) {
158
- snapshot . getMetadata ( ) ;
159
- } ) ;
160
- } ) ;
161
- } ) ;
162
-
163
- describe ( 'success' , function ( ) {
164
- var apiResponse = { a : 'b' , c : 'd' } ;
165
-
166
- beforeEach ( function ( ) {
167
- snapshot . makeReq_ = function ( method , path , query , body , callback ) {
168
- callback ( null , apiResponse ) ;
169
- } ;
170
- } ) ;
171
-
172
- it ( 'should update the metadata to the API response' , function ( done ) {
173
- snapshot . getMetadata ( function ( err ) {
174
- assert . ifError ( err ) ;
175
- assert . strictEqual ( snapshot . metadata , apiResponse ) ;
176
- done ( ) ;
177
- } ) ;
178
- } ) ;
179
-
180
- it ( 'should exec callback with metadata and API response' , function ( done ) {
181
- snapshot . getMetadata ( function ( err , metadata , apiResponse_ ) {
182
- assert . ifError ( err ) ;
183
- assert . strictEqual ( metadata , apiResponse ) ;
184
- assert . strictEqual ( apiResponse_ , apiResponse ) ;
185
- done ( ) ;
186
- } ) ;
187
- } ) ;
188
-
189
- it ( 'should not require a callback' , function ( ) {
190
- assert . doesNotThrow ( function ( ) {
191
- snapshot . getMetadata ( ) ;
192
- } ) ;
193
- } ) ;
194
- } ) ;
195
- } ) ;
196
-
197
- describe ( 'makeReq_' , function ( ) {
198
- it ( 'should make the correct request to Compute' , function ( done ) {
199
- var expectedPathPrefix = '/global/snapshots/' + snapshot . name ;
200
-
201
- var method = 'POST' ;
202
- var path = '/test' ;
203
- var query = {
204
- a : 'b' ,
205
- c : 'd'
206
- } ;
207
- var body = {
208
- a : 'b' ,
209
- c : 'd'
210
- } ;
211
-
212
- snapshot . compute . makeReq_ = function ( method_ , path_ , query_ , body_ , cb ) {
213
- assert . strictEqual ( method_ , method ) ;
214
- assert . strictEqual ( path_ , expectedPathPrefix + path ) ;
215
- assert . strictEqual ( query_ , query ) ;
216
- assert . strictEqual ( body_ , body ) ;
217
- cb ( ) ;
218
- } ;
219
-
220
- snapshot . makeReq_ ( method , path , query , body , done ) ;
221
- } ) ;
222
- } ) ;
223
171
} ) ;
0 commit comments