Skip to content
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

fix(mongo): sorting with UnderscoreNamingStrategy #4314

Merged
merged 2 commits into from May 4, 2023

Conversation

funduck
Copy link
Contributor

@funduck funduck commented May 4, 2023

Fixes #4313

I propose to apply this.renameFields to options.orderBy the same way it is used for where

@codecov
Copy link

codecov bot commented May 4, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.09 ⚠️

Comparison is base (7d49ee7) 99.69% compared to head (ec4fbc8) 99.61%.

❗ Current head ec4fbc8 differs from pull request most recent head c74dae1. Consider uploading reports for the commit c74dae1 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4314      +/-   ##
==========================================
- Coverage   99.69%   99.61%   -0.09%     
==========================================
  Files         214      214              
  Lines       13785    14110     +325     
  Branches     3232     3325      +93     
==========================================
+ Hits        13743    14055     +312     
- Misses         39       54      +15     
+ Partials        3        1       -2     
Impacted Files Coverage Δ
packages/mongodb/src/MongoDriver.ts 99.35% <100.00%> (-0.65%) ⬇️

... and 67 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! interesting this wasnt reported before, i guess most of the mongo folks dont really use different strategies/field names

left a few comments about the test case, i'd like to see that greatly simplified, but will be happy to do that myself after merging if you'd struggle somehow with that.

it('Init orm with basic naming strategy',async () => {
orm = await MikroORM.init({
entities: [A],
clientUrl: await initMongoReplSet('mikro-orm-test'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for replica sets, that memory server implementation is rather unstable and i am using it only in places that are actually testing transactions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, how much faster the test have become without replset! Thanks for hint!

});
}

describe('GH issue 4313', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for nested describe blocks - actually no need for any of them, use just one test block on top level

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll make it shorter

Comment on lines 114 to 117
it('Close connection', async () => {
await orm.close();
await closeReplSets();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be afterAll hook, not a separate test case

Comment on lines 102 to 110
it('Init orm with underscore naming strategy',async () => {
orm = await MikroORM.init({
entities: [A],
clientUrl: await initMongoReplSet('mikro-orm-test'),
driver: MongoDriver,
namingStrategy: UnderscoreNamingStrategy,
});
await orm.schema.clearDatabase();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be beforeAll hook, not a separate test case

it('Init orm with underscore naming strategy',async () => {
orm = await MikroORM.init({
entities: [A],
clientUrl: await initMongoReplSet('mikro-orm-test'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the defaults, just set the database name via dbName

Comment on lines 20 to 35
test('Insert entities', async () => {
const a = new A();
a.complexName = 'a';
const b = new A();
b.complexName = 'b';
await orm.em.persistAndFlush(a);
await orm.em.persistAndFlush(b);
orm.em.clear();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be also part of that (single) beforeAll hook

Comment on lines 25 to 26
await orm.em.persistAndFlush(a);
await orm.em.persistAndFlush(b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can pass both in a single call

Suggested change
await orm.em.persistAndFlush(a);
await orm.em.persistAndFlush(b);
await orm.em.persistAndFlush([a, b]);

Comment on lines +55 to +83
const data1 = await orm.em.findOne(A, { complexName: { $ne: null } }, {
orderBy: { complexName: 'asc' },
});
expect(data1!.complexName).toBe('a');

const data2 = await orm.em.findOne(A, { complexName: { $ne: null } }, {
orderBy: { complexName: 'desc' },
});
expect(data2!.complexName).toBe('b');

const data3 = await orm.em.findOne(A, { complexName: { $ne: null } }, {
orderBy: [{ complexName: 'asc' }],
});
expect(data3!.complexName).toBe('a');

const data4 = await orm.em.findOne(A, { complexName: { $ne: null } }, {
orderBy: [{ complexName: 'desc' }],
});
expect(data4!.complexName).toBe('b');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think we need to test asc vs desc, that's kinda irrelevant to the issue, one direction is enough, the problem is about the left side, not right side

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one case may pass just by an accident, but both wont


let orm: MikroORM<MongoDriver>;

function testCase() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets inline this

@funduck funduck force-pushed the fix-mongo-sorting-underscore-naming branch from ec4fbc8 to 97ee08f Compare May 4, 2023 09:51
@funduck
Copy link
Contributor Author

funduck commented May 4, 2023

Fixed most of the issues you've mentioned
And yes, that's a surprise that nobody uses UnderscoreNaming with Mongo, if I knew earlier I would have used this fact as an argument when my team lead persuaded me to switch naming strategy :)

@funduck funduck requested a review from B4nan May 4, 2023 13:07
tests/issues/GH4313.test.ts Outdated Show resolved Hide resolved
tests/issues/GH4313.test.ts Outdated Show resolved Hide resolved
@B4nan B4nan merged commit a5b0f94 into mikro-orm:master May 4, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

orderBy is not working with UnderscoreNamingStrategy in Mongodb
2 participants