Skip to content

Commit a8181f4

Browse files
authored
Avoid using hardcoded document paths which could cause conflicts when tests are running concurrently (e.g. on Travis). (#250)
1 parent eba194f commit a8181f4

File tree

6 files changed

+67
-97
lines changed

6 files changed

+67
-97
lines changed

packages/firestore/test/integration/api/database.test.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ import * as firestore from 'firestore';
2020
import { Deferred } from '../../../src/util/promise';
2121
import { asyncIt } from '../../util/helpers';
2222
import firebase from '../util/firebase_export';
23-
import { apiDescribe, withTestCollection, withTestDb } from '../util/helpers';
23+
import {
24+
apiDescribe,
25+
withTestCollection,
26+
withTestDb,
27+
withTestDoc
28+
} from '../util/helpers';
2429
import { Firestore } from '../../../src/api/database';
2530

2631
apiDescribe('Database', persistence => {
2732
asyncIt('can set a document', () => {
28-
return withTestDb(persistence, db => {
29-
return db.doc('rooms/Eros').set({
30-
desc: 'Stuff related to Eros project...',
33+
return withTestDoc(persistence, docRef => {
34+
return docRef.set({
35+
desc: 'Stuff related to Firestore project...',
3136
owner: {
3237
name: 'Jonny',
3338
title: 'scallywag'
@@ -46,8 +51,7 @@ apiDescribe('Database', persistence => {
4651
});
4752

4853
asyncIt('can delete a document', () => {
49-
return withTestDb(persistence, db => {
50-
const docRef = db.doc('rooms/Eros');
54+
return withTestDoc(persistence, docRef => {
5155
return docRef
5256
.set({ foo: 'bar' })
5357
.then(() => {
@@ -67,8 +71,7 @@ apiDescribe('Database', persistence => {
6771
});
6872

6973
asyncIt('can update existing document', () => {
70-
return withTestDb(persistence, db => {
71-
const doc = db.doc('rooms/Eros');
74+
return withTestDoc(persistence, doc => {
7275
const initialData = {
7376
desc: 'Description',
7477
owner: { name: 'Jonny', email: '[email protected]' }
@@ -93,8 +96,7 @@ apiDescribe('Database', persistence => {
9396
});
9497

9598
asyncIt('can merge data with an existing document using set', () => {
96-
return withTestDb(persistence, db => {
97-
const doc = db.doc('rooms/Eros');
99+
return withTestDoc(persistence, doc => {
98100
const initialData = {
99101
desc: 'description',
100102
'owner.data': { name: 'Jonny', email: '[email protected]' }
@@ -120,8 +122,7 @@ apiDescribe('Database', persistence => {
120122
});
121123

122124
asyncIt('can merge server timestamps', () => {
123-
return withTestDb(persistence, db => {
124-
const doc = db.doc('rooms/Eros');
125+
return withTestDoc(persistence, doc => {
125126
const initialData = {
126127
updated: false
127128
};
@@ -141,8 +142,7 @@ apiDescribe('Database', persistence => {
141142
});
142143

143144
asyncIt('can replace an array by merging using set', () => {
144-
return withTestDb(persistence, db => {
145-
const doc = db.doc('rooms/Eros');
145+
return withTestDoc(persistence, doc => {
146146
const initialData = {
147147
untouched: true,
148148
data: 'old',
@@ -172,8 +172,7 @@ apiDescribe('Database', persistence => {
172172
});
173173

174174
asyncIt('cannot update nonexistent document', () => {
175-
return withTestDb(persistence, db => {
176-
const doc = db.collection('rooms').doc();
175+
return withTestDoc(persistence, doc => {
177176
return doc
178177
.update({ owner: 'abc' })
179178
.then(
@@ -192,8 +191,7 @@ apiDescribe('Database', persistence => {
192191
});
193192

194193
asyncIt('can delete a field with an update', () => {
195-
return withTestDb(persistence, db => {
196-
const doc = db.doc('rooms/Eros');
194+
return withTestDoc(persistence, doc => {
197195
const initialData = {
198196
desc: 'Description',
199197
owner: { name: 'Jonny', email: '[email protected]' }
@@ -219,8 +217,7 @@ apiDescribe('Database', persistence => {
219217
asyncIt('can update nested fields', () => {
220218
const FieldPath = firebase.firestore.FieldPath;
221219

222-
return withTestDb(persistence, db => {
223-
const doc = db.doc('rooms/Eros');
220+
return withTestDoc(persistence, doc => {
224221
const initialData = {
225222
desc: 'Description',
226223
owner: { name: 'Jonny' },
@@ -248,8 +245,7 @@ apiDescribe('Database', persistence => {
248245
const invalidDocValues = [undefined, null, 0, 'foo', ['a'], new Date()];
249246
for (const val of invalidDocValues) {
250247
asyncIt('set/update should reject: ' + val, () => {
251-
return withTestDb(persistence, db => {
252-
const doc = db.collection('rooms').doc();
248+
return withTestDoc(persistence, doc => {
253249
// tslint:disable-next-line:no-any Intentionally passing bad types.
254250
expect(() => doc.set(val as any)).to.throw();
255251
// tslint:disable-next-line:no-any Intentionally passing bad types.
@@ -337,10 +333,9 @@ apiDescribe('Database', persistence => {
337333
});
338334

339335
asyncIt('Local document events are fired with hasLocalChanges=true.', () => {
340-
return withTestDb(persistence, db => {
336+
return withTestDoc(persistence, docRef => {
341337
let gotLocalDocEvent = false;
342338
const remoteDocEventDeferred = new Deferred();
343-
const docRef = db.collection('rooms').doc();
344339
const unlisten = docRef.onSnapshot(
345340
{ includeMetadataChanges: true },
346341
doc => {
@@ -366,10 +361,9 @@ apiDescribe('Database', persistence => {
366361
asyncIt(
367362
'Metadata only changes are not fired when no options provided',
368363
() => {
369-
return withTestDb(persistence, db => {
364+
return withTestDoc(persistence, docRef => {
370365
const secondUpdateFound = new Deferred();
371366
let count = 0;
372-
const docRef = db.collection('rooms').doc();
373367
const unlisten = docRef.onSnapshot(doc => {
374368
if (doc) {
375369
count++;
@@ -563,8 +557,7 @@ apiDescribe('Database', persistence => {
563557
});
564558

565559
asyncIt('can queue writes while offline', () => {
566-
return withTestDb(persistence, db => {
567-
const docRef = db.collection('rooms').doc();
560+
return withTestDoc(persistence, docRef => {
568561
// TODO(mikelehen): Find better way to expose this to tests.
569562
// tslint:disable-next-line:no-any enableNetwork isn't exposed via d.ts
570563
const firestoreInternal = docRef.firestore.INTERNAL as any;
@@ -585,8 +578,7 @@ apiDescribe('Database', persistence => {
585578
});
586579

587580
asyncIt('can get documents while offline', () => {
588-
return withTestDb(persistence, db => {
589-
const docRef = db.collection('rooms').doc();
581+
return withTestDoc(persistence, docRef => {
590582
// TODO(mikelehen): Find better way to expose this to tests.
591583
// tslint:disable-next-line:no-any enableNetwork isn't exposed via d.ts
592584
const firestoreInternal = docRef.firestore.INTERNAL as any;

packages/firestore/test/integration/api/query.test.ts

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -106,52 +106,31 @@ apiDescribe('Queries', persistence => {
106106
});
107107

108108
asyncIt('can use unary filters', () => {
109-
return withTestDbs(persistence, 2, ([writerDb, readerDb]) => {
110-
return Promise.all([
111-
writerDb
112-
.collection('query_test')
113-
.doc('a')
114-
.set({ null: null, nan: NaN }),
115-
writerDb
116-
.collection('query_test')
117-
.doc('b')
118-
.set({ null: null, nan: 0 }),
119-
writerDb
120-
.collection('query_test')
121-
.doc('c')
122-
.set({ null: false, nan: NaN })
123-
])
124-
.then(() => {
125-
return readerDb
126-
.collection('query_test')
127-
.where('null', '==', null)
128-
.where('nan', '==', NaN)
129-
.get();
130-
})
109+
const testDocs = {
110+
a: { null: null, nan: NaN },
111+
b: { null: null, nan: 0 },
112+
c: { null: false, nan: NaN }
113+
};
114+
return withTestCollection(persistence, testDocs, coll => {
115+
return coll
116+
.where('null', '==', null)
117+
.where('nan', '==', NaN)
118+
.get()
131119
.then(docs => {
132120
expect(toDataArray(docs)).to.deep.equal([{ null: null, nan: NaN }]);
133121
});
134122
});
135123
});
136124

137125
asyncIt('can filter on infinity', () => {
138-
return withTestDbs(persistence, 2, ([writerDb, readerDb]) => {
139-
return Promise.all([
140-
writerDb
141-
.collection('query_test')
142-
.doc('a')
143-
.set({ inf: Infinity }),
144-
writerDb
145-
.collection('query_test')
146-
.doc('b')
147-
.set({ inf: -Infinity })
148-
])
149-
.then(() => {
150-
return readerDb
151-
.collection('query_test')
152-
.where('inf', '==', Infinity)
153-
.get();
154-
})
126+
const testDocs = {
127+
a: { inf: Infinity },
128+
b: { inf: -Infinity }
129+
};
130+
return withTestCollection(persistence, testDocs, coll => {
131+
return coll
132+
.where('inf', '==', Infinity)
133+
.get()
155134
.then(docs => {
156135
expect(toDataArray(docs)).to.deep.equal([{ inf: Infinity }]);
157136
});

packages/firestore/test/integration/api/smoke.test.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ apiDescribe('Smoke Test', persistence => {
5151
});
5252

5353
asyncIt('can read a written document with DocumentKey', () => {
54-
return integrationHelpers.withTestDb(persistence, db => {
55-
const ref1 = db.doc('rooms/eros/messages/2');
56-
const ref2 = db.doc('users/patryk');
54+
return integrationHelpers.withTestDoc(persistence, ref1 => {
55+
const ref2 = ref1.firestore.collection('users').doc();
5756
const data = { user: ref2, message: 'We are writing data' };
5857
return ref2.set({ name: 'patryk' }).then(() => {
5958
return ref1
@@ -79,8 +78,8 @@ apiDescribe('Smoke Test', persistence => {
7978
persistence,
8079
2,
8180
([reader, writer]) => {
82-
const readerRef = reader.doc('rooms/eros/messages/1');
83-
const writerRef = writer.doc('rooms/eros/messages/1');
81+
const readerRef = reader.collection('rooms/firestore/messages').doc();
82+
const writerRef = writer.doc(readerRef.path);
8483
const data = {
8584
name: 'Patryk',
8685
message: 'We are actually writing data!'
@@ -95,27 +94,29 @@ apiDescribe('Smoke Test', persistence => {
9594
expect(docSnap.exists).to.equal(true);
9695
expect(docSnap.data()).to.deep.equal(data);
9796
})
98-
.then(() => unlisten(), () => unlisten());
97+
.then(() => unlisten());
9998
});
10099
}
101100
);
102101
});
103102

104103
asyncIt('will fire value events for empty collections', () => {
105-
return integrationHelpers.withTestDb(persistence, db => {
106-
const collection = db.collection('empty-collection');
107-
108-
const accum = new EventsAccumulator<firestore.QuerySnapshot>();
109-
const unlisten = collection.onSnapshot(accum.storeEvent);
110-
return accum
111-
.awaitEvent()
112-
.then(querySnap => {
113-
expect(querySnap.empty).to.equal(true);
114-
expect(querySnap.size).to.equal(0);
115-
expect(querySnap.docs.length).to.equal(0);
116-
})
117-
.then(() => unlisten(), () => unlisten());
118-
});
104+
return integrationHelpers.withTestCollection(
105+
persistence,
106+
{},
107+
collection => {
108+
const accum = new EventsAccumulator<firestore.QuerySnapshot>();
109+
const unlisten = collection.onSnapshot(accum.storeEvent);
110+
return accum
111+
.awaitEvent()
112+
.then(querySnap => {
113+
expect(querySnap.empty).to.equal(true);
114+
expect(querySnap.size).to.equal(0);
115+
expect(querySnap.docs.length).to.equal(0);
116+
})
117+
.then(() => unlisten());
118+
}
119+
);
119120
});
120121

121122
asyncIt('can get collection query', () => {

packages/firestore/test/integration/api/transactions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ apiDescribe('Database transactions', persistence => {
511511

512512
asyncIt('cannot have a get without mutations', () => {
513513
return integrationHelpers.withTestDb(persistence, db => {
514-
const docRef = db.doc('foo/bar');
514+
const docRef = db.collection('foo').doc();
515515
return (
516516
docRef
517517
.set({ foo: 'bar' })

packages/firestore/test/integration/api/type.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const asyncIt = testHelpers.asyncIt;
2525

2626
apiDescribe('Firestore', persistence => {
2727
function expectRoundtrip(db: firestore.Firestore, data: {}): Promise<void> {
28-
const doc = db.doc('rooms/Eros');
28+
const doc = db.collection('rooms').doc();
2929
return doc
3030
.set(data)
3131
.then(() => doc.get())
@@ -47,8 +47,7 @@ apiDescribe('Firestore', persistence => {
4747
});
4848

4949
asyncIt('can read and write geo point fields', () => {
50-
return withTestDb(persistence, db => {
51-
const doc = db.doc('rooms/Eros');
50+
return withTestDoc(persistence, doc => {
5251
return doc
5352
.set({ geopoint: new firebase.firestore.GeoPoint(1.23, 4.56) })
5453
.then(() => {
@@ -64,8 +63,7 @@ apiDescribe('Firestore', persistence => {
6463
});
6564

6665
asyncIt('can read and write bytes fields', () => {
67-
return withTestDb(persistence, db => {
68-
const doc = db.doc('rooms/Eros');
66+
return withTestDoc(persistence, doc => {
6967
return doc
7068
.set({
7169
bytes: firebase.firestore.Blob.fromUint8Array(

packages/firestore/test/integration/api/validation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ apiDescribe('Validation:', persistence => {
319319
validationIt(persistence, 'may contain indirectly nested arrays.', db => {
320320
const data = { 'nested-array': [1, { foo: [2] }] };
321321

322-
const ref = db.doc('foo/bar');
323-
const ref2 = db.doc('foo/quux');
322+
const ref = db.collection('foo').doc();
323+
const ref2 = db.collection('foo').doc();
324324

325325
ref
326326
.set(data)

0 commit comments

Comments
 (0)