Skip to content

Commit

Permalink
test(AutoIncrementID): add test for skipping id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Sep 1, 2023
1 parent ca1bb67 commit 2ea4988
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ doc[AutoIncrementIDSkipSymbol] = true;
await doc.save();
```

Note: `AutoIncrementIDSkipSymbol` can also be set inside hooks, but hooks might be called before others.
Note: `AutoIncrementIDSkipSymbol` can also be set inside hooks, but hooks might be called before others.
Note: `AutoIncrementIDSkipSymbol` cannot be used for `.create`, because `AutoIncrementIDSkipSymbol` works on the current document and `.create({ [AutoIncrementIDSkipSymbol]: true })` will not transfer symbols to the document

#### incrementBy

Expand Down
55 changes: 54 additions & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getModelForClass, modelOptions, plugin, prop } from '@typegoose/typegoose';
import { assertion } from '@typegoose/typegoose/lib/internal/utils';
import * as mongoose from 'mongoose';
import { AutoIncrementID, AutoIncrementSimple } from '../src/autoIncrement';
import { AutoIncrementID, AutoIncrementIDSkipSymbol, AutoIncrementSimple } from '../src/autoIncrement';

describe('Basic Suite', () => {
describe('AutoIncrementSimple', () => {
Expand Down Expand Up @@ -490,6 +490,59 @@ describe('Basic Suite', () => {
expect(foundTracker.count).toEqual(0n);
});

it('should support skipping with symbol', async () => {
const schema = new mongoose.Schema({
_id: Number,
somefield: Number,
});
schema.plugin(AutoIncrementID, {});
const model = mongoose.model('AutoIncrementID-SomeModel-skiptest', schema);

// test initial 0
{
const doc = await model.create({ somefield: 10 });
expect(doc.somefield).toBe(10);
expect(doc._id).toBe(0);

await doc.save();
expect(doc.somefield).toBe(10);
expect(doc._id).toBe(0);
}

// test add 1
{
// skip only works with the current document, not globally, so ".create" will not work
// because using model.create({ _id: 300, somefield: 20, [AutoIncrementIDSkipSymbol]: true })
// does not transfer symbols
const doc = new model({ _id: 300, somefield: 20 });
doc[AutoIncrementIDSkipSymbol] = true;
await doc.save();
expect(doc.somefield).toBe(20);
expect(doc._id).toBe(300);

await doc.save();
expect(doc.somefield).toBe(20);
expect(doc._id).toBe(300);
}

// test add another 1
{
const doc = await model.create({ somefield: 30 });
expect(doc.somefield).toBe(30);
expect(doc._id).toBe(1);

await doc.save();
expect(doc.somefield).toBe(30);
expect(doc._id).toBe(1);
}

const trackerModel = mongoose.connection.models['identitycounter'];
expect(Object.getPrototypeOf(trackerModel)).toStrictEqual(mongoose.Model);

const foundTracker = await trackerModel.findOne({ modelName: model.modelName }).orFail();
expect(foundTracker.count).toEqual(1n);
});

it('should throw a error if "overwriteModelName" is a function but returns a empty string', async () => {
let fcalled = 0;

Expand Down

0 comments on commit 2ea4988

Please sign in to comment.