I want to create a unit test for a function which sorts a list of objects according to some object attribute(s). Here is a code sample:
class Foo:
"""Custom data type Foo."""
def __init__(self,
a: str,
b: int,
c: int,
d: int,
e: int,
f: int):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = c * d * e
self.h = c * d
This is a part of the unit test function which produces the error:
from random import randint
...
class Test(TestCase):
def test_item_sort_foo(self):
random_list = [Foo(str(randint(0, 1000)),
randint(0, 100),
randint(0, 100),
randint(0, 100),
randint(0, 100),
randint(0, 100))
for x in range(100)]
...
The error message is:
TypeError: 'Foo' object is not subscriptable.
Despite reading this question, I cannot understand why Python cares if Foo
is subscriptable since random_list
already is. I'm trying to generate a list of random Foo
items similarly to the answer here. I am puzzled because I already have a (working) class of the kind
from dataclasses import dataclass
from typing import List
@dataclass
class Bar:
"""Bar custom data type."""
i: str
j: List[Foo]
And I can type, e.g.,
f = Foo(...)
b = Bar("baz", [])
b.j.append(f)
So here are my questions:
- Do elements of subscriptable objects also have to be subscriptable? Why?
- What is the best way to generate random data with the properties from above for testing?
- How do I apply this principle for my case?
EDIT: This is the complete unit testing function:
def test_item_sort_foo(self):
random_list = [Foo(str(randint(0, 1000)),
randint(0, 100),
randint(0, 100),
randint(0, 100),
randint(0, 100),
randint(0, 100))
for x in range(100)]
sorted_list = item_sort_foos(random_list)
assert eq(sorted_list, sorted(random_list,
key=itemgetter(4, 7),
reverse=True))
And here is the code of the function which implements the logic:
def item_sort_foos(foos: List[Foo]) -> List[Foo]
return sorted(foos,
key=attrgetter("e", "h"),
reverse=True)
EDIT2: The full trace is:
Error
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
yield
File "/opt/anaconda3/lib/python3.7/unittest/case.py", line 628, in run
testMethod()
File ".../src/project/tests/test_orderings.py", line 89, in test_item_sort_foos
reverse=True))
TypeError: 'Foo' object is not subscriptable
If you need any additional info, please request in the comments.