Skip to content

Setup firestore tests for named db #7505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/firestore/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ module.exports = function (config) {
};
}

if (argv.databaseId) {
karmaConfig.client = {
...karmaConfig.client,
databaseId: argv.databaseId
};
}

config.set(karmaConfig);
};

Expand Down
8 changes: 6 additions & 2 deletions packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@
"prettier": "prettier --write '*.js' '@(lite|src|test)/**/*.ts' 'test/unit/remote/bloom_filter_golden_test_data/*.json'",
"test:lite": "ts-node ./scripts/run-tests.ts --emulator --platform node_lite --main=lite/index.ts 'test/lite/**/*.test.ts'",
"test:lite:prod": "ts-node ./scripts/run-tests.ts --platform node_lite --main=lite/index.ts 'test/lite/**/*.test.ts'",
"test:lite:prod:nameddb": "ts-node ./scripts/run-tests.ts --platform node_lite --databaseId=test-db --main=lite/index.ts 'test/lite/**/*.test.ts'",
"test:lite:browser": "karma start --single-run --lite",
"test:lite:browser:nameddb": "karma start --single-run --lite --databaseId=test-db",
"test:lite:browser:debug": "karma start --browsers=Chrome --lite --auto-watch",
"test": "run-s lint test:all",
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all:ci",
"test:all:ci": "run-p test:browser test:lite:browser test:travis",
"test:all": "run-p test:browser test:lite:browser test:travis test:minified",
"test:all:ci": "run-s test:browser test:travis test:lite:browser test:browser:prod:nameddb test:lite:browser:nameddb",
"test:all": "run-p test:browser test:lite:browser test:travis test:minified test:browser:prod:nameddb test:lite:browser:nameddb",
"test:browser": "karma start --single-run",
"test:browser:emulator:debug": "karma start --browsers=Chrome --targetBackend=emulator",
"test:browser:emulator": "karma start --single-run --targetBackend=emulator",
"test:browser:nightly": "karma start --single-run --targetBackend=nightly",
"test:browser:prod": "karma start --single-run --targetBackend=prod",
"test:browser:prod:nameddb": "karma start --single-run --targetBackend=prod --databaseId=test-db",
"test:browser:unit": "karma start --single-run --unit",
"test:browser:debug": "karma start --browsers=Chrome --auto-watch",
"test:node": "ts-node ./scripts/run-tests.ts --main=test/register.ts --emulator 'test/{,!(browser|lite)/**/}*.test.ts'",
"test:node:prod": "ts-node ./scripts/run-tests.ts --main=test/register.ts 'test/{,!(browser|lite)/**/}*.test.ts'",
"test:node:prod:nameddb": "ts-node ./scripts/run-tests.ts --main=test/register.ts --databaseId=test-db 'test/{,!(browser|lite)/**/}*.test.ts'",
"test:node:persistence": "ts-node ./scripts/run-tests.ts --main=test/register.ts --persistence --emulator 'test/{,!(browser|lite)/**/}*.test.ts'",
"test:node:persistence:prod": "ts-node ./scripts/run-tests.ts --main=test/register.ts --persistence 'test/{,!(browser|lite)/**/}*.test.ts'",
"test:travis": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/firestore-test-runner.ts",
Expand Down
7 changes: 7 additions & 0 deletions packages/firestore/scripts/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const argv = yargs.options({
},
persistence: {
type: 'boolean'
},
databaseId: {
type: 'string'
}
}).parseSync();

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

if (argv.databaseId) {
process.env.FIRESTORE_TARGET_DB_ID = argv.databaseId;
}

args = args.concat(argv._ as string[]);

