I am writing a simple test for a function that should take as input an iterable and produce a list of all possible partitions of the input:
def partitions(collection):
""" Iterator over partitions of collection
https://stackoverflow.com/a/30134039/4841832
"""
if len(collection) == 1:
yield [ collection ]
return
first = collection[0]
for smaller in partitions(collection[1:]):
# insert `first` in each of the subpartition's subsets
for n, subset in enumerate(smaller):
yield smaller[:n] + [[ first ] + subset] + smaller[n+1:]
# put `first` in its own subset
yield [ [ first ] ] + smaller
I tried using assertCountEquals for my test, but this does not work - it only checks for surface level similarity of the containers, instead of the 2 level similarity I need for my function.
def test_partitions(self):
s = list(partitions([1,2,3]))
self.assertCountEqual(s, [
[[1,2,3]],
[[1], [2,3]],
[[2], [1,3]],
[[3], [1,2]],
[[1], [2], [3]]
])
======================================================================
FAIL: test_partitions (explainbn.test.test_utilities.TestIterationUtilities)
----------------------------------------------------------------------
Traceback (most recent call last):
File "...\test\test_utilities.py", line 124, in test_partitions
self.assertCountEqual(s, [
AssertionError: Element counts were not equal:
First has 1, Second has 0: [[1, 2], [3]]
First has 0, Second has 1: [[3], [1, 2]]
----------------------------------------------------------------------
What is the best way to write the test to achieve what I want? I don't care about the order of the elements or the type of the containers.