Skip to content

Fix for time based mongodb test#3630

Merged
ranbena merged 1 commit into
masterfrom
fix-pytest
Mar 24, 2019
Merged

Fix for time based mongodb test#3630
ranbena merged 1 commit into
masterfrom
fix-pytest

Conversation

@ranbena

@ranbena ranbena commented Mar 24, 2019

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Bug Fix

Description

The following is flaky since if a second passes between the two time dependent commands, the test fails as it did here resulting in:

=================================== FAILURES ===================================
_____________ TestParseQueryJson.test_supports_relative_timestamps _____________

self = <tests.query_runner.test_mongodb.TestParseQueryJson testMethod=test_supports_relative_timestamps>

    def test_supports_relative_timestamps(self):
        query = {
            'ts': {'$humanTime': '1 hour ago'}
        }
    
        one_hour_ago = parse_human_time("1 hour ago")
        query_data = parse_query_json(json_dumps(query))
>       self.assertEqual(query_data['ts'], one_hour_ago)
E       AssertionError: datetime.datetime(2019, 3, 21, 9, 24, 12) != datetime.datetime(2019, 3, 21, 9, 24, 11)

Reproduce

Insert a 2 second sleep.

one_hour_ago = parse_human_time("1 hour ago")
time.sleep(2) # <-- 2s delay
query_data = parse_query_json(json_dumps(query))
self.assertEqual(query_data['ts'], one_hour_ago) # <-- fails due to 2s delta

The fix

Mock "now()" with freezegun.

@freeze_time('2019-01-01 12:00:00')
def test_supports_relative_timestamps(self):
    query = {
        'ts': {'$humanTime': '1 hour ago'}
    }

    one_hour_ago = parse_human_time("1 hour ago")
    time.sleep(2) # <-- 2s delay
    query_data = parse_query_json(json_dumps(query))
    self.assertEqual(query_data['ts'], one_hour_ago) # <-- passes

@ranbena
ranbena requested review from arikfr and rauchy March 24, 2019 09:17
@ranbena
ranbena merged commit aea3c9d into master Mar 24, 2019
@ranbena
ranbena deleted the fix-pytest branch March 24, 2019 09:29
harveyrendell pushed a commit to pushpay/redash that referenced this pull request Nov 14, 2019
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.

2 participants