const childProcess = spawn(nyc, args, {
Expand Down
32 changes: 24 additions & 8 deletions packages/firestore/test/integration/api/aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,15 @@ apiDescribe('Count queries', persistence => {
where('key1', '==', 42),
where('key2', '<', 42)
);
await expect(getCountFromServer(query_)).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
if (coll.firestore._databaseId.isDefaultDatabase) {
await expect(
getCountFromServer(query_)
).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
} else {
await expect(getCountFromServer(query_)).to.be.eventually.rejected;
}
});
}
);
Expand Down Expand Up @@ -343,11 +349,21 @@ apiDescribe('Aggregation queries', persistence => {
where('key1', '==', 42),
where('key2', '<', 42)
);
await expect(
getAggregateFromServer(query_, { count: count() })
).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
if (coll.firestore._databaseId.isDefaultDatabase) {
await expect(
getAggregateFromServer(query_, {
count: count()
})
).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
} else {
await expect(
getAggregateFromServer(query_, {
count: count()
})
).to.be.eventually.rejected;
}
});
}
);
Expand Down
4 changes: 3 additions & 1 deletion packages/firestore/test/integration/api/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ apiDescribe('Bundles', persistence => {
const projectId: string = db.app.options.projectId!;

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

// Recalculating length prefixes for elements that are not BundleMetadata.
let bundleContent = '';
Expand Down
7 changes: 5 additions & 2 deletions packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,8 @@ apiDescribe('Database', persistence => {

const firestore2 = newTestFirestore(
newTestApp(options.projectId!, name),
DEFAULT_SETTINGS
DEFAULT_SETTINGS,
firestore._databaseId.database
);
await enableIndexedDbPersistence(firestore2);
await waitForPendingWrites(firestore2);
Expand Down Expand Up @@ -1157,7 +1158,9 @@ apiDescribe('Database', persistence => {
await deleteApp(app);

const firestore2 = newTestFirestore(
newTestApp(options.projectId!, name)
newTestApp(options.projectId!, name),
undefined,
docRef.firestore._databaseId.database
);
await enableIndexedDbPersistence(firestore2);
const docRef2 = doc(firestore2, docRef.path);
Expand Down
8 changes: 5 additions & 3 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,11 @@ apiDescribe('Queries', persistence => {
err => {
expect(err.code).to.equal('failed-precondition');
expect(err.message).to.exist;
expect(err.message).to.match(
/index.*https:\/\/console\.firebase\.google\.com/
);
if (coll.firestore._databaseId.isDefaultDatabase) {
expect(err.message).to.match(
/index.*https:\/\/console\.firebase\.google\.com/
);
}
deferred.resolve();
}
);
Expand Down
10 changes: 7 additions & 3 deletions packages/firestore/test/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ import {
withTestCollection,
withTestDb
} from '../util/helpers';
import { ALT_PROJECT_ID, DEFAULT_PROJECT_ID } from '../util/settings';
import {
ALT_PROJECT_ID,
DEFAULT_PROJECT_ID,
TARGET_DB_ID
} from '../util/settings';

// We're using 'as any' to pass invalid values to APIs for testing purposes.
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -445,8 +449,8 @@ apiDescribe('Validation:', persistence => {
db,
data,
`Document reference is for database ` +
`${ALT_PROJECT_ID}/(default) but should be for database ` +
`${DEFAULT_PROJECT_ID}/(default) (found in field ` +
`${ALT_PROJECT_ID}/${TARGET_DB_ID} but should be for database ` +
`${DEFAULT_PROJECT_ID}/${TARGET_DB_ID} (found in field ` +
`foo)`
);
});
Expand Down
7 changes: 6 additions & 1 deletion packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
ALT_PROJECT_ID,
DEFAULT_PROJECT_ID,
DEFAULT_SETTINGS,
TARGET_DB_ID,
USE_EMULATOR
} from './settings';

Expand Down Expand Up @@ -295,7 +296,11 @@ export async function withTestDbsSettings<T>(
if (persistence !== PERSISTENCE_MODE_UNSPECIFIED) {
newSettings.localCache = persistence.asLocalCacheFirestoreSettings();
}
const db = newTestFirestore(newTestApp(projectId), newSettings);
const db = newTestFirestore(
newTestApp(projectId),
newSettings,
TARGET_DB_ID
);
dbs.push(db);
}

Expand Down
15 changes: 15 additions & 0 deletions packages/firestore/test/integration/util/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ enum TargetBackend {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const PROJECT_CONFIG = require('../../../../../config/project.json');

export const TARGET_DB_ID: string | '(default)' = getTargetDbId();

const TARGET_BACKEND: TargetBackend = getTargetBackend();

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

// eslint-disable-next-line no-console
console.log(`Default Settings: ${JSON.stringify(DEFAULT_SETTINGS)}`);
// eslint-disable-next-line no-console
console.log(`Default DatabaseId: ${JSON.stringify(TARGET_DB_ID)}`);

function getTargetDbId(): string | '(default)' {
const karma = typeof __karma__ !== 'undefined' ? __karma__ : undefined;
if (karma && karma.config.databaseId) {
return karma.config.databaseId;
}
if (process.env.FIRESTORE_TARGET_DB_ID) {
return process.env.FIRESTORE_TARGET_DB_ID;
}
return '(default)';
}

function parseTargetBackend(targetBackend: string): TargetBackend {
switch (targetBackend) {
Expand Down
5 changes: 3 additions & 2 deletions packages/firestore/test/lite/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import { QueryDocumentSnapshot } from '../../src/lite-api/snapshot';
import { AutoId } from '../../src/util/misc';
import {
DEFAULT_PROJECT_ID,
DEFAULT_SETTINGS
DEFAULT_SETTINGS,
TARGET_DB_ID
} from '../integration/util/settings';

let appCount = 0;
Expand All @@ -50,7 +51,7 @@ export async function withTestDbSettings(
'test-app-' + appCount++
);

const firestore = initializeFirestore(app, settings);
const firestore = initializeFirestore(app, settings, TARGET_DB_ID);
return fn(firestore);
}

Expand Down
32 changes: 22 additions & 10 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2404,9 +2404,13 @@ describe('Count queries', () => {
where('key1', '==', 42),
where('key2', '<', 42)
);
await expect(getCount(query_)).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
if (coll.firestore._databaseId.isDefaultDatabase) {
await expect(getCount(query_)).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
} else {
await expect(getCount(query_)).to.be.eventually.rejected;
}
});
}
);
Expand Down Expand Up @@ -2707,13 +2711,21 @@ describe('Aggregate queries', () => {
where('key1', '==', 42),
where('key2', '<', 42)
);
await expect(
getAggregate(query_, {
myCount: count()
})
).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
if (coll.firestore._databaseId.isDefaultDatabase) {
await expect(
getAggregate(query_, {
myCount: count()
})
).to.be.eventually.rejectedWith(
/index.*https:\/\/console\.firebase\.google\.com/
);
} else {
await expect(
getAggregate(query_, {
myCount: count()
})
).to.be.eventually.rejected;
}
});
}
);
Expand Down