Skip to content

Commit

Permalink
adding in changes from @rhodgkins to match the admin SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
colerogers committed Jun 8, 2022
1 parent 21c27b6 commit 1fc1d33
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 43 deletions.
143 changes: 134 additions & 9 deletions spec/v1/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,17 +639,52 @@ describe('Database Functions', () => {
expect(subject.val()).to.equal(0);
populate({ myKey: 0 });
expect(subject.val()).to.deep.equal({ myKey: 0 });

// Null values are still reported as null.
populate({ myKey: null });
expect(subject.val()).to.deep.equal({ myKey: null });
});

// Regression test: .val() was returning array of nulls when there's a property called length (BUG#37683995)
it('should return correct values when data has "length" property', () => {
populate({ length: 3, foo: 'bar' });
expect(subject.val()).to.deep.equal({ length: 3, foo: 'bar' });
});

it('should deal with null-values appropriately', () => {
populate(null);
expect(subject.val()).to.be.null;

populate({ myKey: null });
expect(subject.val()).to.be.null;
});

it('should deal with empty object values appropriately', () => {
populate({});
expect(subject.val()).to.be.null;

populate({ myKey: {} });
expect(subject.val()).to.be.null;

populate({ myKey: { child: null } });
expect(subject.val()).to.be.null;
});

it('should deal with empty array values appropriately', () => {
populate([]);
expect(subject.val()).to.be.null;

populate({ myKey: [] });
expect(subject.val()).to.be.null;

populate({ myKey: [null] });
expect(subject.val()).to.be.null;

populate({ myKey: [{}] });
expect(subject.val()).to.be.null;

populate({ myKey: [{ myKey: null }] });
expect(subject.val()).to.be.null;

populate({ myKey: [{ myKey: {} }] });
expect(subject.val()).to.be.null;
});
});

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

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

it('should be false for a value pathed beyond a leaf', () => {
populate({ a: { b: 'c' } });
expect(subject.child('a/b/c').exists()).to.be.false;
});

it('should be false for an empty object value', () => {
populate({ a: {} });
expect(subject.child('a').exists()).to.be.false;

populate({ a: { child: null } });
expect(subject.child('a').exists()).to.be.false;

populate({ a: { child: {} } });
expect(subject.child('a').exists()).to.be.false;
});

it('should be false for an empty array value', () => {
populate({ a: [] });
expect(subject.child('a').exists()).to.be.false;

populate({ a: [null] });
expect(subject.child('a').exists()).to.be.false;

populate({ a: [{}] });
expect(subject.child('a').exists()).to.be.false;
});
});

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

expect(subject.forEach(counter)).to.equal(false);
expect(count).to.eq(0);

populate({
a: 'foo',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
count = 0;

expect(subject.forEach(counter)).to.equal(false);
expect(count).to.eq(1);
});

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

