Reputation: 157
Hi so I have a function here:
def cross_product(t1,t2):
new = []
#create new schema
for category in t2[0]:
t1[0].append(category)
new.append(t1[0])
#make table content
if t1 != t2:
for row in t1[1:]:
step = 0
for i in t2[1:]:
new.append(row + i)
if len(new) < 2:
return None
return new
which works as I wanted it to. However, when I test it using this py.test function, I keep getting an assertion error if there is more than one assert statement in the function. I've checked the code and there shouldn't be an assertion error at all...yet I can't seem to fix it. Anyone able to tell me what I can do to fix this? I'm still in the beginner stages of learning python so keep it simple, if you can.
heres the test case:
def test_cross_product_basic():
"""
Test cross product operation with basic/generic inputs.
"""
result_1 = [["Employee", "Department", "Department", "Head"],
["Smith", "sales", "production", "Mori"],
["Smith", "sales", "sales", "Brown"],
["Black", "production", "production", "Mori"],
["Black", "production", "sales", "Brown"],
["White", "production", "production", "Mori"],
["White", "production", "sales", "Brown"]]
assert is_equal(result_1, cross_product(R1, R2))
result_2 = [['Department', 'Head', 'Employee', 'Department'],
['production', 'Mori', 'Smith', 'sales'],
['production', 'Mori', 'Black', 'production'],
['production', 'Mori', 'White', 'production'],
['sales', 'Brown', 'Smith', 'sales'],
['sales', 'Brown', 'Black', 'production'],
['sales', 'Brown', 'White', 'production']]
assert is_equal(result_2, cross_product(R2, R1))
result_3 = [['Surname', 'FirstName', 'Age', 'Salary', 'Employee', 'Department'],
['Smith', 'Mary', 25, 2000, 'Smith', 'sales'],
['Smith', 'Mary', 25, 2000, 'Black', 'production'],
['Smith', 'Mary', 25, 2000, 'White', 'production'],
['Black', 'Lucy', 40, 3000, 'Smith', 'sales'],
['Black', 'Lucy', 40, 3000, 'Black', 'production'],
['Black', 'Lucy', 40, 3000, 'White', 'production'],
['Verdi', 'Nico', 36, 4500, 'Smith', 'sales'],
['Verdi', 'Nico', 36, 4500, 'Black', 'production'],
['Verdi', 'Nico', 36, 4500, 'White', 'production'],
['Smith', 'Mark', 40, 3900, 'Smith', 'sales'],
['Smith', 'Mark', 40, 3900, 'Black', 'production'],
['Smith', 'Mark', 40, 3900, 'White', 'production']]
assert is_equal(result_3, cross_product(EMPLOYEES, R1))
and here's the lists Im using:
R1 = [["Employee", "Department"],
["Smith", "sales"],
["Black", "production"],
["White", "production"]]
EMPLOYEES = [["Surname", "FirstName", "Age", "Salary"],
["Smith", "Mary", 25, 2000],
["Black", "Lucy", 40, 3000],
["Verdi", "Nico", 36, 4500],
["Smith", "Mark", 40, 3900]]
R2 = [["Department", "Head"],
["production", "Mori"],
["sales", "Brown"]]
Apologies for any formatting issues, I'm not so good with SO's text editor
Upvotes: 1
Views: 1073
Reputation: 10926
The cross_product function modifies its arguments (for example, t1[0].append(category)
). Therefore after the first test is complete your test lists R1 and R2 are not at their original values, having been modified by the function. Pass copies of the test lists instead.
Upvotes: 1