Skip to content

Commit 0038e11

Browse files
authored
Setup firestore tests for named db (#7505)
1 parent f9a232a commit 0038e11

File tree

12 files changed

+110
-32
lines changed

12 files changed

+110
-32
lines changed

packages/firestore/karma.conf.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ module.exports = function (config) {
4646
};
4747
}
4848

49+
if (argv.databaseId) {
50+
karmaConfig.client = {
51+
...karmaConfig.client,
52+
databaseId: argv.databaseId
53+
};
54+
}
55+
4956
config.set(karmaConfig);
5057
};
5158

packages/firestore/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,25 @@
2323
"prettier": "prettier --write '*.js' '@(lite|src|test)/**/*.ts' 'test/unit/remote/bloom_filter_golden_test_data/*.json'",
2424
"test:lite": "ts-node ./scripts/run-tests.ts --emulator --platform node_lite --main=lite/index.ts 'test/lite/**/*.test.ts'",
2525
"test:lite:prod": "ts-node ./scripts/run-tests.ts --platform node_lite --main=lite/index.ts 'test/lite/**/*.test.ts'",
26+
"test:lite:prod:nameddb": "ts-node ./scripts/run-tests.ts --platform node_lite --databaseId=test-db --main=lite/index.ts 'test/lite/**/*.test.ts'",
2627
"test:lite:browser": "karma start --single-run --lite",
28+
"test:lite:browser:nameddb": "karma start --single-run --lite --databaseId=test-db",
2729
"test:lite:browser:debug": "karma start --browsers=Chrome --lite --auto-watch",
2830
"test": "run-s lint test:all",
2931
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all:ci",
30-
"test:all:ci": "run-p test:browser test:lite:browser test:travis",
31-
"test:all": "run-p test:browser test:lite:browser test:travis test:minified",
32+
"test:all:ci": "run-s test:browser test:travis test:lite:browser test:browser:prod:nameddb test:lite:browser:nameddb",
33+
"test:all": "run-p test:browser test:lite:browser test:travis test:minified test:browser:prod:nameddb test:lite:browser:nameddb",
3234
"test:browser": "karma start --single-run",
3335
"test:browser:emulator:debug": "karma start --browsers=Chrome --targetBackend=emulator",
3436
"test:browser:emulator": "karma start --single-run --targetBackend=emulator",
3537
"test:browser:nightly": "karma start --single-run --targetBackend=nightly",
3638
"test:browser:prod": "karma start --single-run --targetBackend=prod",
39+
"test:browser:prod:nameddb": "karma start --single-run --targetBackend=prod --databaseId=test-db",
3740
"test:browser:unit": "karma start --single-run --unit",
3841
"test:browser:debug": "karma start --browsers=Chrome --auto-watch",
3942
"test:node": "ts-node ./scripts/run-tests.ts --main=test/register.ts --emulator 'test/{,!(browser|lite)/**/}*.test.ts'",
4043
"test:node:prod": "ts-node ./scripts/run-tests.ts --main=test/register.ts 'test/{,!(browser|lite)/**/}*.test.ts'",
44+
"test:node:prod:nameddb": "ts-node ./scripts/run-tests.ts --main=test/register.ts --databaseId=test-db 'test/{,!(browser|lite)/**/}*.test.ts'",
4145
"test:node:persistence": "ts-node ./scripts/run-tests.ts --main=test/register.ts --persistence --emulator 'test/{,!(browser|lite)/**/}*.test.ts'",
4246
"test:node:persistence:prod": "ts-node ./scripts/run-tests.ts --main=test/register.ts --persistence 'test/{,!(browser|lite)/**/}*.test.ts'",
4347
"test:travis": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/firestore-test-runner.ts",

packages/firestore/scripts/run-tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const argv = yargs.options({
3434
},
3535
persistence: {
3636
type: 'boolean'
37+
},
38+
databaseId: {
39+
type: 'string'
3740
}
3841
}).parseSync();
3942

@@ -66,6 +69,10 @@ if (argv.persistence) {
6669
args.push('--require', 'test/util/node_persistence.ts');
6770
}
6871

72+
if (argv.databaseId) {
73+
process.env.FIRESTORE_TARGET_DB_ID = argv.databaseId;
74+
}
75+
6976
args = args.concat(argv._ as string[]);
7077

