| 1 |
from __future__ import print_function, division, absolute_import |
| 2 |
|
| 3 |
|
| 4 |
from datetime import datetime |
| 5 |
from decimal import Decimal |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from petl.test.helpers import eq_, ieq |
| 10 |
from petl.comparison import Comparable |
| 11 |
|
| 12 |
|
| 13 |
def test_comparable(): |
| 14 |
|
| 15 |
# bools |
| 16 |
d = [True, False] |
| 17 |
a = sorted(d, key=Comparable) |
| 18 |
e = [False, True] |
| 19 |
eq_(e, a) |
| 20 |
|
| 21 |
# ints |
| 22 |
d = [3, 1, 2] |
| 23 |
a = sorted(d, key=Comparable) |
| 24 |
e = [1, 2, 3] |
| 25 |
eq_(e, a) |
| 26 |
|
| 27 |
# floats |
| 28 |
d = [3., 1.2, 2.5] |
| 29 |
a = sorted(d, key=Comparable) |
| 30 |
e = [1.2, 2.5, 3.] |
| 31 |
eq_(e, a) |
| 32 |
|
| 33 |
# mixed numeric |
| 34 |
d = [3., 1, 2.5, Decimal('1.5')] |
| 35 |
a = sorted(d, key=Comparable) |
| 36 |
e = [1, Decimal('1.5'), 2.5, 3.] |
| 37 |
eq_(e, a) |
| 38 |
|
| 39 |
# mixed numeric and bool |
| 40 |
d = [True, False, -1.2, 2, .5] |
| 41 |
a = sorted(d, key=Comparable) |
| 42 |
e = [-1.2, False, .5, True, 2] |
| 43 |
eq_(e, a) |
| 44 |
|
| 45 |
# mixed numeric and None |
| 46 |
d = [3, None, 2.5] |
| 47 |
a = sorted(d, key=Comparable) |
| 48 |
e = [None, 2.5, 3.] |
| 49 |
eq_(e, a) |
| 50 |
|
| 51 |
# bytes |
| 52 |
d = [b'b', b'ccc', b'aa'] |
| 53 |
a = sorted(d, key=Comparable) |
| 54 |
e = [b'aa', b'b', b'ccc'] |
| 55 |
eq_(e, a) |
| 56 |
|
| 57 |
# text |
| 58 |
d = [u'b', u'ccc', u'aa'] |
| 59 |
a = sorted(d, key=Comparable) |
| 60 |
e = [u'aa', u'b', u'ccc'] |
| 61 |
eq_(e, a) |
| 62 |
|
| 63 |
# mixed bytes and text |
| 64 |
d = [u'b', b'ccc', b'aa'] |
| 65 |
a = sorted(d, key=Comparable) |
| 66 |
# N.B., expect always bytes < unicode |
| 67 |
e = [b'aa', b'ccc', u'b'] |
| 68 |
eq_(e, a) |
| 69 |
|
| 70 |
# mixed bytes and None |
| 71 |
d = [b'b', b'ccc', None, b'aa'] |
| 72 |
a = sorted(d, key=Comparable) |
| 73 |
e = [None, b'aa', b'b', b'ccc'] |
| 74 |
eq_(e, a) |
| 75 |
|
| 76 |
# mixed text and None |
| 77 |
d = [u'b', u'ccc', None, u'aa'] |
| 78 |
a = sorted(d, key=Comparable) |
| 79 |
e = [None, u'aa', u'b', u'ccc'] |
| 80 |
eq_(e, a) |
| 81 |
|
| 82 |
# mixed bytes, text and None |
| 83 |
d = [u'b', b'ccc', None, b'aa'] |
| 84 |
a = sorted(d, key=Comparable) |
| 85 |
# N.B., expect always bytes < unicode |
| 86 |
e = [None, b'aa', b'ccc', u'b'] |
| 87 |
eq_(e, a) |
| 88 |
|
| 89 |
# mixed bytes, text, numbers and None |
| 90 |
d = [u'b', True, b'ccc', False, None, b'aa', -1, 3.4] |
| 91 |
a = sorted(d, key=Comparable) |
| 92 |
e = [None, -1, False, True, 3.4, b'aa', b'ccc', u'b'] |
| 93 |
eq_(e, a) |
| 94 |
|
| 95 |
|
| 96 |
def test_comparable_datetime(): |
| 97 |
|
| 98 |
dt = datetime.now().replace |
| 99 |
|
| 100 |
# datetimes |
| 101 |
d = [dt(hour=12), dt(hour=3), dt(hour=1)] |
| 102 |
a = sorted(d, key=Comparable) |
| 103 |
e = [dt(hour=1), dt(hour=3), dt(hour=12)] |
| 104 |
eq_(e, a) |
| 105 |
|
| 106 |
# mixed datetime and None |
| 107 |
d = [dt(hour=12), None, dt(hour=3), dt(hour=1)] |
| 108 |
a = sorted(d, key=Comparable) |
| 109 |
e = [None, dt(hour=1), dt(hour=3), dt(hour=12)] |
| 110 |
eq_(e, a) |
| 111 |
|
| 112 |
# mixed datetime, numbers, bytes, text and None |
| 113 |
d = [dt(hour=12), None, dt(hour=3), u'b', True, b'ccc', False, b'aa', -1, |
| 114 |
3.4] |
| 115 |
a = sorted(d, key=Comparable) |
| 116 |
# N.B., because bytes and unicode type names have changed in PY3, |
| 117 |
# petl uses PY2 type names to try and achieve consistent behaviour across |
| 118 |
# versions, i.e., 'datetime' < 'str' < 'unicode' rather than 'bytes' < |
| 119 |
# 'datetime' < 'str' |
| 120 |
e = [None, -1, False, True, 3.4, dt(hour=3), dt(hour=12), b'aa', b'ccc', |
| 121 |
u'b'] |
| 122 |
eq_(e, a) |
| 123 |
|
| 124 |
|
| 125 |
def test_comparable_nested(): |
| 126 |
|
| 127 |
# lists |
| 128 |
d = [[3], [1], [2]] |
| 129 |
a = sorted(d, key=Comparable) |
| 130 |
e = [[1], [2], [3]] |
| 131 |
eq_(e, a) |
| 132 |
|
| 133 |
# tuples |
| 134 |
d = [(3,), (1,), (2,)] |
| 135 |
a = sorted(d, key=Comparable) |
| 136 |
e = [(1,), (2,), (3,)] |
| 137 |
eq_(e, a) |
| 138 |
|
| 139 |
# mixed lists and numeric |
| 140 |
d = [3, 1, [2]] |
| 141 |
a = sorted(d, key=Comparable) |
| 142 |
e = [1, 3, [2]] |
| 143 |
eq_(e, a) |
| 144 |
|
| 145 |
# lists containing None |
| 146 |
d = [[3], [None], [2]] |
| 147 |
a = sorted(d, key=Comparable) |
| 148 |
e = [[None], [2], [3]] |
| 149 |
eq_(e, a) |
| 150 |
|
| 151 |
# mixed lists and tuples |
| 152 |
d = [[3], [1], (2,)] |
| 153 |
a = sorted(d, key=Comparable) |
| 154 |
e = [[1], (2,), [3]] |
| 155 |
eq_(e, a) |
| 156 |
|
| 157 |
# length 2 lists |
| 158 |
d = [[3, 2], [3, 1], [2]] |
| 159 |
a = sorted(d, key=Comparable) |
| 160 |
e = [[2], [3, 1], [3, 2]] |
| 161 |
eq_(e, a) |
| 162 |
|
| 163 |
dt = datetime.now().replace |
| 164 |
|
| 165 |
# mixed everything |
| 166 |
d = [dt(hour=12), |
| 167 |
None, |
| 168 |
(dt(hour=3), 'b'), |
| 169 |
True, |
| 170 |
[b'aa', False], |
| 171 |
(b'aa', -1), |
| 172 |
3.4] |
| 173 |
a = sorted(d, key=Comparable) |
| 174 |
e = [None, |
| 175 |
True, |
| 176 |
3.4, |
| 177 |
dt(hour=12), |
| 178 |
(dt(hour=3), 'b'), |
| 179 |
(b'aa', -1), |
| 180 |
[b'aa', False]] |
| 181 |
eq_(e, a) |
| 182 |
|
| 183 |
|
| 184 |
def test_comparable_ieq_table(): |
| 185 |
rows = [[u'Bob', 42, 33], |
| 186 |
[u'Jim', 13, 69], |
| 187 |
[u'Joe', 86, 17], |
| 188 |
[u'Ted', 23, 51]] |
| 189 |
ieq(rows, rows) |
| 190 |
|
| 191 |
|
| 192 |
def test_comparable_ieq_rows(): |
| 193 |
rows = [['a', 'b', 'c'], [1, 2]] |
| 194 |
ieq(rows, rows) |
| 195 |
|
| 196 |
|
| 197 |
def test_comparable_ieq_missing(): |
| 198 |
x = ['a', 'b', 'c'] |
| 199 |
y = ['a', 'b'] |
| 200 |
with pytest.raises(AssertionError): |
| 201 |
ieq(x, y) |
| 202 |
with pytest.raises(AssertionError): |
| 203 |
ieq(y, x) |