fb16776f9d
- Add PEP8 section to tox.ini - Add hacking to requirements to enforce OpenStack style requirements - Change setup.py to use PBR - Add setup.cfg - Fix formatting issues flagged by flake8 check - Add copyright notices to all remaining files - Update .gitignore file - Fix bug in expression.py where a variable was set incorrectly Change-Id: I634adba3a44b2bcebb4d8c5620cbade28c6c489a
146 lines
5.6 KiB
Python
146 lines
5.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- encoding: utf-8 -*-
|
|
#
|
|
# Copyright © 2014 Rackspace Hosting.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import datetime
|
|
|
|
# for Python2.6 compatability.
|
|
import unittest2 as unittest
|
|
|
|
import timex
|
|
|
|
|
|
class TestParse(unittest.TestCase):
|
|
def setUp(self):
|
|
super(TestParse, self).setUp()
|
|
self.dt = datetime.datetime(2014, 8, 1, 2, 10, 23, 550)
|
|
self.other_dt = datetime.datetime(2014, 8, 7, 3, 20, 0, 0)
|
|
|
|
def test_var(self):
|
|
exp = timex.parse("$test_thingy")
|
|
t = exp(test_thingy=self.dt)
|
|
self.assertFalse(t.is_range)
|
|
self.assertEqual(t.timestamp, self.dt)
|
|
|
|
def test_timestamp_add(self):
|
|
result = datetime.datetime(2014, 8, 2, 4, 10, 23, 550)
|
|
exp = timex.parse("$test_thingy + 1d 2h")
|
|
t = exp(test_thingy=self.dt)
|
|
self.assertFalse(t.is_range)
|
|
self.assertEqual(t.timestamp, result)
|
|
|
|
def test_timestamp_sub(self):
|
|
result = datetime.datetime(2014, 7, 31, 0, 10, 23, 550)
|
|
exp = timex.parse("$test_thingy - 1d 2h")
|
|
t = exp(test_thingy=self.dt)
|
|
self.assertFalse(t.is_range)
|
|
self.assertEqual(t.timestamp, result)
|
|
|
|
def test_timestamp_replace(self):
|
|
result = datetime.datetime(2014, 8, 7, 6, 10, 23, 550)
|
|
exp = timex.parse("$test_thingy @ 7d 6h")
|
|
t = exp(test_thingy=self.dt)
|
|
self.assertFalse(t.is_range)
|
|
self.assertEqual(t.timestamp, result)
|
|
|
|
def test_timerange(self):
|
|
exp = timex.parse("$test_thingy to $other")
|
|
t = exp(test_thingy=self.dt, other=self.other_dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, self.dt)
|
|
self.assertEqual(t.end, self.other_dt)
|
|
|
|
def test_timerange_add(self):
|
|
result_begin = datetime.datetime(2014, 8, 2, 4, 10, 23, 550)
|
|
result_end = datetime.datetime(2014, 8, 8, 5, 20, 0, 0)
|
|
exp = timex.parse("($test_thingy to $other) + 1d 2h")
|
|
t = exp(test_thingy=self.dt, other=self.other_dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_timerange_sub(self):
|
|
result_begin = datetime.datetime(2014, 7, 31, 0, 10, 23, 550)
|
|
result_end = datetime.datetime(2014, 8, 6, 1, 20, 0, 0)
|
|
exp = timex.parse("($test_thingy to $other) - 1d 2h")
|
|
t = exp(test_thingy=self.dt, other=self.other_dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_timerange_replace(self):
|
|
result_begin = datetime.datetime(2014, 8, 1, 6, 10, 23, 550)
|
|
result_end = datetime.datetime(2014, 8, 7, 6, 20, 0, 0)
|
|
exp = timex.parse("($test_thingy to $other) @ 6h")
|
|
t = exp(test_thingy=self.dt, other=self.other_dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_special_range(self):
|
|
result_begin = datetime.datetime(2014, 8, 1, 0, 0, 0, 0)
|
|
result_end = datetime.datetime(2014, 8, 2, 0, 0, 0, 0)
|
|
exp = timex.parse("day($test_thingy)")
|
|
t = exp(test_thingy=self.dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
exp = timex.parse("day")
|
|
t = exp(timestamp=self.dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_special_range_wrap_replace(self):
|
|
result_begin = datetime.datetime(2014, 7, 31, 6, 0, 0, 0)
|
|
result_end = datetime.datetime(2014, 8, 1, 6, 0, 0, 0)
|
|
exp = timex.parse("day @ 6h")
|
|
t = exp(timestamp=self.dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_special_range_wrap_add(self):
|
|
result_begin = datetime.datetime(2014, 7, 31, 6, 0, 0, 0)
|
|
result_end = datetime.datetime(2014, 8, 1, 6, 0, 0, 0)
|
|
exp = timex.parse("day + 6h")
|
|
t = exp(timestamp=self.dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_special_range_wrap_sub(self):
|
|
result_begin = datetime.datetime(2014, 8, 1, 18, 0, 0, 0)
|
|
result_end = datetime.datetime(2014, 8, 2, 18, 0, 0, 0)
|
|
exp = timex.parse("day - 6h")
|
|
t = exp(timestamp=datetime.datetime(2014, 8, 1, 19, 45, 30, 225))
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|
|
|
|
def test_timerange_ambig_duration(self):
|
|
# Ambiguous durations are a bit of a hack to make timex syntax
|
|
# compatable with the (much less flexible) syntax for timeranges
|
|
# used for some OpenStack projects. (mdragon)
|
|
result_begin = datetime.datetime(2014, 8, 1, 2, 0, 0, 0)
|
|
result_end = datetime.datetime(2014, 8, 2, 2, 0, 0, 0)
|
|
exp = timex.parse("day @ 2")
|
|
t = exp(timestamp=self.dt)
|
|
self.assertTrue(t.is_range)
|
|
self.assertEqual(t.begin, result_begin)
|
|
self.assertEqual(t.end, result_end)
|