Skip to content

Commit 1fc1d33

Browse files
committed
adding in changes from @rhodgkins to match the admin SDK
1 parent 21c27b6 commit 1fc1d33

File tree

5 files changed

+266
-43
lines changed

5 files changed

+266
-43
lines changed

spec/v1/providers/database.spec.ts

Lines changed: 134 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,17 +639,52 @@ describe('Database Functions', () => {
639639
expect(subject.val()).to.equal(0);
640640
populate({ myKey: 0 });
641641
expect(subject.val()).to.deep.equal({ myKey: 0 });
642-
643-
// Null values are still reported as null.
644-
populate({ myKey: null });
645-
expect(subject.val()).to.deep.equal({ myKey: null });
646642
});
647643

648644
// Regression test: .val() was returning array of nulls when there's a property called length (BUG#37683995)
649645
it('should return correct values when data has "length" property', () => {
650646
populate({ length: 3, foo: 'bar' });
651647
expect(subject.val()).to.deep.equal({ length: 3, foo: 'bar' });
652648
});
649+
650+
it('should deal with null-values appropriately', () => {
651+
populate(null);
652+
expect(subject.val()).to.be.null;
653+
654+
populate({ myKey: null });
655+
expect(subject.val()).to.be.null;
656+
});
657+
658+
it('should deal with empty object values appropriately', () => {
659+
populate({});
660+
expect(subject.val()).to.be.null;
661+
662+
populate({ myKey: {} });
663+
expect(subject.val()).to.be.null;
664+
665+
populate({ myKey: { child: null } });
666+
expect(subject.val()).to.be.null;
667+
});
668+
669+
it('should deal with empty array values appropriately', () => {
670+
populate([]);
671+
expect(subject.val()).to.be.null;
672+
673+
populate({ myKey: [] });
674+
expect(subject.val()).to.be.null;
675+
676+
populate({ myKey: [null] });
677+
expect(subject.val()).to.be.null;
678+
679+
populate({ myKey: [{}] });
680+
expect(subject.val()).to.be.null;
681+
682+
populate({ myKey: [{ myKey: null }] });
683+
expect(subject.val()).to.be.null;
684+
685+
populate({ myKey: [{ myKey: {} }] });
686+
expect(subject.val()).to.be.null;
687+
});
653688
});
654689

655690
describe('#child(): DataSnapshot', () => {
@@ -676,14 +711,37 @@ describe('Database Functions', () => {
676711
});
677712

678713
it('should be false for a non-existent value', () => {
679-
populate({ a: { b: 'c' } });
714+
populate({ a: { b: 'c', nullChild: null } });
680715
expect(subject.child('d').exists()).to.be.false;
716+
expect(subject.child('nullChild').exists()).to.be.false;
681717
});
682718

683719
it('should be false for a value pathed beyond a leaf', () => {
684720
populate({ a: { b: 'c' } });
685721
expect(subject.child('a/b/c').exists()).to.be.false;
686722
});
723+
724+
it('should be false for an empty object value', () => {
725+
populate({ a: {} });
726+
expect(subject.child('a').exists()).to.be.false;
727+
728+
populate({ a: { child: null } });
729+
expect(subject.child('a').exists()).to.be.false;
730+
731+
populate({ a: { child: {} } });
732+
expect(subject.child('a').exists()).to.be.false;
733+
});
734+
735+
it('should be false for an empty array value', () => {
736+
populate({ a: [] });
737+
expect(subject.child('a').exists()).to.be.false;
738+
739+
populate({ a: [null] });
740+
expect(subject.child('a').exists()).to.be.false;
741+
742+
populate({ a: [{}] });
743+
expect(subject.child('a').exists()).to.be.false;
744+
});
687745
});
688746

