Skip to content

Commit

Permalink
test: fixing test consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
mcs-alejandro authored and ejscribner committed Jan 23, 2024
1 parent 9c5a53b commit ddb0d12
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
3 changes: 1 addition & 2 deletions __test__/handler-update-many.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DocumentNotFoundError, getDefaultInstance, getModelMetadata, IManyQueryResponse, model, Schema } from '../src';
import { updateCallback } from '../src/handler';
import { StatusExecution, updateCallback } from '../src/handler';
import { consistency, startInTest } from './testData';
import { StatusExecution } from '../lib/types/handler';

describe('Test Document Update Many', () => {
test('Test Update Many Function', async () => {
Expand Down
47 changes: 36 additions & 11 deletions __test__/model-find-one-and-update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ describe('Test findOneAndUpdate function', () => {
});
const Cat = model('Cat', CatSchema);
await startInTest(getDefaultInstance());
await Cat.create({ name: 'Figaro', age: 27 });
const response = await Cat.findOneAndUpdate({ name: { $like: '%Figaro%' } }, { name: 'Kitty' });
const cleanUp = async () => await Cat.removeById(response.id);
await Cat.create({ name: 'Figaro123', age: 27 });
const response = await Cat.findOneAndUpdate(
{ name: { $like: '%Figaro123%' } },
{ name: 'Kitty' },
{ consistency: SearchConsistency.LOCAL },
);
const cleanUp = async () => {
if (response.id) {
await Cat.removeById(response.id);
}
};
await cleanUp();
expect(response.name).toBe('Figaro');
expect(response.name).toBe('Figaro123');
});
test('Test find item and update check stored document', async () => {
const CatSchema = new Schema({
Expand All @@ -22,12 +30,16 @@ describe('Test findOneAndUpdate function', () => {
});
const Cat = model('Cat', CatSchema);
await startInTest(getDefaultInstance());
await Cat.create({ name: 'Figaro', age: 27 });
const response = await Cat.findOneAndUpdate({ name: { $like: '%Figaro%' } }, { name: 'Kitty' });
await Cat.create({ name: 'Figaro456', age: 27 });
const response = await Cat.findOneAndUpdate(
{ name: { $like: '%Figaro456%' } },
{ name: 'Kitty' },
{ consistency: SearchConsistency.LOCAL },
);
const doc = await Cat.findById(response.id);
const cleanUp = async () => await Cat.removeById(response.id);
await cleanUp();
expect(response.name).toBe('Figaro');
expect(response.name).toBe('Figaro456');
expect(doc.name).toBe('Kitty');
expect(response.id).toBe(doc.id);
});
Expand All @@ -38,8 +50,12 @@ describe('Test findOneAndUpdate function', () => {
});
const Cat = model('Cat', CatSchema);
await startInTest(getDefaultInstance());
await Cat.create({ name: 'Figaro', age: 27 });
const response = await Cat.findOneAndUpdate({ name: { $like: '%Figaro%' } }, { name: 'Kitty' }, { new: true });
await Cat.create({ name: 'Figaro789', age: 27 });
const response = await Cat.findOneAndUpdate(
{ name: { $like: '%Figaro789%' } },
{ name: 'Kitty' },
{ new: true, consistency: SearchConsistency.LOCAL },
);
const cleanUp = async () => await Cat.removeMany({ _type: 'Cat' });
await cleanUp();
expect(response.name).toBe('Kitty');
Expand All @@ -52,7 +68,11 @@ describe('Test findOneAndUpdate function', () => {
const Cat = model('Cat', CatSchema);
await startInTest(getDefaultInstance());
await Cat.create({ name: 'Cat0', age: 27 });
const response = await Cat.findOneAndUpdate({ name: 'Kitty' }, { name: 'Kitty', age: 20 }, { upsert: true });
const response = await Cat.findOneAndUpdate(
{ name: 'Kitty' },
{ name: 'Kitty', age: 20 },
{ upsert: true, consistency: SearchConsistency.LOCAL },
);
const cleanUp = async () => await Cat.removeMany({ _type: 'Cat' });
await cleanUp();
expect(response.name).toBe('Kitty');
Expand All @@ -68,7 +88,12 @@ describe('Test findOneAndUpdate function', () => {
}
const Cat = model<ICat>('Cat', CatSchema);
await startInTest(getDefaultInstance());
const run = async () => await Cat.findOneAndUpdate({ name: { $like: 'DummyCatName91' } }, { name: 'Kitty' });
const run = async () =>
await Cat.findOneAndUpdate(
{ name: { $like: 'DummyCatName91' } },
{ name: 'Kitty' },
{ consistency: SearchConsistency.LOCAL },
);
await expect(run).rejects.toThrow(DocumentNotFoundError);
});

Expand Down
5 changes: 2 additions & 3 deletions src/model/create-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getValueByPath, Model, Query, SearchConsistency, setValueByPath } from
import { BuildIndexQueryError, OttomanError } from '../exceptions/ottoman-errors';
import { createMany, find, FindOptions, ManyQueryResponse, removeMany, updateMany } from '../handler';
import { FindByIdOptions, IFindOptions } from '../handler/';
import { LogicalWhereExpr } from '../query';
import { IConditionExpr, LogicalWhereExpr } from '../query';
import { Schema } from '../schema';
import { cast, CAST_STRATEGY, CastOptions, MutationFunctionOptions } from '../utils/cast-strategy';
import { _keyGenerator, DEFAULT_MAX_EXPIRY } from '../utils/constants';
Expand All @@ -19,7 +19,6 @@ import { ModelMetadata } from './interfaces/model-metadata.interface';
import { UpdateManyOptions } from './interfaces/update-many.interface';
import { ModelTypes, saveOptions } from './model.types';
import { getModelMetadata, getPopulated, setModelMetadata } from './utils/model.utils';
import { IConditionExpr } from '../query';
import { mergeDoc } from '../utils/merge';

/**
Expand Down Expand Up @@ -351,7 +350,7 @@ export const _buildModel = (metadata: ModelMetadata) => {
saveOptions.enforceRefCheck = options.enforceRefCheck;
}
try {
const before = await _Model.findOne(filter, { ...options, consistency: 1 });
const before = await _Model.findOne(filter, { ...options, consistency: SearchConsistency.LOCAL });
if (before) {
const toSave = new _Model({ ...before }).$wasNew();
toSave._applyData({ ...doc }, options.strict);
Expand Down

0 comments on commit ddb0d12

Please sign in to comment.