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

Add Postgres JSON filters #1789

Merged
merged 5 commits into from
Nov 28, 2024

Conversation

henadzit
Copy link
Contributor

@henadzit henadzit commented Nov 25, 2024

Description

This PR:

  • Adds new Postgres-specific JSON filter lookup types such as in, not_in, gt, lt, etc. This is based on changes from Additional Postgresql JSON Functions #1215
  • Enables tests from TestJSONFields which were disabled previously by mistake. Apparently if you stack @test.requireCapability(dialect="mysql") and @test.requireCapability(dialect="postgres") on top of each other, the test will not run for neither mysql or postgres
  • Fixes JSON array referencing which seems to have been broken for a while, e.g. Model.filter(data__filter={"names_0": "John"})
  • Updates the docs

Motivation and Context

JSON and JSONB fields are very popular with people who use Postgres. It makes sense to support a range of JSON operations. Django offers similar functionality.

Related issues:

How Has This Been Tested?

Added more tests and enabled the existing ones.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 12016619424

Details

  • 28 of 28 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.8%) to 90.282%

Totals Coverage Status
Change from base Build 12007385729: 0.8%
Covered Lines: 6305
Relevant Lines: 6877

💛 - Coveralls

Comment on lines +53 to +73
"gte": cast(Callable[..., Criterion], operator.ge),
"gt": cast(Callable[..., Criterion], operator.gt),
"lte": cast(Callable[..., Criterion], operator.le),
"lt": cast(Callable[..., Criterion], operator.lt),
"range": between_and,
"contains": contains,
"startswith": starts_with,
"endswith": ends_with,
"iexact": insensitive_exact,
"icontains": insensitive_contains,
"istartswith": insensitive_starts_with,
"iendswith": insensitive_ends_with,
"year": extract_year_equal,
"quarter": extract_quarter_equal,
"month": extract_month_equal,
"week": extract_week_equal,
"day": extract_day_equal,
"hour": extract_hour_equal,
"minute": extract_minute_equal,
"second": extract_second_equal,
"microsecond": extract_microsecond_equal,
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need more test for all these new operators?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@abondar there are tests in tests/contrib/postgres/test_json.py for all of these. Can you suggest any particular test cases you want me to add?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I am in wrong here, for some reason I only saw file with generic test_json, and didn't notice file with postgres-specific test_json

@henadzit henadzit merged commit 962de96 into tortoise:develop Nov 28, 2024
7 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.

Additional JSON Functions for Postgres Could JSON Fields support filter like 'in' 'gt' 'lt' ?
4 participants