Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
import unittest | ||
|
||
import hiro | ||
from hiro.errors import InvalidTypeError | ||
from hiro.utils import timedelta_to_seconds, time_in_seconds, chained | ||
|
||
|
||
class TestTimeDeltaToSeconds(unittest.TestCase): | ||
def test_fractional(self): | ||
delta = datetime.timedelta(seconds=1, microseconds=1000) | ||
self.assertAlmostEqual(timedelta_to_seconds(delta), 1.001) | ||
|
||
def test_days(self): | ||
delta = datetime.timedelta(days=10) | ||
self.assertEqual(timedelta_to_seconds(delta), | ||
delta.days * 24 * 60 * 60) | ||
|
||
|
||
class TestTimeInSeconds(unittest.TestCase): | ||
def test_passthrough(self): | ||
self.assertEqual(time_in_seconds(1), 1) | ||
self.assertEqual(time_in_seconds(1.0), 1.0) | ||
|
||
def test_date(self): | ||
d = datetime.date(1970, 1, 1) | ||
self.assertEqual(time_in_seconds(d), 0) | ||
|
||
def test_datetime(self): | ||
d = datetime.datetime(1970, 1, 1, 0, 0, 0) | ||
self.assertEqual(time_in_seconds(d), 0) | ||
|
||
def test_invalid_type(self): | ||
with self.assertRaises(InvalidTypeError): | ||
time_in_seconds("this is a string") | ||
|
||
|
||
class TestChained(unittest.TestCase): | ||
class Foo(object): | ||
@chained | ||
def return_value(self, value=None): | ||
return value | ||
|
||
def setUp(self): | ||
self.obj = self.Foo() | ||
|
||
def test_no_return(self): | ||
self.assertTrue(self.obj.return_value() is self.obj) | ||
|
||
def test_with_return(self): | ||
o = object() | ||
self.assertTrue(self.obj.return_value(o) is o) | ||
|
||
def test_kwargs(self): | ||
o = object() | ||
self.assertTrue(self.obj.return_value(value=o) is o) |