Created
January 7, 2013 16:18
-
-
Save tomhennigan/4476203 to your computer and use it in GitHub Desktop.
relativedelta comparator test (<, >, <=, >=, __cmp__)
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
from dateutil.relativedelta import relativedelta | |
import unittest | |
class Test_relativedelta(unittest.TestCase): | |
def test___lt__(self): | |
# For all fields test that a relative delta with only a single field | |
# set behaves as expected. | |
self.assertLess(relativedelta(years=1990), relativedelta(years=2012)) | |
self.assertLess(relativedelta(months=1), relativedelta(months=12)) | |
self.assertLess(relativedelta(days=40), relativedelta(days=100)) | |
self.assertLess(relativedelta(hours=1), relativedelta(hours=23)) | |
self.assertLess(relativedelta(minutes=13), relativedelta(minutes=59)) | |
self.assertLess(relativedelta(seconds=50), relativedelta(seconds=59)) | |
self.assertLess(relativedelta(microseconds=40), relativedelta(microseconds=99)) | |
# Check with trivially wrong cases. | |
self.assertFalse(relativedelta(years=2) < relativedelta(years=1, months=11)) | |
# Check where all values are increment in size (years > months) that | |
# the logic holds. | |
self.assertLess(relativedelta(years=1, months=2, days=3, hours=4, minutes=5, seconds=6, microseconds=7), relativedelta(years=2)) | |
def test___gt__(self): | |
# For all fields test that a relative delta with only a single field | |
# set behaves as expected. | |
self.assertGreater(relativedelta(years=2012), relativedelta(years=1990)) | |
self.assertGreater(relativedelta(months=12), relativedelta(months=1)) | |
self.assertGreater(relativedelta(days=100), relativedelta(days=40)) | |
self.assertGreater(relativedelta(hours=23), relativedelta(hours=1)) | |
self.assertGreater(relativedelta(minutes=59), relativedelta(minutes=13)) | |
self.assertGreater(relativedelta(seconds=59), relativedelta(seconds=50)) | |
self.assertGreater(relativedelta(microseconds=99), relativedelta(microseconds=40)) | |
# Check with trivially wrong cases. | |
self.assertFalse(relativedelta(years=1, months=11) > relativedelta(years=2)) | |
# Check where all values are increment in size (years > months) that | |
# the logic holds. | |
self.assertGreater(relativedelta(years=2), relativedelta(years=1, months=2, days=3, hours=4, minutes=5, seconds=6, microseconds=7)) | |
def test___ge__(self): | |
# For all fields test that a relative delta with only a single field | |
# set behaves as expected. | |
# First up, check > | |
self.assertGreaterEqual(relativedelta(years=2012), relativedelta(years=1990)) | |
self.assertGreaterEqual(relativedelta(months=12), relativedelta(months=1)) | |
self.assertGreaterEqual(relativedelta(days=100), relativedelta(days=40)) | |
self.assertGreaterEqual(relativedelta(hours=23), relativedelta(hours=1)) | |
self.assertGreaterEqual(relativedelta(minutes=59), relativedelta(minutes=13)) | |
self.assertGreaterEqual(relativedelta(seconds=59), relativedelta(seconds=50)) | |
self.assertGreaterEqual(relativedelta(microseconds=99), relativedelta(microseconds=40)) | |
# Now check == | |
self.assertGreaterEqual(relativedelta(years=2012), relativedelta(years=2012)) | |
self.assertGreaterEqual(relativedelta(months=12), relativedelta(months=12)) | |
self.assertGreaterEqual(relativedelta(days=100), relativedelta(days=100)) | |
self.assertGreaterEqual(relativedelta(hours=23), relativedelta(hours=23)) | |
self.assertGreaterEqual(relativedelta(minutes=59), relativedelta(minutes=59)) | |
self.assertGreaterEqual(relativedelta(seconds=59), relativedelta(seconds=59)) | |
self.assertGreaterEqual(relativedelta(microseconds=99), relativedelta(microseconds=99)) | |
# Check with trivially wrong cases. | |
self.assertFalse(relativedelta(years=1, months=11) >= relativedelta(years=2)) | |
# Check where all values are increment in size (years > months) that | |
# the logic holds. | |
self.assertGreaterEqual(relativedelta(years=2), relativedelta(years=1, months=2, days=3, hours=4, minutes=5, seconds=6, microseconds=7)) | |
def test___le__(self): | |
# For all fields test that a relative delta with only a single field | |
# set behaves as expected. | |
# First up, check < | |
self.assertLessEqual(relativedelta(years=1990), relativedelta(years=2012)) | |
self.assertLessEqual(relativedelta(months=1), relativedelta(months=12)) | |
self.assertLessEqual(relativedelta(days=40), relativedelta(days=100)) | |
self.assertLessEqual(relativedelta(hours=1), relativedelta(hours=23)) | |
self.assertLessEqual(relativedelta(minutes=13), relativedelta(minutes=59)) | |
self.assertLessEqual(relativedelta(seconds=50), relativedelta(seconds=59)) | |
self.assertLessEqual(relativedelta(microseconds=40), relativedelta(microseconds=99)) | |
# Now check == | |
self.assertLessEqual(relativedelta(years=1990), relativedelta(years=1990)) | |
self.assertLessEqual(relativedelta(months=1), relativedelta(months=1)) | |
self.assertLessEqual(relativedelta(days=40), relativedelta(days=40)) | |
self.assertLessEqual(relativedelta(hours=1), relativedelta(hours=1)) | |
self.assertLessEqual(relativedelta(minutes=13), relativedelta(minutes=13)) | |
self.assertLessEqual(relativedelta(seconds=50), relativedelta(seconds=50)) | |
self.assertLessEqual(relativedelta(microseconds=40), relativedelta(microseconds=40)) | |
# Check with trivially wrong cases. | |
self.assertFalse(relativedelta(years=2) <= relativedelta(years=1, months=11)) | |
# Check where all values are increment in size (years > months) that | |
# the logic holds. | |
self.assertLessEqual(relativedelta(years=1, months=2, days=3, hours=4, minutes=5, seconds=6, microseconds=7), relativedelta(years=2)) | |
def test___cmp__(self): | |
self.assertEquals(-1, relativedelta(years=1990).__cmp__(relativedelta(years=2012))) | |
self.assertEquals(-1, relativedelta(months=1).__cmp__(relativedelta(months=12))) | |
self.assertEquals(-1, relativedelta(days=40).__cmp__(relativedelta(days=100))) | |
self.assertEquals(-1, relativedelta(hours=1).__cmp__(relativedelta(hours=23))) | |
self.assertEquals(-1, relativedelta(minutes=13).__cmp__(relativedelta(minutes=59))) | |
self.assertEquals(-1, relativedelta(seconds=50).__cmp__(relativedelta(seconds=59))) | |
self.assertEquals(-1, relativedelta(microseconds=40).__cmp__(relativedelta(microseconds=99))) | |
self.assertEquals(1, relativedelta(years=2012).__cmp__(relativedelta(years=1990))) | |
self.assertEquals(1, relativedelta(months=12).__cmp__(relativedelta(months=1))) | |
self.assertEquals(1, relativedelta(days=100).__cmp__(relativedelta(days=40))) | |
self.assertEquals(1, relativedelta(hours=23).__cmp__(relativedelta(hours=1))) | |
self.assertEquals(1, relativedelta(minutes=59).__cmp__(relativedelta(minutes=13))) | |
self.assertEquals(1, relativedelta(seconds=59).__cmp__(relativedelta(seconds=50))) | |
self.assertEquals(1, relativedelta(microseconds=99).__cmp__(relativedelta(microseconds=40))) | |
self.assertEquals(0, relativedelta(years=2012).__cmp__(relativedelta(years=2012))) | |
self.assertEquals(0, relativedelta(months=12).__cmp__(relativedelta(months=12))) | |
self.assertEquals(0, relativedelta(days=100).__cmp__(relativedelta(days=100))) | |
self.assertEquals(0, relativedelta(hours=23).__cmp__(relativedelta(hours=23))) | |
self.assertEquals(0, relativedelta(minutes=59).__cmp__(relativedelta(minutes=59))) | |
self.assertEquals(0, relativedelta(seconds=59).__cmp__(relativedelta(seconds=59))) | |
self.assertEquals(0, relativedelta(microseconds=99).__cmp__(relativedelta(microseconds=99))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment