I have the following python code:
class CSVFile:
def __init__(self, filename):
self.filename = filename
def write_csv_line(self, row, mode):
for value in row:
if value == "None":
value = ""
writer = csv.writer(open(self.filename, mode))
writer.writerow(row)
def read_file(self):
data = []
with open(self.filename, mode="r") as csvfile:
csv_data = csv.reader(csvfile, delimiter=",")
for row in csv_data:
data.append(row)
return data
def file_exists(self):
return os.path.exists(self.filename)
def delete(self):
if os.path.exists(self.filename):
os.remove(self.filename)
return True
else:
return False
I'm trying to parametrize testing for the code, as follows:
@pytest.fixture(scope="function")
def csv_file():
csv_file = CSVFile("test")
yield csv_file
csv_file.delete()
# Test if file is writable and correct data written
# @pytest.mark.skip("WIP")
@pytest.mark.parametrize(
"file, row, expected",
[
(lazy_fixture("csv_file"), [["a", "b", "c"]], [["a", "b", "c"]]),
],
)
def test_CSVLineWritable(file, row, expected):
file.write_csv_line(row, "w")
data_read = file.read_file()
assert file.file_exists() is True
assert data_read == expected
When I run pytest, I get:
file = <process_resources.CSVFile object at 0x108a64af0>, row = [['a', 'b', 'c']], expected = [['a', 'b', 'c']]
@pytest.mark.parametrize(
"file, row, expected",
[
(lazy_fixture("csv_file"), [["a", "b", "c"]], [["a", "b", "c"]]),
# (lazy_fixture("csv_file"), [["None", "b", "c"]], [["", "b", "c"]]),
# (lazy_fixture("csv_file"), [[None, "b", "c"]], [["", "b", "c"]]),
],
)
def test_CSVLineWritable(file, row, expected):
file.write_csv_line(row, "w")
data_read = file.read_file()
assert file.file_exists() is True
> assert data_read == expected
E assert [["['a', 'b', 'c']"]] == [['a', 'b', 'c']]
E At index 0 diff: ["['a', 'b', 'c']"] != ['a', 'b', 'c']
E Full diff:
E - [['a', 'b', 'c']]
E + [["['a', 'b', 'c']"]]
E ? ++ + +
tests/test_process_resources.py:117: AssertionError
Specifically, it seems the data is getting corrupted, as what's read in isn't the same thing as what's written. In other words, the contents of data_read isn't the same as expected. I've reviewed the read_file method, and it seems fine to me. Any thoughts on why it's not working?