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 __eq__ method to Q to more easily test dynamically-built queries #1506

Merged
merged 1 commit into from
Apr 27, 2024

Conversation

cikay
Copy link
Contributor

@cikay cikay commented Oct 29, 2023

Add __eq__ method to Q to more easily test dynamically-built queries

Description

When a query is built dynamically it is needed to be tested if it is built correctly. As query is built in a tree it is too hard to test it.

Motivation and Context

Lets say the following query is built dynamically

def build_dynamic_query():
    return (Q(firstname="John") & Q(lastname="Doe")) | Q(mother_name="Jane")

Currently to test it, the following code needs to be written which is already too complicated with just three Q objects.

def test_build_dynamic_query():
    dynamic_query = build_dynamic_query()
    expected_query = (Q(firstname="John") & Q(lastname="Doe")) | Q(mother_name="Jane")

    assert dynamic_query.join_type == expected_query.join_type
    assert dynamic_query.filters == expected_query.filters

    q1_2, q3 = expected_query.children
    expected_q3 = Q(mother_name="Jane")
    assert q1_2.join_type == Q.AND

    assert q3.join_type == expected_q3.join_type
    assert q3.filters == expected_q3.filters
    assert q3.children == expected_q3.children

    q1, q2 = q1_2.children
    expected_q2 = Q(lastname="Doe")

    assert q2.join_type == expected_q2.join_type
    assert q2.filters == expected_q2.filters
    assert q2.children == expected_q2.children

    expected_q1 = Q(firstname="John")
    assert q1.join_type == expected_q1.join_type
    assert q1.filters == expected_q1.filters
    assert q1.children == expected_q1.children

With this feature, the result will be checked against the expected value

def test_build_dynamic_query():
    dynamic_query = build_dynamic_query()
    expected_query = (Q(firstname="John") & Q(lastname="Doe")) | Q(mother_name="Jane")
    assert dynamic_query == expected_query

How Has This Been Tested?

I added tests for various queries including basic, or, and, combination of and and or.

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.

@long2ice
Copy link
Member

long2ice commented Nov 3, 2023

Thanks! Please update changelog.

@cikay
Copy link
Contributor Author

cikay commented Nov 3, 2023

Thanks! Please update changelog.

Done

CHANGELOG.rst Outdated
------

Added

Copy link
Member

Choose a reason for hiding this comment

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

please add ^^^^^ here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not get it. Could you please provide an example and also document how to add changelog?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Just one thing, please add in 0.20.1 section.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hope it is okay now.

@cikay cikay force-pushed the query branch 2 times, most recently from c271747 to 8beb30a Compare November 5, 2023 09:52
CHANGELOG.rst Outdated
@@ -6,6 +6,12 @@ Changelog

.. rst-class:: emphasize-children

0.20.1
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 under 0.20 section

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@cikay cikay force-pushed the query branch 4 times, most recently from d929585 to 698afe2 Compare November 5, 2023 16:14
@abondar abondar merged commit 3fbde62 into tortoise:develop Apr 27, 2024
1 check 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.

3 participants