0

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?

Burvil
  • 65
  • 1
  • 5

1 Answers1

0

I fixed the problem, and with that, some bugs. I realized the problem was with writing the file, i.e. the file getting corrupted data at that point. So I focused on that function.

    def write_csv_line(self, row, mode):
        i = 0
        for value in row:
            print(row)
            if value == "None" or value is None:
                row[i] = ""
                print("changed - " + str(row))
            i = i + 1
        with open(self.filename, "w", newline="") as csvfile:
            writer = csv.writer(csvfile, delimiter=",")
            writer.writerow(row)

I also updated the test cases to be correct:

@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

Burvil
  • 65
  • 1
  • 5