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

Fix for time based mongodb test #3630

Merged
merged 1 commit into from
Mar 24, 2019
Merged

Fix for time based mongodb test #3630

merged 1 commit into from
Mar 24, 2019

Conversation

ranbena
Copy link
Contributor

@ranbena ranbena commented Mar 24, 2019

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 rauchy and arikfr 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