Skip to content

Commit

Permalink
move the last test
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Sep 1, 2023
1 parent 419cd5a commit 5999ae6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 167 deletions.
177 changes: 10 additions & 167 deletions packages/firestore/test/unit/local/local_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ import {
import { SnapshotVersion } from '../../../src/core/snapshot_version';
import { Target } from '../../../src/core/target';
import { BatchId, TargetId } from '../../../src/core/types';
import { IndexBackfiller } from '../../../src/local/index_backfiller';
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
import { LocalStore } from '../../../src/local/local_store';
import {
localStoreAcknowledgeBatch,
localStoreAllocateTarget,
localStoreApplyBundledDocuments,
localStoreApplyRemoteEventToLocalCache,
localStoreConfigureFieldIndexes,
localStoreDeleteAllFieldIndexes,
localStoreExecuteQuery,
localStoreGetHighestUnacknowledgedBatchId,
localStoreGetTargetData,
Expand All @@ -55,8 +52,7 @@ import {
localStoreReleaseTarget,
localStoreSaveBundle,
localStoreSaveNamedQuery,
newLocalStore,
TestingHooks as LocalStoreTestingHooks
newLocalStore
} from '../../../src/local/local_store_impl';
import { LocalViewChanges } from '../../../src/local/local_view_changes';
import { Persistence } from '../../../src/local/persistence';
Expand All @@ -66,12 +62,6 @@ import {
DocumentMap
} from '../../../src/model/collections';
import { Document } from '../../../src/model/document';
import {
FieldIndex,
IndexKind,
IndexSegment,
IndexState
} from '../../../src/model/field_index';
import { FieldMask } from '../../../src/model/field_mask';
import {
FieldTransform,
Expand All @@ -86,7 +76,6 @@ import {
MutationBatchResult
} from '../../../src/model/mutation_batch';
import { ObjectValue } from '../../../src/model/object_value';
import { FieldPath } from '../../../src/model/path';
import { serverTimestamp } from '../../../src/model/server_timestamps';
import { ServerTimestampTransform } from '../../../src/model/transform_operation';
import { BundleMetadata as ProtoBundleMetadata } from '../../../src/protos/firestore_bundle_proto';
Expand Down Expand Up @@ -146,7 +135,6 @@ class LocalStoreTester {
private lastTargetId: TargetId | null = null;
private batches: MutationBatch[] = [];
private bundleConverter: BundleConverterImpl;
private indexBackfiller: IndexBackfiller;

private queryExecutionCount = 0;

Expand All @@ -157,7 +145,6 @@ class LocalStoreTester {
readonly gcIsEager: boolean
) {
this.bundleConverter = new BundleConverterImpl(JSON_SERIALIZER);
this.indexBackfiller = new IndexBackfiller(localStore, persistence);
}

private prepareNextStep(): void {
Expand Down Expand Up @@ -192,10 +179,6 @@ class LocalStoreTester {
}
}

afterMutation(mutation: Mutation): LocalStoreTester {
return this.afterMutations([mutation]);
}

afterMutations(mutations: Mutation[]): LocalStoreTester {
this.prepareNextStep();

Expand Down Expand Up @@ -223,11 +206,6 @@ class LocalStoreTester {
return this;
}

afterRemoteEvents(remoteEvents: RemoteEvent[]): LocalStoreTester {
remoteEvents.forEach(remoteEvent => this.afterRemoteEvent(remoteEvent));
return this;
}

afterBundleDocuments(
documents: BundledDocuments,
bundleName?: string
Expand Down Expand Up @@ -353,59 +331,6 @@ class LocalStoreTester {
return this;
}

afterIndexAutoCreationConfigure(config: {
isEnabled?: boolean;
indexAutoCreationMinCollectionSize?: number;
relativeIndexReadCostPerDocument?: number;
}): LocalStoreTester {
this.prepareNextStep();

this.promiseChain = this.promiseChain.then(() => {
if (config.isEnabled !== undefined) {
localStoreSetIndexAutoCreationEnabled(
this.localStore,
config.isEnabled
);
}
LocalStoreTestingHooks.setIndexAutoCreationSettings(
this.localStore,
config
);
});

return this;
}

afterDeleteAllFieldIndexes(): LocalStoreTester {
this.prepareNextStep();
this.promiseChain = this.promiseChain.then(() =>
localStoreDeleteAllFieldIndexes(this.localStore)
);
return this;
}

afterConfigureFieldIndexes(fieldIndexes: FieldIndex[]): LocalStoreTester {
this.prepareNextStep();
this.promiseChain = this.promiseChain.then(() =>
localStoreConfigureFieldIndexes(this.localStore, fieldIndexes)
);
return this;
}

afterBackfillIndexes(options?: {
maxDocumentsToProcess?: number;
}): LocalStoreTester {
this.prepareNextStep();

this.promiseChain = this.promiseChain.then(() =>
this.indexBackfiller
.backfill(options?.maxDocumentsToProcess)
.then(() => {})
);

return this;
}

/**
* Asserts the expected number of mutations and documents read by
* the MutationQueue and the RemoteDocumentCache.
Expand Down Expand Up @@ -485,40 +410,31 @@ class LocalStoreTester {
}

toReturnChangedInternal(
docsOrKeyStrs: Document[] | string[],
docs: Document[],
isEqual?: (lhs: Document | null, rhs: Document | null) => boolean
): LocalStoreTester {
this.promiseChain = this.promiseChain.then(() => {
debugAssert(
this.lastChanges !== null,
'Called toReturnChanged() without prior after()'
);
expect(this.lastChanges.size).to.equal(
docsOrKeyStrs.length,
'number of changes'
);
for (const docOrKeyStr of docsOrKeyStrs) {
const docKey =
typeof docOrKeyStr === 'string' ? key(docOrKeyStr) : docOrKeyStr.key;
const returned = this.lastChanges.get(docKey);
expect(this.lastChanges.size).to.equal(docs.length, 'number of changes');
for (const doc of docs) {
const returned = this.lastChanges.get(doc.key);
const message = `Expected '${returned}' to equal '${doc}'.`;
if (typeof docOrKeyStr === 'string') {
expect(returned?.isValidDocument()).to.equal(true, message);
} else if (isEqual) {
expect(isEqual(docOrKeyStr, returned)).to.equal(true, message);
if (isEqual) {
expect(isEqual(doc, returned)).to.equal(true, message);
} else {
expectEqual(docOrKeyStr, returned, message);
expectEqual(doc, returned, message);
}
}
this.lastChanges = null;
});
return this;
}

toReturnChanged(...docs: Document[]): LocalStoreTester;
toReturnChanged(...docKeyStrs: string[]): LocalStoreTester;
toReturnChanged(...docsOrKeyStrs: Document[] | string[]): LocalStoreTester {
return this.toReturnChangedInternal(docsOrKeyStrs);
toReturnChanged(...docs: Document[]): LocalStoreTester {
return this.toReturnChangedInternal(docs);
}

toReturnChangedWithDocComparator(
Expand Down Expand Up @@ -673,18 +589,6 @@ function compareDocsWithCreateTime(
);
}

function fieldIndex(
collectionGroup: string,
indexId: number,
indexState: IndexState,
field: string,
kind: IndexKind
): FieldIndex {
const fieldPath = new FieldPath(field.split('.'));
const segments = [new IndexSegment(fieldPath, kind)];
return new FieldIndex(indexId, collectionGroup, segments, indexState);
}

describe('LocalStore w/ Memory Persistence', () => {
async function initialize(): Promise<LocalStoreComponents> {
const queryEngine = new CountingQueryEngine();
Expand Down Expand Up @@ -725,8 +629,6 @@ describe('LocalStore w/ IndexedDB Persistence', () => {
addEqualityMatcher();
describe('genericLocalStoreTests', () =>
genericLocalStoreTests(initialize, /* gcIsEager= */ false));
describe('indexedDbLocalStoreTests', () =>
indexedDbLocalStoreTests(initialize, /* gcIsEager= */ false));
});

function genericLocalStoreTests(
Expand Down Expand Up @@ -2727,62 +2629,3 @@ function genericLocalStoreTests(
}
);
}

function indexedDbLocalStoreTests(
getComponents: () => Promise<LocalStoreComponents>,
gcIsEager: boolean
): void {
let persistence: Persistence;
let localStore: LocalStore;
let queryEngine: CountingQueryEngine;

beforeEach(async () => {
const components = await getComponents();
persistence = components.persistence;
localStore = components.localStore;
queryEngine = components.queryEngine;
});

afterEach(async () => {
await persistence.shutdown();
await persistenceHelpers.clearTestPersistence();
});

function expectLocalStore(): LocalStoreTester {
return new LocalStoreTester(
localStore,
persistence,
queryEngine,
gcIsEager
);
}

// TODO(dconeybe) port this test next
it('delete all indexes works with manual added indexes', () => {
const query_ = query('coll', filter('matches', '==', true));
return expectLocalStore()
.afterConfigureFieldIndexes([
fieldIndex(
'coll',
0,
IndexState.empty(),
'matches',
IndexKind.ASCENDING
)
])
.afterAllocatingQuery(query_)
.toReturnTargetId(2)
.afterRemoteEvents([
docAddedRemoteEvent(doc('coll/a', 10, { matches: true }), [2], [])
])
.afterBackfillIndexes()
.afterExecutingQuery(query_)
.toHaveRead({ documentsByKey: 1, documentsByCollection: 0 })
.toReturnChanged('coll/a')
.afterDeleteAllFieldIndexes()
.afterExecutingQuery(query_)
.toHaveRead({ documentsByKey: 0, documentsByCollection: 1 })
.toReturnChanged('coll/a')
.finish();
});
}
22 changes: 22 additions & 0 deletions packages/firestore/test/unit/local/local_store_indexeddb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,26 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
test.assertQueryReturned('coll/a', 'coll/e');
});

it('delete all indexes works with manual added indexes', async () => {
const query_ = query('coll', filter('matches', '==', true));

await test.configureFieldsIndexes(fieldIndex('coll', {
fields: [['matches', IndexKind.ASCENDING]]
}));

const targetId = await test.allocateQuery(query_);
await test.applyRemoteEvent(docAddedRemoteEvent(doc('coll/a', 10, { matches: true }), [targetId]));
await test.backfillIndexes();

await test.executeQuery(query_);
test.assertRemoteDocumentsRead(1, 0);
test.assertQueryReturned('coll/a');

await test.deleteAllFieldIndexes();

await test.executeQuery(query_);
test.assertRemoteDocumentsRead(0, 1);
test.assertQueryReturned('coll/a');
});

});

0 comments on commit 5999ae6

Please sign in to comment.