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

FakeQueryCondition._evaluate_for_range_query_type tries to isoparse a datetime object #84

Open
JimGomez48 opened this issue Mar 16, 2023 · 2 comments

Comments

@JimGomez48
Copy link

dateutil.parser.isoparse is meant to be used to transfrom ISO 8601 datetime strings into datetime objects. However, it is being used in FakeQueryCondition to parse a datetime object, which is both redundant and erroneous.

See https://github.com/vrcmarcos/elasticmock/blob/master/elasticmock/fake_elasticsearch.py#L179-L180

Proposal: Use a try/except block to try to parse the value. If exception on isoparse(), pass and continue.

@mRcSchwering
Copy link

I'm actually seeing the proble that doc_val stays a string for me in fake_elasticsearch:

          for sign, value in comparisons.items():
                if isinstance(doc_val, datetime.datetime):
                    value = dateutil.parser.isoparse(value)
                if sign == 'gte':
>                   if doc_val < value:
E                   TypeError: '<' not supported between instances of 'str' and 'datetime.datetime'

In the mapping the field is defined as {"type": "date"} and value seems to already be a datetime.datetime, but doc_val is still a string

@mRcSchwering
Copy link

mRcSchwering commented Jun 7, 2023

I see FakeIndicesClient ignores body, so types have to be converted by hand before adding.
In case anyone stumbles across this as well.
I patched dateutil.parser.isoparse to achieve what @JimGomez48 was talking about:

def isoparse(arg):
    try:
        return dt.datetime.fromisoformat(arg)
    except TypeError:
        return arg

@pytest.fixture(autouse=True, scope="module")
def mocked_es():
    ELASTIC_INSTANCES.clear()
    patch_es = patch("elasticsearch.Elasticsearch", _get_elasticmock)
    patch_bug = patch("dateutil.parser.isoparse", isoparse)
    with patch_es, patch_bug:
        _setup_mocked_es()  # loads testdata
        yield

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

No branches or pull requests

2 participants