|
1
|
|
|
import datetime |
|
2
|
|
|
|
|
3
|
|
|
import arrow |
|
4
|
|
|
|
|
5
|
|
|
from st2tests.base import BaseActionTestCase |
|
6
|
|
|
|
|
7
|
|
|
from parse_date_string import ParseDateStringAction |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class ParseDateStringActionTestCase(BaseActionTestCase): |
|
11
|
|
|
def test_run_success(self): |
|
12
|
|
|
action = ParseDateStringAction() |
|
13
|
|
|
|
|
14
|
|
|
result = action.run(date_string='now') |
|
15
|
|
|
expected = arrow.utcnow().timestamp |
|
16
|
|
|
self.assertTimestampMatches(result, expected) |
|
17
|
|
|
|
|
18
|
|
|
result = action.run(date_string='1 hour ago') |
|
19
|
|
|
expected = arrow.utcnow().replace(hours=-1).timestamp |
|
20
|
|
|
self.assertTimestampMatches(result, expected) |
|
21
|
|
|
|
|
22
|
|
|
result = action.run(date_string='3 days ago') |
|
23
|
|
|
expected = arrow.utcnow().replace(days=-3).timestamp |
|
24
|
|
|
self.assertTimestampMatches(result, expected) |
|
25
|
|
|
|
|
26
|
|
|
result = action.run(date_string='2013-05-12') |
|
27
|
|
|
expected = datetime.datetime(2013, 5, 12) |
|
28
|
|
|
expected = arrow.get(expected).timestamp |
|
29
|
|
|
self.assertEqual(result, expected) |
|
30
|
|
|
|
|
31
|
|
|
def test_run_invalid_date_string(self): |
|
32
|
|
|
action = ParseDateStringAction() |
|
33
|
|
|
self.assertRaises(ValueError, action.run, date_string='some invalid string') |
|
34
|
|
|
|
|
35
|
|
|
def assertTimestampMatches(self, actual_ts, expected_ts): |
|
36
|
|
|
""" |
|
37
|
|
|
Custom assert function which allows actual result to drift from the expected one |
|
38
|
|
|
for +/- 1 second (this is to account for the time between the function runs). |
|
39
|
|
|
""" |
|
40
|
|
|
self.assertTrue(actual_ts in [expected_ts, (expected_ts + 1), (expected_ts - 1)]) |
|
41
|
|
|
|