689747
describe('#forEach(action: (a: DataSnapshot) => boolean): boolean', () => {
@@ -712,6 +770,17 @@ describe('Database Functions', () => {
712770

713771
expect(subject.forEach(counter)).to.equal(false);
714772
expect(count).to.eq(0);
773+
774+
populate({
775+
a: 'foo',
776+
nullChild: null,
777+
emptyObjectChild: {},
778+
emptyArrayChild: [],
779+
});
780+
count = 0;
781+
782+
expect(subject.forEach(counter)).to.equal(false);
783+
expect(count).to.eq(1);
715784
});
716785

717786
it('should cancel further enumeration if callback returns true', () => {
@@ -751,13 +820,51 @@ describe('Database Functions', () => {
751820

752821
describe('#numChildren()', () => {
753822
it('should be key count for objects', () => {
754-
populate({ a: 'b', c: 'd' });
823+
populate({
824+
a: 'b',
825+
c: 'd',
826+
nullChild: null,
827+
emptyObjectChild: {},
828+
emptyArrayChild: [],
829+
});
755830
expect(subject.numChildren()).to.eq(2);
756831
});
757832

758833
it('should be 0 for non-objects', () => {
759834
populate(23);
760835
expect(subject.numChildren()).to.eq(0);
836+
837+
populate({
838+
nullChild: null,
839+
emptyObjectChild: {},
840+
emptyArrayChild: [],
841+
});
842+
expect(subject.numChildren()).to.eq(0);
843+
});
844+
});
845+
846+
describe('#hasChildren()', () => {
847+
it('should true for objects', () => {
848+
populate({
849+
a: 'b',
850+
c: 'd',
851+
nullChild: null,
852+
emptyObjectChild: {},
853+
emptyArrayChild: [],
854+
});
855+
expect(subject.hasChildren()).to.be.true;
856+
});
857+
858+
it('should be false for non-objects', () => {
859+
populate(23);
860+
expect(subject.hasChildren()).to.be.false;
861+
862+
populate({
863+
nullChild: null,
864+
emptyObjectChild: {},
865+
emptyArrayChild: [],
866+
});
867+
expect(subject.hasChildren()).to.be.false;
761868
});
762869
});
763870

@@ -769,9 +876,17 @@ describe('Database Functions', () => {
769876
});
770877

771878
it('should return false if a child is missing', () => {
772-
populate({ a: 'b' });
879+
populate({
880+
a: 'b',
881+
nullChild: null,
882+
emptyObjectChild: {},
883+
emptyArrayChild: [],
884+
});
773885
expect(subject.hasChild('c')).to.be.false;
774886
expect(subject.hasChild('a/b')).to.be.false;
887+
expect(subject.hasChild('nullChild')).to.be.false;
888+
expect(subject.hasChild('emptyObjectChild')).to.be.false;
889+
expect(subject.hasChild('emptyArrayChild')).to.be.false;
775890
});
776891
});
777892

@@ -801,11 +916,21 @@ describe('Database Functions', () => {
801916

802917
describe('#toJSON(): Object', () => {
803918
it('should return the current value', () => {
804-
populate({ a: 'b' });
919+
populate({
920+
a: 'b',
921+
nullChild: null,
922+
emptyObjectChild: {},
923+
emptyArrayChild: [],
924+
});
805925
expect(subject.toJSON()).to.deep.equal(subject.val());
806926
});
807927
it('should be stringifyable', () => {
808-
populate({ a: 'b' });
928+
populate({
929+
a: 'b',
930+
nullChild: null,
931+
emptyObjectChild: {},
932+
emptyArrayChild: [],
933+
});
809934
expect(JSON.stringify(subject)).to.deep.equal('{"a":"b"}');
810935
});
811936
});

spec/v2/providers/database.spec.ts

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,70 @@ describe('database', () => {
146146
});
147147
});
148148

149-
describe('onOperation', () => {
149+
describe('makeEndpoint', () => {
150+
it('should create an endpoint with an instance wildcard', () => {
151+
const ep = database.makeEndpoint(
152+
database.writtenEventType,
153+
{
154+
region: 'us-central1',
155+
labels: { 1: '2' },
156+
},
157+
new PathPattern('foo/bar'),
158+
new PathPattern('{inst}')
159+
);
160+
161+
expect(ep).to.deep.equal({
162+
platform: 'gcfv2',
163+
labels: {
164+
1: '2',
165+
},
166+
region: ['us-central1'],
167+
eventTrigger: {
168+
eventType: database.writtenEventType,
169+
eventFilters: {},
170+
eventFilterPathPatterns: {
171+
ref: 'foo/bar',
172+
instance: '{inst}',
173+
},
174+
retry: false,
175+
},
176+
});
177+
});
178+
179+
it('should create an endpoint without an instance wildcard', () => {
180+
const ep = database.makeEndpoint(
181+
database.writtenEventType,
182+
{
183+
region: 'us-central1',
184+
labels: { 1: '2' },
185+
},
186+
new PathPattern('foo/bar'),
187+
new PathPattern('my-instance')
188+
);
189+
190+
expect(ep).to.deep.equal({
191+
platform: 'gcfv2',
192+
labels: {
193+
1: '2',
194+
},
195+
region: ['us-central1'],
196+
eventTrigger: {
197+
eventType: database.writtenEventType,
198+
eventFilters: {
199+
instance: 'my-instance',
200+
},
201+
eventFilterPathPatterns: {
202+
ref: 'foo/bar',
203+
},
204+
retry: false,
205+
},
206+
});
207+
});
208+
});
209+
210+
describe('onChangedOperation', () => {
150211
it('should create a function for a written event', () => {
151-
const func = database.onOperation(
212+
const func = database.onChangedOperation(
152213
database.writtenEventType,
153214
'/foo/{bar}/',
154215
(event) => 2
@@ -169,9 +230,9 @@ describe('database', () => {
169230
});
170231
});
171232

172-
it('should create a function for a created event', () => {
173-
const func = database.onOperation(
174-
database.createdEventType,
233+
it('should create a function for a updated event', () => {
234+
const func = database.onChangedOperation(
235+
database.updatedEventType,
175236
'/foo/{bar}/',
176237
(event) => 2
177238
);
@@ -180,7 +241,7 @@ describe('database', () => {
180241
platform: 'gcfv2',
181242
labels: {},
182243
eventTrigger: {
183-
eventType: database.createdEventType,
244+
eventType: database.updatedEventType,
184245
eventFilters: {},
185246
eventFilterPathPatterns: {
186247
ref: 'foo/{bar}',
@@ -191,9 +252,43 @@ describe('database', () => {
191252
});
192253
});
193254

194-
it('should create a function for a updated event', () => {
255+
it('should create a complex function', () => {
256+
const func = database.onChangedOperation(
257+
database.writtenEventType,
258+
{
259+
ref: '/foo/{path=**}/{bar}/',
260+
instance: 'my-instance',
261+
region: 'us-central1',
262+
cpu: 'gcf_gen1',
263+
minInstances: 2,
264+
},
265+
(event) => 2
266+
);
267+
268+
expect(func.__endpoint).to.deep.equal({
269+
platform: 'gcfv2',
270+
cpu: 'gcf_gen1',
271+
minInstances: 2,
272+
region: ['us-central1'],
273+
labels: {},
274+
eventTrigger: {
275+
eventType: database.writtenEventType,
276+
eventFilters: {
277+
instance: 'my-instance',
278+
},
279+
eventFilterPathPatterns: {
280+
ref: 'foo/{path=**}/{bar}',
281+
},
282+
retry: false,
283+
},
284+
});
285+
});
286+
});
287+
288+
describe('onOperation', () => {
289+
it('should create a function for a created event', () => {
195290
const func = database.onOperation(
196-
database.updatedEventType,
291+
database.createdEventType,
197292
'/foo/{bar}/',
198293
(event) => 2
199294
);
@@ -202,7 +297,7 @@ describe('database', () => {
202297
platform: 'gcfv2',
203298
labels: {},
204299
eventTrigger: {
205-
eventType: database.updatedEventType,
300+
eventType: database.createdEventType,
206301
eventFilters: {},
207302
eventFilterPathPatterns: {
208303
ref: 'foo/{bar}',
@@ -237,7 +332,7 @@ describe('database', () => {
237332

238333
it('should create a complex function', () => {
239334
const func = database.onOperation(
240-
database.writtenEventType,
335+
database.createdEventType,
241336
{
242337
ref: '/foo/{path=**}/{bar}/',
243338
instance: 'my-instance',
@@ -255,7 +350,7 @@ describe('database', () => {
255350
region: ['us-central1'],
256351
labels: {},
257352
eventTrigger: {
258-
eventType: database.writtenEventType,
353+
eventType: database.createdEventType,
259354
eventFilters: {
260355
instance: 'my-instance',
261356
},

0 commit comments

Comments
 (0)