describe('#numChildren()', () => {
it('should be key count for objects', () => {
populate({ a: 'b', c: 'd' });
populate({
a: 'b',
c: 'd',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.numChildren()).to.eq(2);
});

it('should be 0 for non-objects', () => {
populate(23);
expect(subject.numChildren()).to.eq(0);

populate({
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.numChildren()).to.eq(0);
});
});

describe('#hasChildren()', () => {
it('should true for objects', () => {
populate({
a: 'b',
c: 'd',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.hasChildren()).to.be.true;
});

it('should be false for non-objects', () => {
populate(23);
expect(subject.hasChildren()).to.be.false;

populate({
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.hasChildren()).to.be.false;
});
});

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

it('should return false if a child is missing', () => {
populate({ a: 'b' });
populate({
a: 'b',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.hasChild('c')).to.be.false;
expect(subject.hasChild('a/b')).to.be.false;
expect(subject.hasChild('nullChild')).to.be.false;
expect(subject.hasChild('emptyObjectChild')).to.be.false;
expect(subject.hasChild('emptyArrayChild')).to.be.false;
});
});

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

describe('#toJSON(): Object', () => {
it('should return the current value', () => {
populate({ a: 'b' });
populate({
a: 'b',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(subject.toJSON()).to.deep.equal(subject.val());
});
it('should be stringifyable', () => {
populate({ a: 'b' });
populate({
a: 'b',
nullChild: null,
emptyObjectChild: {},
emptyArrayChild: [],
});
expect(JSON.stringify(subject)).to.deep.equal('{"a":"b"}');
});
});
Expand Down
117 changes: 106 additions & 11 deletions spec/v2/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,70 @@ describe('database', () => {
});
});

describe('onOperation', () => {
describe('makeEndpoint', () => {
it('should create an endpoint with an instance wildcard', () => {
const ep = database.makeEndpoint(
database.writtenEventType,
{
region: 'us-central1',
labels: { 1: '2' },
},
new PathPattern('foo/bar'),
new PathPattern('{inst}')
);

expect(ep).to.deep.equal({
platform: 'gcfv2',
labels: {
1: '2',
},
region: ['us-central1'],
eventTrigger: {
eventType: database.writtenEventType,
eventFilters: {},
eventFilterPathPatterns: {
ref: 'foo/bar',
instance: '{inst}',
},
retry: false,
},
});
});

it('should create an endpoint without an instance wildcard', () => {
const ep = database.makeEndpoint(
database.writtenEventType,
{
region: 'us-central1',
labels: { 1: '2' },
},
new PathPattern('foo/bar'),
new PathPattern('my-instance')
);

expect(ep).to.deep.equal({
platform: 'gcfv2',
labels: {
1: '2',
},
region: ['us-central1'],
eventTrigger: {
eventType: database.writtenEventType,
eventFilters: {
instance: 'my-instance',
},
eventFilterPathPatterns: {
ref: 'foo/bar',
},
retry: false,
},
});
});
});

describe('onChangedOperation', () => {
it('should create a function for a written event', () => {
const func = database.onOperation(
const func = database.onChangedOperation(
database.writtenEventType,
'/foo/{bar}/',
(event) => 2
Expand All @@ -169,9 +230,9 @@ describe('database', () => {
});
});

it('should create a function for a created event', () => {
const func = database.onOperation(
database.createdEventType,
it('should create a function for a updated event', () => {
const func = database.onChangedOperation(
database.updatedEventType,
'/foo/{bar}/',
(event) => 2
);
Expand All @@ -180,7 +241,7 @@ describe('database', () => {
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: database.createdEventType,
eventType: database.updatedEventType,
eventFilters: {},
eventFilterPathPatterns: {
ref: 'foo/{bar}',
Expand All @@ -191,9 +252,43 @@ describe('database', () => {
});
});

it('should create a function for a updated event', () => {
it('should create a complex function', () => {
const func = database.onChangedOperation(
database.writtenEventType,
{
ref: '/foo/{path=**}/{bar}/',
instance: 'my-instance',
region: 'us-central1',
cpu: 'gcf_gen1',
minInstances: 2,
},
(event) => 2
);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
cpu: 'gcf_gen1',
minInstances: 2,
region: ['us-central1'],
labels: {},
eventTrigger: {
eventType: database.writtenEventType,
eventFilters: {
instance: 'my-instance',
},
eventFilterPathPatterns: {
ref: 'foo/{path=**}/{bar}',
},
retry: false,
},
});
});
});

describe('onOperation', () => {
it('should create a function for a created event', () => {
const func = database.onOperation(
database.updatedEventType,
database.createdEventType,
'/foo/{bar}/',
(event) => 2
);
Expand All @@ -202,7 +297,7 @@ describe('database', () => {
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: database.updatedEventType,
eventType: database.createdEventType,
eventFilters: {},
eventFilterPathPatterns: {
ref: 'foo/{bar}',
Expand Down Expand Up @@ -237,7 +332,7 @@ describe('database', () => {

it('should create a complex function', () => {
const func = database.onOperation(
database.writtenEventType,
database.createdEventType,
{
ref: '/foo/{path=**}/{bar}/',
instance: 'my-instance',
Expand All @@ -255,7 +350,7 @@ describe('database', () => {
region: ['us-central1'],
labels: {},
eventTrigger: {
eventType: database.writtenEventType,
eventType: database.createdEventType,
eventFilters: {
instance: 'my-instance',
},
Expand Down

1 comment on commit 1fc1d33

@rhodgkins
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had email about this - is this the changes from #926?
Will it apply to code still using v1 functions as well?

Please sign in to comment.