Skip to content

Commit

Permalink
Merge pull request #2195 from tamarbuta/fix-intermittent-configtest
Browse files Browse the repository at this point in the history
Add assertDatetimeAlmostEqual helper for comparing datetime objects.
  • Loading branch information
glasserc committed Jul 11, 2019
2 parents 3795521 + 0bb6288 commit b6a6255
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -15,6 +15,7 @@ This document describes changes between each past release.
**Bug fixes**

- Fix apparence of Admin notifications (fixes #2191)
- Fix intermittent failure when comparing timestamps in ConfigTest (fixes #2129)


13.2.1 (2019-06-21)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Expand Up @@ -73,6 +73,7 @@ Contributors
* realsumit <sumitsarinofficial@gmail.com>
* Rektide <rektide@voodoowarez.com>
* Rémy Hubscher <rhubscher@mozilla.com>
* Renisha Nellums <r.nellums@gmail.com>
* Ricardo <@rkleine>
* Rodolphe Quiédeville <rodolphe@quiedeville.org>
* Sahil Dua <sahildua2305@gmail.com>
Expand Down
54 changes: 49 additions & 5 deletions tests/test_config.py
Expand Up @@ -2,6 +2,7 @@
import os
import tempfile
import unittest
from datetime import datetime, timedelta
from time import strftime
from unittest import mock

Expand All @@ -10,6 +11,29 @@


class ConfigTest(unittest.TestCase):
def _assertTimestampStringsAlmostEqual(self, s1, s2, delta=timedelta(seconds=1)):
"""Assert that two timestamp strings are almost equal, within a
specified timedelta.
:param s1: a string representing a timestamp with the format
format %a, %d %b %Y %H:%M:%S %z
:param s2: a string representing a timestamp with the format
format %a, %d %b %Y %H:%M:%S %z
:param delta: timedelta, default is 1 second
:returns: True, if time between s1 and s2 is less than delta.
False, otherwise.
"""
s1 = datetime.strptime(s1, "%a, %d %b %Y %H:%M:%S %z")
s2 = datetime.strptime(s2, "%a, %d %b %Y %H:%M:%S %z")

return self.assertLessEqual(
abs(s1 - s2),
delta,
f"Delta between {s1} and {s2} is {s1 - s2}. Expected difference "
f"to be less than or equal to {delta}",
)

def test_transpose_parameters_into_template(self):
self.maxDiff = None
template = "kinto.tpl"
Expand Down Expand Up @@ -85,10 +109,15 @@ def test_init_postgresql_values(self, mocked_render_template):
"cache_url": postgresql_url,
"permission_url": postgresql_url,
"kinto_version": __version__,
"config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"),
"config_file_timestamp": mock.ANY,
},
)

self._assertTimestampStringsAlmostEqual(
strftime("%a, %d %b %Y %H:%M:%S %z"), # expected
kwargs["config_file_timestamp"], # actual
)

@mock.patch("kinto.config.render_template")
def test_init_postgresql_memcached_values(self, mocked_render_template):
config.init("kinto.ini", backend="postgresql", cache_backend="memcached")
Expand All @@ -110,10 +139,15 @@ def test_init_postgresql_memcached_values(self, mocked_render_template):
"cache_url": cache_url,
"permission_url": postgresql_url,
"kinto_version": __version__,
"config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"),
"config_file_timestamp": mock.ANY,
},
)

self._assertTimestampStringsAlmostEqual(
strftime("%a, %d %b %Y %H:%M:%S %z"), # expected
kwargs["config_file_timestamp"], # actual
)

@mock.patch("kinto.config.render_template")
def test_init_redis_values(self, mocked_render_template):
config.init("kinto.ini", backend="redis", cache_backend="redis")
Expand All @@ -135,10 +169,15 @@ def test_init_redis_values(self, mocked_render_template):
"cache_url": redis_url + "/2",
"permission_url": redis_url + "/3",
"kinto_version": __version__,
"config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"),
"config_file_timestamp": mock.ANY,
},
)

self._assertTimestampStringsAlmostEqual(
strftime("%a, %d %b %Y %H:%M:%S %z"), # expected
kwargs["config_file_timestamp"], # actual
)

@mock.patch("kinto.config.render_template")
def test_init_memory_values(self, mocked_render_template):
config.init("kinto.ini", backend="memory", cache_backend="memory")
Expand All @@ -158,10 +197,15 @@ def test_init_memory_values(self, mocked_render_template):
"cache_url": "",
"permission_url": "",
"kinto_version": __version__,
"config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"),
"config_file_timestamp": mock.ANY,
},
)

self._assertTimestampStringsAlmostEqual(
strftime("%a, %d %b %Y %H:%M:%S %z"), # expected
kwargs["config_file_timestamp"], # actual
)

def test_render_template_works_with_file_in_cwd(self):
temp_path = tempfile.mkdtemp()
os.chdir(temp_path)
Expand All @@ -179,6 +223,6 @@ def test_render_template_works_with_file_in_cwd(self):
"permission_url": "",
"kinto_version": "",
"config_file_timestamp": "",
}
},
)
self.assertTrue(os.path.exists(os.path.join(temp_path, "kinto.ini")))

0 comments on commit b6a6255

Please sign in to comment.