mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -3,14 +3,13 @@ from django.test import SimpleTestCase
|
||||
|
||||
|
||||
class GEOSCoordSeqTest(SimpleTestCase):
|
||||
|
||||
def test_getitem(self):
|
||||
coord_seq = LineString([(x, x) for x in range(2)]).coord_seq
|
||||
for i in (0, 1):
|
||||
with self.subTest(i):
|
||||
self.assertEqual(coord_seq[i], (i, i))
|
||||
for i in (-3, 10):
|
||||
msg = 'invalid GEOS Geometry index: %s' % i
|
||||
msg = "invalid GEOS Geometry index: %s" % i
|
||||
with self.subTest(i):
|
||||
with self.assertRaisesMessage(IndexError, msg):
|
||||
coord_seq[i]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,12 @@
|
||||
import unittest
|
||||
|
||||
from django.contrib.gis.geos import (
|
||||
LinearRing, LineString, MultiPoint, Point, Polygon, fromstr,
|
||||
LinearRing,
|
||||
LineString,
|
||||
MultiPoint,
|
||||
Point,
|
||||
Polygon,
|
||||
fromstr,
|
||||
)
|
||||
|
||||
|
||||
@@ -66,8 +71,9 @@ def api_get_length(x):
|
||||
|
||||
|
||||
geos_function_tests = [
|
||||
val for name, val in vars().items()
|
||||
if hasattr(val, '__call__') and name.startswith('api_get_')
|
||||
val
|
||||
for name, val in vars().items()
|
||||
if hasattr(val, "__call__") and name.startswith("api_get_")
|
||||
]
|
||||
|
||||
|
||||
@@ -78,7 +84,7 @@ class GEOSMutationTest(unittest.TestCase):
|
||||
"""
|
||||
|
||||
def test00_GEOSIndexException(self):
|
||||
'Testing Geometry IndexError'
|
||||
"Testing Geometry IndexError"
|
||||
p = Point(1, 2)
|
||||
for i in range(-2, 2):
|
||||
p._checkindex(i)
|
||||
@@ -88,87 +94,124 @@ class GEOSMutationTest(unittest.TestCase):
|
||||
p._checkindex(-3)
|
||||
|
||||
def test01_PointMutations(self):
|
||||
'Testing Point mutations'
|
||||
for p in (Point(1, 2, 3), fromstr('POINT (1 2 3)')):
|
||||
self.assertEqual(p._get_single_external(1), 2.0, 'Point _get_single_external')
|
||||
"Testing Point mutations"
|
||||
for p in (Point(1, 2, 3), fromstr("POINT (1 2 3)")):
|
||||
self.assertEqual(
|
||||
p._get_single_external(1), 2.0, "Point _get_single_external"
|
||||
)
|
||||
|
||||
# _set_single
|
||||
p._set_single(0, 100)
|
||||
self.assertEqual(p.coords, (100.0, 2.0, 3.0), 'Point _set_single')
|
||||
self.assertEqual(p.coords, (100.0, 2.0, 3.0), "Point _set_single")
|
||||
|
||||
# _set_list
|
||||
p._set_list(2, (50, 3141))
|
||||
self.assertEqual(p.coords, (50.0, 3141.0), 'Point _set_list')
|
||||
self.assertEqual(p.coords, (50.0, 3141.0), "Point _set_list")
|
||||
|
||||
def test02_PointExceptions(self):
|
||||
'Testing Point exceptions'
|
||||
"Testing Point exceptions"
|
||||
with self.assertRaises(TypeError):
|
||||
Point(range(1))
|
||||
with self.assertRaises(TypeError):
|
||||
Point(range(4))
|
||||
|
||||
def test03_PointApi(self):
|
||||
'Testing Point API'
|
||||
"Testing Point API"
|
||||
q = Point(4, 5, 3)
|
||||
for p in (Point(1, 2, 3), fromstr('POINT (1 2 3)')):
|
||||
for p in (Point(1, 2, 3), fromstr("POINT (1 2 3)")):
|
||||
p[0:2] = [4, 5]
|
||||
for f in geos_function_tests:
|
||||
self.assertEqual(f(q), f(p), 'Point ' + f.__name__)
|
||||
self.assertEqual(f(q), f(p), "Point " + f.__name__)
|
||||
|
||||
def test04_LineStringMutations(self):
|
||||
'Testing LineString mutations'
|
||||
for ls in (LineString((1, 0), (4, 1), (6, -1)),
|
||||
fromstr('LINESTRING (1 0,4 1,6 -1)')):
|
||||
self.assertEqual(ls._get_single_external(1), (4.0, 1.0), 'LineString _get_single_external')
|
||||
"Testing LineString mutations"
|
||||
for ls in (
|
||||
LineString((1, 0), (4, 1), (6, -1)),
|
||||
fromstr("LINESTRING (1 0,4 1,6 -1)"),
|
||||
):
|
||||
self.assertEqual(
|
||||
ls._get_single_external(1),
|
||||
(4.0, 1.0),
|
||||
"LineString _get_single_external",
|
||||
)
|
||||
|
||||
# _set_single
|
||||
ls._set_single(0, (-50, 25))
|
||||
self.assertEqual(ls.coords, ((-50.0, 25.0), (4.0, 1.0), (6.0, -1.0)), 'LineString _set_single')
|
||||
self.assertEqual(
|
||||
ls.coords,
|
||||
((-50.0, 25.0), (4.0, 1.0), (6.0, -1.0)),
|
||||
"LineString _set_single",
|
||||
)
|
||||
|
||||
# _set_list
|
||||
ls._set_list(2, ((-50.0, 25.0), (6.0, -1.0)))
|
||||
self.assertEqual(ls.coords, ((-50.0, 25.0), (6.0, -1.0)), 'LineString _set_list')
|
||||
self.assertEqual(
|
||||
ls.coords, ((-50.0, 25.0), (6.0, -1.0)), "LineString _set_list"
|
||||
)
|
||||
|
||||
lsa = LineString(ls.coords)
|
||||
for f in geos_function_tests:
|
||||
self.assertEqual(f(lsa), f(ls), 'LineString ' + f.__name__)
|
||||
self.assertEqual(f(lsa), f(ls), "LineString " + f.__name__)
|
||||
|
||||
def test05_Polygon(self):
|
||||
'Testing Polygon mutations'
|
||||
for pg in (Polygon(((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
|
||||
((5, 4), (6, 4), (6, 3), (5, 4))),
|
||||
fromstr('POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))')):
|
||||
self.assertEqual(pg._get_single_external(0),
|
||||
LinearRing((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
|
||||
'Polygon _get_single_external(0)')
|
||||
self.assertEqual(pg._get_single_external(1),
|
||||
LinearRing((5, 4), (6, 4), (6, 3), (5, 4)),
|
||||
'Polygon _get_single_external(1)')
|
||||
"Testing Polygon mutations"
|
||||
for pg in (
|
||||
Polygon(
|
||||
((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
|
||||
((5, 4), (6, 4), (6, 3), (5, 4)),
|
||||
),
|
||||
fromstr("POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))"),
|
||||
):
|
||||
self.assertEqual(
|
||||
pg._get_single_external(0),
|
||||
LinearRing((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
|
||||
"Polygon _get_single_external(0)",
|
||||
)
|
||||
self.assertEqual(
|
||||
pg._get_single_external(1),
|
||||
LinearRing((5, 4), (6, 4), (6, 3), (5, 4)),
|
||||
"Polygon _get_single_external(1)",
|
||||
)
|
||||
|
||||
# _set_list
|
||||
pg._set_list(2, (((1, 2), (10, 0), (12, 9), (-1, 15), (1, 2)), ((4, 2), (5, 2), (5, 3), (4, 2))))
|
||||
pg._set_list(
|
||||
2,
|
||||
(
|
||||
((1, 2), (10, 0), (12, 9), (-1, 15), (1, 2)),
|
||||
((4, 2), (5, 2), (5, 3), (4, 2)),
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
pg.coords,
|
||||
(((1.0, 2.0), (10.0, 0.0), (12.0, 9.0), (-1.0, 15.0), (1.0, 2.0)),
|
||||
((4.0, 2.0), (5.0, 2.0), (5.0, 3.0), (4.0, 2.0))),
|
||||
'Polygon _set_list')
|
||||
(
|
||||
((1.0, 2.0), (10.0, 0.0), (12.0, 9.0), (-1.0, 15.0), (1.0, 2.0)),
|
||||
((4.0, 2.0), (5.0, 2.0), (5.0, 3.0), (4.0, 2.0)),
|
||||
),
|
||||
"Polygon _set_list",
|
||||
)
|
||||
|
||||
lsa = Polygon(*pg.coords)
|
||||
for f in geos_function_tests:
|
||||
self.assertEqual(f(lsa), f(pg), 'Polygon ' + f.__name__)
|
||||
self.assertEqual(f(lsa), f(pg), "Polygon " + f.__name__)
|
||||
|
||||
def test06_Collection(self):
|
||||
'Testing Collection mutations'
|
||||
"Testing Collection mutations"
|
||||
points = (
|
||||
MultiPoint(*map(Point, ((3, 4), (-1, 2), (5, -4), (2, 8)))),
|
||||
fromstr('MULTIPOINT (3 4,-1 2,5 -4,2 8)'),
|
||||
fromstr("MULTIPOINT (3 4,-1 2,5 -4,2 8)"),
|
||||
)
|
||||
for mp in points:
|
||||
self.assertEqual(mp._get_single_external(2), Point(5, -4), 'Collection _get_single_external')
|
||||
self.assertEqual(
|
||||
mp._get_single_external(2),
|
||||
Point(5, -4),
|
||||
"Collection _get_single_external",
|
||||
)
|
||||
|
||||
mp._set_list(3, map(Point, ((5, 5), (3, -2), (8, 1))))
|
||||
self.assertEqual(mp.coords, ((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)), 'Collection _set_list')
|
||||
self.assertEqual(
|
||||
mp.coords, ((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)), "Collection _set_list"
|
||||
)
|
||||
|
||||
lsa = MultiPoint(*map(Point, ((5, 5), (3, -2), (8, 1))))
|
||||
for f in geos_function_tests:
|
||||
self.assertEqual(f(lsa), f(mp), 'MultiPoint ' + f.__name__)
|
||||
self.assertEqual(f(lsa), f(mp), "MultiPoint " + f.__name__)
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
import binascii
|
||||
|
||||
from django.contrib.gis.geos import (
|
||||
GEOSGeometry, Point, Polygon, WKBReader, WKBWriter, WKTReader, WKTWriter,
|
||||
GEOSGeometry,
|
||||
Point,
|
||||
Polygon,
|
||||
WKBReader,
|
||||
WKBWriter,
|
||||
WKTReader,
|
||||
WKTWriter,
|
||||
)
|
||||
from django.contrib.gis.geos.libgeos import geos_version_tuple
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
|
||||
class GEOSIOTest(SimpleTestCase):
|
||||
|
||||
def test01_wktreader(self):
|
||||
# Creating a WKTReader instance
|
||||
wkt_r = WKTReader()
|
||||
wkt = 'POINT (5 23)'
|
||||
wkt = "POINT (5 23)"
|
||||
|
||||
# read() should return a GEOSGeometry
|
||||
ref = GEOSGeometry(wkt)
|
||||
@@ -26,7 +31,7 @@ class GEOSIOTest(SimpleTestCase):
|
||||
with self.assertRaises(TypeError):
|
||||
wkt_r.read(1)
|
||||
with self.assertRaises(TypeError):
|
||||
wkt_r.read(memoryview(b'foo'))
|
||||
wkt_r.read(memoryview(b"foo"))
|
||||
|
||||
def test02_wktwriter(self):
|
||||
# Creating a WKTWriter instance, testing its ptr property.
|
||||
@@ -34,24 +39,24 @@ class GEOSIOTest(SimpleTestCase):
|
||||
with self.assertRaises(TypeError):
|
||||
wkt_w.ptr = WKTReader.ptr_type()
|
||||
|
||||
ref = GEOSGeometry('POINT (5 23)')
|
||||
ref_wkt = 'POINT (5.0000000000000000 23.0000000000000000)'
|
||||
ref = GEOSGeometry("POINT (5 23)")
|
||||
ref_wkt = "POINT (5.0000000000000000 23.0000000000000000)"
|
||||
self.assertEqual(ref_wkt, wkt_w.write(ref).decode())
|
||||
|
||||
def test_wktwriter_constructor_arguments(self):
|
||||
wkt_w = WKTWriter(dim=3, trim=True, precision=3)
|
||||
ref = GEOSGeometry('POINT (5.34562 23 1.5)')
|
||||
ref = GEOSGeometry("POINT (5.34562 23 1.5)")
|
||||
if geos_version_tuple() > (3, 10):
|
||||
ref_wkt = 'POINT Z (5.346 23 1.5)'
|
||||
ref_wkt = "POINT Z (5.346 23 1.5)"
|
||||
else:
|
||||
ref_wkt = 'POINT Z (5.35 23 1.5)'
|
||||
ref_wkt = "POINT Z (5.35 23 1.5)"
|
||||
self.assertEqual(ref_wkt, wkt_w.write(ref).decode())
|
||||
|
||||
def test03_wkbreader(self):
|
||||
# Creating a WKBReader instance
|
||||
wkb_r = WKBReader()
|
||||
|
||||
hex = b'000000000140140000000000004037000000000000'
|
||||
hex = b"000000000140140000000000004037000000000000"
|
||||
wkb = memoryview(binascii.a2b_hex(hex))
|
||||
ref = GEOSGeometry(hex)
|
||||
|
||||
@@ -72,17 +77,17 @@ class GEOSIOTest(SimpleTestCase):
|
||||
|
||||
# Representations of 'POINT (5 23)' in hex -- one normal and
|
||||
# the other with the byte order changed.
|
||||
g = GEOSGeometry('POINT (5 23)')
|
||||
hex1 = b'010100000000000000000014400000000000003740'
|
||||
g = GEOSGeometry("POINT (5 23)")
|
||||
hex1 = b"010100000000000000000014400000000000003740"
|
||||
wkb1 = memoryview(binascii.a2b_hex(hex1))
|
||||
hex2 = b'000000000140140000000000004037000000000000'
|
||||
hex2 = b"000000000140140000000000004037000000000000"
|
||||
wkb2 = memoryview(binascii.a2b_hex(hex2))
|
||||
|
||||
self.assertEqual(hex1, wkb_w.write_hex(g))
|
||||
self.assertEqual(wkb1, wkb_w.write(g))
|
||||
|
||||
# Ensuring bad byteorders are not accepted.
|
||||
for bad_byteorder in (-1, 2, 523, 'foo', None):
|
||||
for bad_byteorder in (-1, 2, 523, "foo", None):
|
||||
# Equivalent of `wkb_w.byteorder = bad_byteorder`
|
||||
with self.assertRaises(ValueError):
|
||||
wkb_w._set_byteorder(bad_byteorder)
|
||||
@@ -96,17 +101,21 @@ class GEOSIOTest(SimpleTestCase):
|
||||
wkb_w.byteorder = 1
|
||||
|
||||
# Now, trying out the 3D and SRID flags.
|
||||
g = GEOSGeometry('POINT (5 23 17)')
|
||||
g = GEOSGeometry("POINT (5 23 17)")
|
||||
g.srid = 4326
|
||||
|
||||
hex3d = b'0101000080000000000000144000000000000037400000000000003140'
|
||||
hex3d = b"0101000080000000000000144000000000000037400000000000003140"
|
||||
wkb3d = memoryview(binascii.a2b_hex(hex3d))
|
||||
hex3d_srid = b'01010000A0E6100000000000000000144000000000000037400000000000003140'
|
||||
hex3d_srid = (
|
||||
b"01010000A0E6100000000000000000144000000000000037400000000000003140"
|
||||
)
|
||||
wkb3d_srid = memoryview(binascii.a2b_hex(hex3d_srid))
|
||||
|
||||
# Ensuring bad output dimensions are not accepted
|
||||
for bad_outdim in (-1, 0, 1, 4, 423, 'foo', None):
|
||||
with self.assertRaisesMessage(ValueError, 'WKB output dimension must be 2 or 3'):
|
||||
for bad_outdim in (-1, 0, 1, 4, 423, "foo", None):
|
||||
with self.assertRaisesMessage(
|
||||
ValueError, "WKB output dimension must be 2 or 3"
|
||||
):
|
||||
wkb_w.outdim = bad_outdim
|
||||
|
||||
# Now setting the output dimensions to be 3
|
||||
@@ -123,53 +132,73 @@ class GEOSIOTest(SimpleTestCase):
|
||||
def test_wkt_writer_trim(self):
|
||||
wkt_w = WKTWriter()
|
||||
self.assertFalse(wkt_w.trim)
|
||||
self.assertEqual(wkt_w.write(Point(1, 1)), b'POINT (1.0000000000000000 1.0000000000000000)')
|
||||
self.assertEqual(
|
||||
wkt_w.write(Point(1, 1)), b"POINT (1.0000000000000000 1.0000000000000000)"
|
||||
)
|
||||
|
||||
wkt_w.trim = True
|
||||
self.assertTrue(wkt_w.trim)
|
||||
self.assertEqual(wkt_w.write(Point(1, 1)), b'POINT (1 1)')
|
||||
self.assertEqual(wkt_w.write(Point(1.1, 1)), b'POINT (1.1 1)')
|
||||
self.assertEqual(wkt_w.write(Point(1. / 3, 1)), b'POINT (0.3333333333333333 1)')
|
||||
self.assertEqual(wkt_w.write(Point(1, 1)), b"POINT (1 1)")
|
||||
self.assertEqual(wkt_w.write(Point(1.1, 1)), b"POINT (1.1 1)")
|
||||
self.assertEqual(
|
||||
wkt_w.write(Point(1.0 / 3, 1)), b"POINT (0.3333333333333333 1)"
|
||||
)
|
||||
|
||||
wkt_w.trim = False
|
||||
self.assertFalse(wkt_w.trim)
|
||||
self.assertEqual(wkt_w.write(Point(1, 1)), b'POINT (1.0000000000000000 1.0000000000000000)')
|
||||
self.assertEqual(
|
||||
wkt_w.write(Point(1, 1)), b"POINT (1.0000000000000000 1.0000000000000000)"
|
||||
)
|
||||
|
||||
def test_wkt_writer_precision(self):
|
||||
wkt_w = WKTWriter()
|
||||
self.assertIsNone(wkt_w.precision)
|
||||
self.assertEqual(wkt_w.write(Point(1. / 3, 2. / 3)), b'POINT (0.3333333333333333 0.6666666666666666)')
|
||||
self.assertEqual(
|
||||
wkt_w.write(Point(1.0 / 3, 2.0 / 3)),
|
||||
b"POINT (0.3333333333333333 0.6666666666666666)",
|
||||
)
|
||||
|
||||
wkt_w.precision = 1
|
||||
self.assertEqual(wkt_w.precision, 1)
|
||||
self.assertEqual(wkt_w.write(Point(1. / 3, 2. / 3)), b'POINT (0.3 0.7)')
|
||||
self.assertEqual(wkt_w.write(Point(1.0 / 3, 2.0 / 3)), b"POINT (0.3 0.7)")
|
||||
|
||||
wkt_w.precision = 0
|
||||
self.assertEqual(wkt_w.precision, 0)
|
||||
self.assertEqual(wkt_w.write(Point(1. / 3, 2. / 3)), b'POINT (0 1)')
|
||||
self.assertEqual(wkt_w.write(Point(1.0 / 3, 2.0 / 3)), b"POINT (0 1)")
|
||||
|
||||
wkt_w.precision = None
|
||||
self.assertIsNone(wkt_w.precision)
|
||||
self.assertEqual(wkt_w.write(Point(1. / 3, 2. / 3)), b'POINT (0.3333333333333333 0.6666666666666666)')
|
||||
self.assertEqual(
|
||||
wkt_w.write(Point(1.0 / 3, 2.0 / 3)),
|
||||
b"POINT (0.3333333333333333 0.6666666666666666)",
|
||||
)
|
||||
|
||||
with self.assertRaisesMessage(AttributeError, 'WKT output rounding precision must be '):
|
||||
wkt_w.precision = 'potato'
|
||||
with self.assertRaisesMessage(
|
||||
AttributeError, "WKT output rounding precision must be "
|
||||
):
|
||||
wkt_w.precision = "potato"
|
||||
|
||||
def test_empty_point_wkb(self):
|
||||
p = Point(srid=4326)
|
||||
wkb_w = WKBWriter()
|
||||
|
||||
wkb_w.srid = False
|
||||
with self.assertRaisesMessage(ValueError, 'Empty point is not representable in WKB.'):
|
||||
with self.assertRaisesMessage(
|
||||
ValueError, "Empty point is not representable in WKB."
|
||||
):
|
||||
wkb_w.write(p)
|
||||
with self.assertRaisesMessage(ValueError, 'Empty point is not representable in WKB.'):
|
||||
with self.assertRaisesMessage(
|
||||
ValueError, "Empty point is not representable in WKB."
|
||||
):
|
||||
wkb_w.write_hex(p)
|
||||
|
||||
wkb_w.srid = True
|
||||
for byteorder, hex in enumerate([
|
||||
b'0020000001000010E67FF80000000000007FF8000000000000',
|
||||
b'0101000020E6100000000000000000F87F000000000000F87F',
|
||||
]):
|
||||
for byteorder, hex in enumerate(
|
||||
[
|
||||
b"0020000001000010E67FF80000000000007FF8000000000000",
|
||||
b"0101000020E6100000000000000000F87F000000000000F87F",
|
||||
]
|
||||
):
|
||||
wkb_w.byteorder = byteorder
|
||||
self.assertEqual(wkb_w.write_hex(p), hex)
|
||||
self.assertEqual(GEOSGeometry(wkb_w.write_hex(p)), p)
|
||||
@@ -181,14 +210,18 @@ class GEOSIOTest(SimpleTestCase):
|
||||
p_no_srid = Polygon()
|
||||
wkb_w = WKBWriter()
|
||||
wkb_w.srid = True
|
||||
for byteorder, hexes in enumerate([
|
||||
(b'000000000300000000', b'0020000003000010E600000000'),
|
||||
(b'010300000000000000', b'0103000020E610000000000000'),
|
||||
]):
|
||||
for byteorder, hexes in enumerate(
|
||||
[
|
||||
(b"000000000300000000", b"0020000003000010E600000000"),
|
||||
(b"010300000000000000", b"0103000020E610000000000000"),
|
||||
]
|
||||
):
|
||||
wkb_w.byteorder = byteorder
|
||||
for srid, hex in enumerate(hexes):
|
||||
wkb_w.srid = srid
|
||||
self.assertEqual(wkb_w.write_hex(p), hex)
|
||||
self.assertEqual(GEOSGeometry(wkb_w.write_hex(p)), p if srid else p_no_srid)
|
||||
self.assertEqual(
|
||||
GEOSGeometry(wkb_w.write_hex(p)), p if srid else p_no_srid
|
||||
)
|
||||
self.assertEqual(wkb_w.write(p), memoryview(binascii.a2b_hex(hex)))
|
||||
self.assertEqual(GEOSGeometry(wkb_w.write(p)), p if srid else p_no_srid)
|
||||
|
||||
@@ -29,7 +29,7 @@ class UserListA(ListMixin):
|
||||
# this would work:
|
||||
# self._list = self._mytype(items)
|
||||
# but then we wouldn't be testing length parameter
|
||||
itemList = ['x'] * length
|
||||
itemList = ["x"] * length
|
||||
for i, v in enumerate(items):
|
||||
itemList[i] = v
|
||||
|
||||
@@ -59,6 +59,7 @@ class ListMixinTest(unittest.TestCase):
|
||||
Tests base class ListMixin by comparing a list clone which is
|
||||
a ListMixin subclass with a real Python list.
|
||||
"""
|
||||
|
||||
limit = 3
|
||||
listType = UserListA
|
||||
|
||||
@@ -75,57 +76,61 @@ class ListMixinTest(unittest.TestCase):
|
||||
return [*range(-1 - self.limit, 0), *range(1, 1 + self.limit)]
|
||||
|
||||
def test01_getslice(self):
|
||||
'Slice retrieval'
|
||||
"Slice retrieval"
|
||||
pl, ul = self.lists_of_len()
|
||||
for i in self.limits_plus(1):
|
||||
self.assertEqual(pl[i:], ul[i:], 'slice [%d:]' % (i))
|
||||
self.assertEqual(pl[:i], ul[:i], 'slice [:%d]' % (i))
|
||||
self.assertEqual(pl[i:], ul[i:], "slice [%d:]" % (i))
|
||||
self.assertEqual(pl[:i], ul[:i], "slice [:%d]" % (i))
|
||||
|
||||
for j in self.limits_plus(1):
|
||||
self.assertEqual(pl[i:j], ul[i:j], 'slice [%d:%d]' % (i, j))
|
||||
self.assertEqual(pl[i:j], ul[i:j], "slice [%d:%d]" % (i, j))
|
||||
for k in self.step_range():
|
||||
self.assertEqual(pl[i:j:k], ul[i:j:k], 'slice [%d:%d:%d]' % (i, j, k))
|
||||
self.assertEqual(
|
||||
pl[i:j:k], ul[i:j:k], "slice [%d:%d:%d]" % (i, j, k)
|
||||
)
|
||||
|
||||
for k in self.step_range():
|
||||
self.assertEqual(pl[i::k], ul[i::k], 'slice [%d::%d]' % (i, k))
|
||||
self.assertEqual(pl[:i:k], ul[:i:k], 'slice [:%d:%d]' % (i, k))
|
||||
self.assertEqual(pl[i::k], ul[i::k], "slice [%d::%d]" % (i, k))
|
||||
self.assertEqual(pl[:i:k], ul[:i:k], "slice [:%d:%d]" % (i, k))
|
||||
|
||||
for k in self.step_range():
|
||||
self.assertEqual(pl[::k], ul[::k], 'slice [::%d]' % (k))
|
||||
self.assertEqual(pl[::k], ul[::k], "slice [::%d]" % (k))
|
||||
|
||||
def test02_setslice(self):
|
||||
'Slice assignment'
|
||||
"Slice assignment"
|
||||
|
||||
def setfcn(x, i, j, k, L):
|
||||
x[i:j:k] = range(L)
|
||||
|
||||
pl, ul = self.lists_of_len()
|
||||
for slen in range(self.limit + 1):
|
||||
ssl = nextRange(slen)
|
||||
ul[:] = ssl
|
||||
pl[:] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [:]')
|
||||
self.assertEqual(pl, ul[:], "set slice [:]")
|
||||
|
||||
for i in self.limits_plus(1):
|
||||
ssl = nextRange(slen)
|
||||
ul[i:] = ssl
|
||||
pl[i:] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [%d:]' % (i))
|
||||
self.assertEqual(pl, ul[:], "set slice [%d:]" % (i))
|
||||
|
||||
ssl = nextRange(slen)
|
||||
ul[:i] = ssl
|
||||
pl[:i] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [:%d]' % (i))
|
||||
self.assertEqual(pl, ul[:], "set slice [:%d]" % (i))
|
||||
|
||||
for j in self.limits_plus(1):
|
||||
ssl = nextRange(slen)
|
||||
ul[i:j] = ssl
|
||||
pl[i:j] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [%d:%d]' % (i, j))
|
||||
self.assertEqual(pl, ul[:], "set slice [%d:%d]" % (i, j))
|
||||
|
||||
for k in self.step_range():
|
||||
ssl = nextRange(len(ul[i:j:k]))
|
||||
ul[i:j:k] = ssl
|
||||
pl[i:j:k] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [%d:%d:%d]' % (i, j, k))
|
||||
self.assertEqual(pl, ul[:], "set slice [%d:%d:%d]" % (i, j, k))
|
||||
|
||||
sliceLen = len(ul[i:j:k])
|
||||
with self.assertRaises(ValueError):
|
||||
@@ -138,83 +143,86 @@ class ListMixinTest(unittest.TestCase):
|
||||
ssl = nextRange(len(ul[i::k]))
|
||||
ul[i::k] = ssl
|
||||
pl[i::k] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [%d::%d]' % (i, k))
|
||||
self.assertEqual(pl, ul[:], "set slice [%d::%d]" % (i, k))
|
||||
|
||||
ssl = nextRange(len(ul[:i:k]))
|
||||
ul[:i:k] = ssl
|
||||
pl[:i:k] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [:%d:%d]' % (i, k))
|
||||
self.assertEqual(pl, ul[:], "set slice [:%d:%d]" % (i, k))
|
||||
|
||||
for k in self.step_range():
|
||||
ssl = nextRange(len(ul[::k]))
|
||||
ul[::k] = ssl
|
||||
pl[::k] = ssl
|
||||
self.assertEqual(pl, ul[:], 'set slice [::%d]' % (k))
|
||||
self.assertEqual(pl, ul[:], "set slice [::%d]" % (k))
|
||||
|
||||
def test03_delslice(self):
|
||||
'Delete slice'
|
||||
"Delete slice"
|
||||
for Len in range(self.limit):
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[:]
|
||||
del ul[:]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [:]')
|
||||
self.assertEqual(pl[:], ul[:], "del slice [:]")
|
||||
for i in range(-Len - 1, Len + 1):
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[i:]
|
||||
del ul[i:]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [%d:]' % (i))
|
||||
self.assertEqual(pl[:], ul[:], "del slice [%d:]" % (i))
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[:i]
|
||||
del ul[:i]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [:%d]' % (i))
|
||||
self.assertEqual(pl[:], ul[:], "del slice [:%d]" % (i))
|
||||
for j in range(-Len - 1, Len + 1):
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[i:j]
|
||||
del ul[i:j]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [%d:%d]' % (i, j))
|
||||
self.assertEqual(pl[:], ul[:], "del slice [%d:%d]" % (i, j))
|
||||
for k in [*range(-Len - 1, 0), *range(1, Len)]:
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[i:j:k]
|
||||
del ul[i:j:k]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [%d:%d:%d]' % (i, j, k))
|
||||
self.assertEqual(
|
||||
pl[:], ul[:], "del slice [%d:%d:%d]" % (i, j, k)
|
||||
)
|
||||
|
||||
for k in [*range(-Len - 1, 0), *range(1, Len)]:
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[:i:k]
|
||||
del ul[:i:k]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [:%d:%d]' % (i, k))
|
||||
self.assertEqual(pl[:], ul[:], "del slice [:%d:%d]" % (i, k))
|
||||
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[i::k]
|
||||
del ul[i::k]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [%d::%d]' % (i, k))
|
||||
self.assertEqual(pl[:], ul[:], "del slice [%d::%d]" % (i, k))
|
||||
|
||||
for k in [*range(-Len - 1, 0), *range(1, Len)]:
|
||||
pl, ul = self.lists_of_len(Len)
|
||||
del pl[::k]
|
||||
del ul[::k]
|
||||
self.assertEqual(pl[:], ul[:], 'del slice [::%d]' % (k))
|
||||
self.assertEqual(pl[:], ul[:], "del slice [::%d]" % (k))
|
||||
|
||||
def test04_get_set_del_single(self):
|
||||
'Get/set/delete single item'
|
||||
"Get/set/delete single item"
|
||||
pl, ul = self.lists_of_len()
|
||||
for i in self.limits_plus(0):
|
||||
self.assertEqual(pl[i], ul[i], 'get single item [%d]' % i)
|
||||
self.assertEqual(pl[i], ul[i], "get single item [%d]" % i)
|
||||
|
||||
for i in self.limits_plus(0):
|
||||
pl, ul = self.lists_of_len()
|
||||
pl[i] = 100
|
||||
ul[i] = 100
|
||||
self.assertEqual(pl[:], ul[:], 'set single item [%d]' % i)
|
||||
self.assertEqual(pl[:], ul[:], "set single item [%d]" % i)
|
||||
|
||||
for i in self.limits_plus(0):
|
||||
pl, ul = self.lists_of_len()
|
||||
del pl[i]
|
||||
del ul[i]
|
||||
self.assertEqual(pl[:], ul[:], 'del single item [%d]' % i)
|
||||
self.assertEqual(pl[:], ul[:], "del single item [%d]" % i)
|
||||
|
||||
def test05_out_of_range_exceptions(self):
|
||||
'Out of range exceptions'
|
||||
"Out of range exceptions"
|
||||
|
||||
def setfcn(x, i):
|
||||
x[i] = 20
|
||||
|
||||
@@ -223,6 +231,7 @@ class ListMixinTest(unittest.TestCase):
|
||||
|
||||
def delfcn(x, i):
|
||||
del x[i]
|
||||
|
||||
pl, ul = self.lists_of_len()
|
||||
for i in (-1 - self.limit, self.limit):
|
||||
with self.assertRaises(IndexError): # 'set index %d' % i)
|
||||
@@ -233,39 +242,40 @@ class ListMixinTest(unittest.TestCase):
|
||||
delfcn(ul, i)
|
||||
|
||||
def test06_list_methods(self):
|
||||
'List methods'
|
||||
"List methods"
|
||||
pl, ul = self.lists_of_len()
|
||||
pl.append(40)
|
||||
ul.append(40)
|
||||
self.assertEqual(pl[:], ul[:], 'append')
|
||||
self.assertEqual(pl[:], ul[:], "append")
|
||||
|
||||
pl.extend(range(50, 55))
|
||||
ul.extend(range(50, 55))
|
||||
self.assertEqual(pl[:], ul[:], 'extend')
|
||||
self.assertEqual(pl[:], ul[:], "extend")
|
||||
|
||||
pl.reverse()
|
||||
ul.reverse()
|
||||
self.assertEqual(pl[:], ul[:], 'reverse')
|
||||
self.assertEqual(pl[:], ul[:], "reverse")
|
||||
|
||||
for i in self.limits_plus(1):
|
||||
pl, ul = self.lists_of_len()
|
||||
pl.insert(i, 50)
|
||||
ul.insert(i, 50)
|
||||
self.assertEqual(pl[:], ul[:], 'insert at %d' % i)
|
||||
self.assertEqual(pl[:], ul[:], "insert at %d" % i)
|
||||
|
||||
for i in self.limits_plus(0):
|
||||
pl, ul = self.lists_of_len()
|
||||
self.assertEqual(pl.pop(i), ul.pop(i), 'popped value at %d' % i)
|
||||
self.assertEqual(pl[:], ul[:], 'after pop at %d' % i)
|
||||
self.assertEqual(pl.pop(i), ul.pop(i), "popped value at %d" % i)
|
||||
self.assertEqual(pl[:], ul[:], "after pop at %d" % i)
|
||||
|
||||
pl, ul = self.lists_of_len()
|
||||
self.assertEqual(pl.pop(), ul.pop(i), 'popped value')
|
||||
self.assertEqual(pl[:], ul[:], 'after pop')
|
||||
self.assertEqual(pl.pop(), ul.pop(i), "popped value")
|
||||
self.assertEqual(pl[:], ul[:], "after pop")
|
||||
|
||||
pl, ul = self.lists_of_len()
|
||||
|
||||
def popfcn(x, i):
|
||||
x.pop(i)
|
||||
|
||||
with self.assertRaises(IndexError):
|
||||
popfcn(ul, self.limit)
|
||||
with self.assertRaises(IndexError):
|
||||
@@ -273,29 +283,30 @@ class ListMixinTest(unittest.TestCase):
|
||||
|
||||
pl, ul = self.lists_of_len()
|
||||
for val in range(self.limit):
|
||||
self.assertEqual(pl.index(val), ul.index(val), 'index of %d' % val)
|
||||
self.assertEqual(pl.index(val), ul.index(val), "index of %d" % val)
|
||||
|
||||
for val in self.limits_plus(2):
|
||||
self.assertEqual(pl.count(val), ul.count(val), 'count %d' % val)
|
||||
self.assertEqual(pl.count(val), ul.count(val), "count %d" % val)
|
||||
|
||||
for val in range(self.limit):
|
||||
pl, ul = self.lists_of_len()
|
||||
pl.remove(val)
|
||||
ul.remove(val)
|
||||
self.assertEqual(pl[:], ul[:], 'after remove val %d' % val)
|
||||
self.assertEqual(pl[:], ul[:], "after remove val %d" % val)
|
||||
|
||||
def indexfcn(x, v):
|
||||
return x.index(v)
|
||||
|
||||
def removefcn(x, v):
|
||||
return x.remove(v)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
indexfcn(ul, 40)
|
||||
with self.assertRaises(ValueError):
|
||||
removefcn(ul, 40)
|
||||
|
||||
def test07_allowed_types(self):
|
||||
'Type-restricted list'
|
||||
"Type-restricted list"
|
||||
pl, ul = self.lists_of_len()
|
||||
ul._allowed = int
|
||||
ul[1] = 50
|
||||
@@ -303,13 +314,14 @@ class ListMixinTest(unittest.TestCase):
|
||||
|
||||
def setfcn(x, i, v):
|
||||
x[i] = v
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
setfcn(ul, 2, 'hello')
|
||||
setfcn(ul, 2, "hello")
|
||||
with self.assertRaises(TypeError):
|
||||
setfcn(ul, slice(0, 3, 2), ('hello', 'goodbye'))
|
||||
setfcn(ul, slice(0, 3, 2), ("hello", "goodbye"))
|
||||
|
||||
def test08_min_length(self):
|
||||
'Length limits'
|
||||
"Length limits"
|
||||
pl, ul = self.lists_of_len(5)
|
||||
ul._minlength = 3
|
||||
|
||||
@@ -318,12 +330,13 @@ class ListMixinTest(unittest.TestCase):
|
||||
|
||||
def setfcn(x, i):
|
||||
x[:i] = []
|
||||
|
||||
for i in range(len(ul) - ul._minlength + 1, len(ul)):
|
||||
with self.assertRaises(ValueError):
|
||||
delfcn(ul, i)
|
||||
with self.assertRaises(ValueError):
|
||||
setfcn(ul, i)
|
||||
del ul[:len(ul) - ul._minlength]
|
||||
del ul[: len(ul) - ul._minlength]
|
||||
|
||||
ul._maxlength = 4
|
||||
for i in range(0, ul._maxlength - len(ul)):
|
||||
@@ -332,99 +345,102 @@ class ListMixinTest(unittest.TestCase):
|
||||
ul.append(10)
|
||||
|
||||
def test09_iterable_check(self):
|
||||
'Error on assigning non-iterable to slice'
|
||||
"Error on assigning non-iterable to slice"
|
||||
pl, ul = self.lists_of_len(self.limit + 1)
|
||||
|
||||
def setfcn(x, i, v):
|
||||
x[i] = v
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
setfcn(ul, slice(0, 3, 2), 2)
|
||||
|
||||
def test10_checkindex(self):
|
||||
'Index check'
|
||||
"Index check"
|
||||
pl, ul = self.lists_of_len()
|
||||
for i in self.limits_plus(0):
|
||||
if i < 0:
|
||||
self.assertEqual(ul._checkindex(i), i + self.limit, '_checkindex(neg index)')
|
||||
self.assertEqual(
|
||||
ul._checkindex(i), i + self.limit, "_checkindex(neg index)"
|
||||
)
|
||||
else:
|
||||
self.assertEqual(ul._checkindex(i), i, '_checkindex(pos index)')
|
||||
self.assertEqual(ul._checkindex(i), i, "_checkindex(pos index)")
|
||||
|
||||
for i in (-self.limit - 1, self.limit):
|
||||
with self.assertRaises(IndexError):
|
||||
ul._checkindex(i)
|
||||
|
||||
def test_11_sorting(self):
|
||||
'Sorting'
|
||||
"Sorting"
|
||||
pl, ul = self.lists_of_len()
|
||||
pl.insert(0, pl.pop())
|
||||
ul.insert(0, ul.pop())
|
||||
pl.sort()
|
||||
ul.sort()
|
||||
self.assertEqual(pl[:], ul[:], 'sort')
|
||||
self.assertEqual(pl[:], ul[:], "sort")
|
||||
mid = pl[len(pl) // 2]
|
||||
pl.sort(key=lambda x: (mid - x) ** 2)
|
||||
ul.sort(key=lambda x: (mid - x) ** 2)
|
||||
self.assertEqual(pl[:], ul[:], 'sort w/ key')
|
||||
self.assertEqual(pl[:], ul[:], "sort w/ key")
|
||||
|
||||
pl.insert(0, pl.pop())
|
||||
ul.insert(0, ul.pop())
|
||||
pl.sort(reverse=True)
|
||||
ul.sort(reverse=True)
|
||||
self.assertEqual(pl[:], ul[:], 'sort w/ reverse')
|
||||
self.assertEqual(pl[:], ul[:], "sort w/ reverse")
|
||||
mid = pl[len(pl) // 2]
|
||||
pl.sort(key=lambda x: (mid - x) ** 2)
|
||||
ul.sort(key=lambda x: (mid - x) ** 2)
|
||||
self.assertEqual(pl[:], ul[:], 'sort w/ key')
|
||||
self.assertEqual(pl[:], ul[:], "sort w/ key")
|
||||
|
||||
def test_12_arithmetic(self):
|
||||
'Arithmetic'
|
||||
"Arithmetic"
|
||||
pl, ul = self.lists_of_len()
|
||||
al = list(range(10, 14))
|
||||
self.assertEqual(list(pl + al), list(ul + al), 'add')
|
||||
self.assertEqual(type(ul), type(ul + al), 'type of add result')
|
||||
self.assertEqual(list(al + pl), list(al + ul), 'radd')
|
||||
self.assertEqual(type(al), type(al + ul), 'type of radd result')
|
||||
self.assertEqual(list(pl + al), list(ul + al), "add")
|
||||
self.assertEqual(type(ul), type(ul + al), "type of add result")
|
||||
self.assertEqual(list(al + pl), list(al + ul), "radd")
|
||||
self.assertEqual(type(al), type(al + ul), "type of radd result")
|
||||
objid = id(ul)
|
||||
pl += al
|
||||
ul += al
|
||||
self.assertEqual(pl[:], ul[:], 'in-place add')
|
||||
self.assertEqual(objid, id(ul), 'in-place add id')
|
||||
self.assertEqual(pl[:], ul[:], "in-place add")
|
||||
self.assertEqual(objid, id(ul), "in-place add id")
|
||||
|
||||
for n in (-1, 0, 1, 3):
|
||||
pl, ul = self.lists_of_len()
|
||||
self.assertEqual(list(pl * n), list(ul * n), 'mul by %d' % n)
|
||||
self.assertEqual(type(ul), type(ul * n), 'type of mul by %d result' % n)
|
||||
self.assertEqual(list(n * pl), list(n * ul), 'rmul by %d' % n)
|
||||
self.assertEqual(type(ul), type(n * ul), 'type of rmul by %d result' % n)
|
||||
self.assertEqual(list(pl * n), list(ul * n), "mul by %d" % n)
|
||||
self.assertEqual(type(ul), type(ul * n), "type of mul by %d result" % n)
|
||||
self.assertEqual(list(n * pl), list(n * ul), "rmul by %d" % n)
|
||||
self.assertEqual(type(ul), type(n * ul), "type of rmul by %d result" % n)
|
||||
objid = id(ul)
|
||||
pl *= n
|
||||
ul *= n
|
||||
self.assertEqual(pl[:], ul[:], 'in-place mul by %d' % n)
|
||||
self.assertEqual(objid, id(ul), 'in-place mul by %d id' % n)
|
||||
self.assertEqual(pl[:], ul[:], "in-place mul by %d" % n)
|
||||
self.assertEqual(objid, id(ul), "in-place mul by %d id" % n)
|
||||
|
||||
pl, ul = self.lists_of_len()
|
||||
self.assertEqual(pl, ul, 'cmp for equal')
|
||||
self.assertNotEqual(ul, pl + [2], 'cmp for not equal')
|
||||
self.assertGreaterEqual(pl, ul, 'cmp for gte self')
|
||||
self.assertLessEqual(pl, ul, 'cmp for lte self')
|
||||
self.assertGreaterEqual(ul, pl, 'cmp for self gte')
|
||||
self.assertLessEqual(ul, pl, 'cmp for self lte')
|
||||
self.assertEqual(pl, ul, "cmp for equal")
|
||||
self.assertNotEqual(ul, pl + [2], "cmp for not equal")
|
||||
self.assertGreaterEqual(pl, ul, "cmp for gte self")
|
||||
self.assertLessEqual(pl, ul, "cmp for lte self")
|
||||
self.assertGreaterEqual(ul, pl, "cmp for self gte")
|
||||
self.assertLessEqual(ul, pl, "cmp for self lte")
|
||||
|
||||
self.assertGreater(pl + [5], ul, 'cmp')
|
||||
self.assertGreaterEqual(pl + [5], ul, 'cmp')
|
||||
self.assertLess(pl, ul + [2], 'cmp')
|
||||
self.assertLessEqual(pl, ul + [2], 'cmp')
|
||||
self.assertGreater(ul + [5], pl, 'cmp')
|
||||
self.assertGreaterEqual(ul + [5], pl, 'cmp')
|
||||
self.assertLess(ul, pl + [2], 'cmp')
|
||||
self.assertLessEqual(ul, pl + [2], 'cmp')
|
||||
self.assertGreater(pl + [5], ul, "cmp")
|
||||
self.assertGreaterEqual(pl + [5], ul, "cmp")
|
||||
self.assertLess(pl, ul + [2], "cmp")
|
||||
self.assertLessEqual(pl, ul + [2], "cmp")
|
||||
self.assertGreater(ul + [5], pl, "cmp")
|
||||
self.assertGreaterEqual(ul + [5], pl, "cmp")
|
||||
self.assertLess(ul, pl + [2], "cmp")
|
||||
self.assertLessEqual(ul, pl + [2], "cmp")
|
||||
|
||||
pl[1] = 20
|
||||
self.assertGreater(pl, ul, 'cmp for gt self')
|
||||
self.assertLess(ul, pl, 'cmp for self lt')
|
||||
self.assertGreater(pl, ul, "cmp for gt self")
|
||||
self.assertLess(ul, pl, "cmp for self lt")
|
||||
pl[1] = -20
|
||||
self.assertLess(pl, ul, 'cmp for lt self')
|
||||
self.assertGreater(ul, pl, 'cmp for gt self')
|
||||
self.assertLess(pl, ul, "cmp for lt self")
|
||||
self.assertGreater(ul, pl, "cmp for gt self")
|
||||
|
||||
|
||||
class ListMixinTestSingle(ListMixinTest):
|
||||
|
||||
Reference in New Issue
Block a user