7178
const childProcess = spawn(nyc, args, {

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,15 @@ apiDescribe('Count queries', persistence => {
139139
where('key1', '==', 42),
140140
where('key2', '<', 42)
141141
);
142-
await expect(getCountFromServer(query_)).to.be.eventually.rejectedWith(
143-
/index.*https:\/\/console\.firebase\.google\.com/
144-
);
142+
if (coll.firestore._databaseId.isDefaultDatabase) {
143+
await expect(
144+
getCountFromServer(query_)
145+
).to.be.eventually.rejectedWith(
146+
/index.*https:\/\/console\.firebase\.google\.com/
147+
);
148+
} else {
149+
await expect(getCountFromServer(query_)).to.be.eventually.rejected;
150+
}
145151
});
146152
}
147153
);
@@ -343,11 +349,21 @@ apiDescribe('Aggregation queries', persistence => {
343349
where('key1', '==', 42),
344350
where('key2', '<', 42)
345351
);
346-
await expect(
347-
getAggregateFromServer(query_, { count: count() })
348-
).to.be.eventually.rejectedWith(
349-
/index.*https:\/\/console\.firebase\.google\.com/
350-
);
352+
if (coll.firestore._databaseId.isDefaultDatabase) {
353+
await expect(
354+
getAggregateFromServer(query_, {
355+
count: count()
356+
})
357+
).to.be.eventually.rejectedWith(
358+
/index.*https:\/\/console\.firebase\.google\.com/
359+
);
360+
} else {
361+
await expect(
362+
getAggregateFromServer(query_, {
363+
count: count()
364+
})
365+
).to.be.eventually.rejected;
366+
}
351367
});
352368
}
353369
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ apiDescribe('Bundles', persistence => {
8585
const projectId: string = db.app.options.projectId!;
8686

8787
// Extract elements from BUNDLE_TEMPLATE and replace the project ID.
88-
const elements = BUNDLE_TEMPLATE.map(e => e.replace('{0}', projectId));
88+
const elements = BUNDLE_TEMPLATE.map(e =>
89+
e.replace('{0}', projectId).replace('(default)', db._databaseId.database)
90+
);
8991

9092
// Recalculating length prefixes for elements that are not BundleMetadata.
9193
let bundleContent = '';

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,8 @@ apiDescribe('Database', persistence => {
11141114

11151115
const firestore2 = newTestFirestore(
11161116
newTestApp(options.projectId!, name),
1117-
DEFAULT_SETTINGS
1117+
DEFAULT_SETTINGS,
1118+
firestore._databaseId.database
11181119
);
11191120
await enableIndexedDbPersistence(firestore2);
11201121
await waitForPendingWrites(firestore2);
@@ -1157,7 +1158,9 @@ apiDescribe('Database', persistence => {
11571158
await deleteApp(app);
11581159

11591160
const firestore2 = newTestFirestore(
1160-
newTestApp(options.projectId!, name)
1161+
newTestApp(options.projectId!, name),
1162+
undefined,
1163+
docRef.firestore._databaseId.database
11611164
);
11621165
await enableIndexedDbPersistence(firestore2);
11631166
const docRef2 = doc(firestore2, docRef.path);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,11 @@ apiDescribe('Queries', persistence => {
682682
err => {
683683
expect(err.code).to.equal('failed-precondition');
684684
expect(err.message).to.exist;
685-
expect(err.message).to.match(
686-
/index.*https:\/\/console\.firebase\.google\.com/
687-
);
685+
if (coll.firestore._databaseId.isDefaultDatabase) {
686+
expect(err.message).to.match(
687+
/index.*https:\/\/console\.firebase\.google\.com/
688+
);
689+
}
688690
deferred.resolve();
689691
}
690692
);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ import {
6161
withTestCollection,
6262
withTestDb
6363
} from '../util/helpers';
64-
import { ALT_PROJECT_ID, DEFAULT_PROJECT_ID } from '../util/settings';
64+
import {
65+
ALT_PROJECT_ID,
66+
DEFAULT_PROJECT_ID,
67+
TARGET_DB_ID
68+
} from '../util/settings';
6569

6670
// We're using 'as any' to pass invalid values to APIs for testing purposes.
6771
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -445,8 +449,8 @@ apiDescribe('Validation:', persistence => {
445449
db,
446450
data,
447451
`Document reference is for database ` +
448-
`${ALT_PROJECT_ID}/(default) but should be for database ` +
449-
`${DEFAULT_PROJECT_ID}/(default) (found in field ` +
452+
`${ALT_PROJECT_ID}/${TARGET_DB_ID} but should be for database ` +
453+
`${DEFAULT_PROJECT_ID}/${TARGET_DB_ID} (found in field ` +
450454
`foo)`
451455
);
452456
});

packages/firestore/test/integration/util/helpers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
ALT_PROJECT_ID,
4646
DEFAULT_PROJECT_ID,
4747
DEFAULT_SETTINGS,
48+
TARGET_DB_ID,
4849
USE_EMULATOR
4950
} from './settings';
5051

@@ -295,7 +296,11 @@ export async function withTestDbsSettings<T>(
295296
if (persistence !== PERSISTENCE_MODE_UNSPECIFIED) {
296297
newSettings.localCache = persistence.asLocalCacheFirestoreSettings();
297298
}
298-
const db = newTestFirestore(newTestApp(projectId), newSettings);
299+
const db = newTestFirestore(
300+
newTestApp(projectId),
301+
newSettings,
302+
TARGET_DB_ID
303+
);
299304
dbs.push(db);
300305
}
301306

packages/firestore/test/integration/util/settings.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ enum TargetBackend {
3535
// eslint-disable-next-line @typescript-eslint/no-require-imports
3636
const PROJECT_CONFIG = require('../../../../../config/project.json');
3737

38+
export const TARGET_DB_ID: string | '(default)' = getTargetDbId();
39+
3840
const TARGET_BACKEND: TargetBackend = getTargetBackend();
3941

4042
export const USE_EMULATOR: boolean = TARGET_BACKEND === TargetBackend.EMULATOR;
@@ -46,6 +48,19 @@ export const DEFAULT_SETTINGS: PrivateSettings = {
4648

4749
// eslint-disable-next-line no-console
4850
console.log(`Default Settings: ${JSON.stringify(DEFAULT_SETTINGS)}`);
51+
// eslint-disable-next-line no-console
52+
console.log(`Default DatabaseId: ${JSON.stringify(TARGET_DB_ID)}`);
53+
54+
function getTargetDbId(): string | '(default)' {
55+
const karma = typeof __karma__ !== 'undefined' ? __karma__ : undefined;
56+
if (karma && karma.config.databaseId) {
57+
return karma.config.databaseId;
58+
}
59+
if (process.env.FIRESTORE_TARGET_DB_ID) {
60+
return process.env.FIRESTORE_TARGET_DB_ID;
61+
}
62+
return '(default)';
63+
}
4964

5065
function parseTargetBackend(targetBackend: string): TargetBackend {
5166
switch (targetBackend) {

packages/firestore/test/lite/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import { QueryDocumentSnapshot } from '../../src/lite-api/snapshot';
3535
import { AutoId } from '../../src/util/misc';
3636
import {
3737
DEFAULT_PROJECT_ID,
38-
DEFAULT_SETTINGS
38+
DEFAULT_SETTINGS,
39+
TARGET_DB_ID
3940
} from '../integration/util/settings';
4041

4142
let appCount = 0;
@@ -50,7 +51,7 @@ export async function withTestDbSettings(
5051
'test-app-' + appCount++
5152
);
5253

53-
const firestore = initializeFirestore(app, settings);
54+
const firestore = initializeFirestore(app, settings, TARGET_DB_ID);
5455
return fn(firestore);
5556
}
5657

packages/firestore/test/lite/integration.test.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,9 +2404,13 @@ describe('Count queries', () => {
24042404
where('key1', '==', 42),
24052405
where('key2', '<', 42)
24062406
);
2407-
await expect(getCount(query_)).to.be.eventually.rejectedWith(
2408-
/index.*https:\/\/console\.firebase\.google\.com/
2409-
);
2407+
if (coll.firestore._databaseId.isDefaultDatabase) {
2408+
await expect(getCount(query_)).to.be.eventually.rejectedWith(
2409+
/index.*https:\/\/console\.firebase\.google\.com/
2410+
);
2411+
} else {
2412+
await expect(getCount(query_)).to.be.eventually.rejected;
2413+
}
24102414
});
24112415
}
24122416
);
@@ -2707,13 +2711,21 @@ describe('Aggregate queries', () => {
27072711
where('key1', '==', 42),
27082712
where('key2', '<', 42)
27092713
);
2710-
await expect(
2711-
getAggregate(query_, {
2712-
myCount: count()
2713-
})
2714-
).to.be.eventually.rejectedWith(
2715-
/index.*https:\/\/console\.firebase\.google\.com/
2716-
);
2714+
if (coll.firestore._databaseId.isDefaultDatabase) {
2715+
await expect(
2716+
getAggregate(query_, {
2717+
myCount: count()
2718+
})
2719+
).to.be.eventually.rejectedWith(
2720+
/index.*https:\/\/console\.firebase\.google\.com/
2721+
);
2722+
} else {
2723+
await expect(
2724+
getAggregate(query_, {
2725+
myCount: count()
2726+
})
2727+
).to.be.eventually.rejected;
2728+
}
27172729
});
27182730
}
27192731
);

0 commit comments

Comments
 (0)