Repository navigation
fix: transform inq to jsonContains for referencesMany relation queries #2615
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
Open
aaqilniz
wants to merge
5
commits into
loopbackio:master
Choose a base branch
from
aaqilniz:fix/inq-in-refmany
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef21e83
fix: handle inq for referencesMany
aaqilniz b864cf7
fix: hanlde nested inq
aaqilniz 0d8c7e2
test: test nested inq to jsoncontains
aaqilniz ee11df0
fix: remove dependency on rest-controller options
aaqilniz 8cb53b4
fix: remove unnecessary change
aaqilniz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| 'use strict'; | ||
|
|
||
| describe('DataAccessObject.find with referencesMany relations', function() { | ||
| let db, Post, connector; | ||
| let interceptedQuery; | ||
| let originalAllDescriptor; | ||
|
|
||
| before(function() { | ||
| /* global getSchema:false */ | ||
| db = getSchema(); | ||
|
|
||
| db.define('Tag', { | ||
| id: {type: Number, id: true}, | ||
| name: String, | ||
| }); | ||
|
|
||
| db.define('Category', { | ||
| id: {type: Number, id: true}, | ||
| name: String, | ||
| }); | ||
|
|
||
| Post = db.define('Post', { | ||
| id: {type: Number, id: true}, | ||
| title: String, | ||
| tagIds: {type: Array, sqliteType: 'JSON'}, | ||
| categoryIds: {type: Array, sqliteType: 'JSON'}, | ||
| }, { | ||
| relations: { | ||
| tags: { | ||
| type: 'referencesMany', | ||
| model: 'Tag', | ||
| keyFrom: 'tagIds', | ||
| }, | ||
| categories: { | ||
| type: 'referencesMany', | ||
| model: 'Category', | ||
| keyFrom: 'categoryIds', | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(async function() { | ||
| await db.automigrate(); | ||
|
|
||
| connector = db.connector; | ||
| interceptedQuery = undefined; | ||
| }); | ||
|
|
||
| afterEach(function() { | ||
| if (originalAllDescriptor) { | ||
| Object.defineProperty(connector, 'all', originalAllDescriptor); | ||
| } else { | ||
| delete connector.all; | ||
| } | ||
|
|
||
| originalAllDescriptor = undefined; | ||
| }); | ||
|
|
||
| function interceptConnectorAll() { | ||
| originalAllDescriptor = Object.getOwnPropertyDescriptor( | ||
| connector, | ||
| 'all', | ||
| ); | ||
|
|
||
| connector.all = function(model, query, options, cb) { | ||
| interceptedQuery = query; | ||
| cb(null, []); | ||
| }; | ||
| } | ||
| it('transforms inq conditions on referencesMany properties', async function() { | ||
| interceptConnectorAll(); | ||
|
|
||
| const query = { | ||
| where: { | ||
| tagIds: {inq: [1, 2]}, | ||
| }, | ||
| }; | ||
|
|
||
| await Post.find(query, {model: Post}); | ||
|
|
||
| interceptedQuery.should.deepEqual({ | ||
| where: { | ||
| tagIds: {jsonContains: [1, 2]}, | ||
| }, | ||
| }); | ||
| }); | ||
| it('transforms multiple referencesMany properties', async function() { | ||
| interceptConnectorAll(); | ||
|
|
||
| const query = { | ||
| where: { | ||
| tagIds: {inq: [1, 2]}, | ||
| categoryIds: {inq: [3, 4]}, | ||
| }, | ||
| }; | ||
|
|
||
| await Post.find(query, {model: Post}); | ||
|
|
||
| interceptedQuery.should.deepEqual({ | ||
| where: { | ||
| tagIds: {jsonContains: [1, 2]}, | ||
| categoryIds: {jsonContains: [3, 4]}, | ||
| }, | ||
| }); | ||
| }); | ||
| it('transforms nested and/or conditions', async function() { | ||
| interceptConnectorAll(); | ||
|
|
||
| const query = { | ||
| where: { | ||
| and: [ | ||
| {tagIds: {inq: [1, 2]}}, | ||
| { | ||
| or: [ | ||
| {categoryIds: {inq: [3, 4]}}, | ||
| {title: 'Example'}, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| await Post.find(query, {model: Post}); | ||
|
|
||
| interceptedQuery.should.deepEqual({ | ||
| where: { | ||
| and: [ | ||
| {tagIds: {jsonContains: [1, 2]}}, | ||
| { | ||
| or: [ | ||
| {categoryIds: {jsonContains: [3, 4]}}, | ||
| {title: 'Example'}, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
| it('does not transform inq conditions on non-referencesMany properties', async function() { | ||
| interceptConnectorAll(); | ||
|
|
||
| const query = { | ||
| where: { | ||
| title: {inq: ['First', 'Second']}, | ||
| }, | ||
| }; | ||
|
|
||
| await Post.find(query, {model: Post}); | ||
|
|
||
| interceptedQuery.should.deepEqual(query); | ||
| }); | ||
| it('does not transform non-inq operators on referencesMany properties', async function() { | ||
| interceptConnectorAll(); | ||
|
|
||
| const query = { | ||
| where: { | ||
| tagIds: {eq: [1, 2]}, | ||
| }, | ||
| }; | ||
|
|
||
| await Post.find(query, {model: Post}); | ||
|
|
||
| interceptedQuery.should.deepEqual(query); | ||
| }); | ||
| it('handles queries without options.model gracefully', async function() { | ||
| interceptConnectorAll(); | ||
|
|
||
| const query = { | ||
| where: { | ||
| tagIds: {inq: [1, 2]}, | ||
| }, | ||
| }; | ||
|
|
||
| await Post.find(query); | ||
|
|
||
| interceptedQuery.should.deepEqual({ | ||
| where: { | ||
| tagIds: {jsonContains: [1, 2]}, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests verify that the query object is transformed, but they don't verify that the transformed query actually works.
Could we add integration tests using real stored records to validate:
inqbehavior with the memory connectorAlso, we're overwriting
connector.allwithout restoring it. We should restore the original method after each test to avoid affecting other tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added tests for multiple referencesMany properties, nested and/or conditions, and ensuring unrelated properties/operators remain unchanged.
For integration tests, I think we'll have to wait until these changes to mysql-connector become available.
For now, the DAO tests verify the query transformation; they cannot validate SQL generation or actual database matching behavior.