Home:ALL Converter>csv file not being read correctly

csv file not being read correctly

Ask Time:2022-11-08T09:01:00         Author:Burvil

Json Formatter

I have the following python code that's not passing my test. For some reason, the csv reader is splitting the filename given to the object and thinking that's the content of the file. This is completely different from the list for which I'm writing the contents to the csv file.

The code I'm trying to test:

#!env python3

import pytest

from file import CSVFile


@pytest.fixture
def csv_file():
    csv_file = CSVFile("test")
    yield csv_file
    csv_file.delete()


# Test if file is writable and correct data written
def test_CSVLineWritable(csv_file):
    row = ["a", "b", "c"]
    data = []
    data.append(row)
    csv_file.write_line(row, "w")
    data_read = csv_file.read_file()
    assert csv_file.file_exists() is True
    assert data_read == data

The test code:

#!env python3

import csv
import os


class CSVFile:
    def __init__(self, filename):
        self.filename = filename

    def write_line(self, row, mode):
        for value in row:
            if value == "None":
                value = ""
        writer = csv.writer(open(self.filename, mode))
        print("Write confirm - " + str(row))
        writer.writerow(row)

    def read_file(self):
        data = []
        with open(self.filename, mode="r") as csv_reader:
            csv_reader = csv.reader(self.filename, delimiter=",")
            for row in csv_reader:
                print("Read confirm - " + str(row))
                data.append(row)
            return data

    def file_exists(self):
        if os.path.exists(self.filename):
            return True
        else:
            return False

    def delete(self):
        if os.path.exists(self.filename):
            os.remove(self.filename)
            return True
        else:
            return False

The pytest results:

➜  aws git:(91f044f) ✗ pytest --cov=file tests          
=========================================================================================== test session starts ============================================================================================
platform darwin -- Python 3.10.8, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/bchang/Documents/git-files/cds-devops/scripts/aws
plugins: freezegun-0.4.2, cov-4.0.0
collected 1 item                                                                                                                                                                                           

tests/test_file.py F                                                                                                                                                                                 [100%]

================================================================================================= FAILURES =================================================================================================
___________________________________________________________________________________________ test_CSVLineWritable ___________________________________________________________________________________________

csv_file = <file.CSVFile object at 0x107b3d7e0>

    def test_CSVLineWritable(csv_file):
        row = ["a", "b", "c"]
        data = []
        data.append(row)
        csv_file.write_line(row, "w")
        data_read = csv_file.read_file()
        assert csv_file.file_exists() is True
>       assert data_read == data
E       AssertionError: assert [['t'], ['e'], ['s'], ['t']] == [['a', 'b', 'c']]
E         At index 0 diff: ['t'] != ['a', 'b', 'c']
E         Left contains 3 more items, first extra item: ['e']
E         Use -v to get more diff

tests/test_file.py:23: AssertionError
------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------
Write confirm - ['a', 'b', 'c']
Read confirm - ['t']
Read confirm - ['e']
Read confirm - ['s']
Read confirm - ['t']
============================================================================================= warnings summary =============================================================================================
../../../../../../../usr/local/lib/python3.10/site-packages/pytest_freezegun.py:17
../../../../../../../usr/local/lib/python3.10/site-packages/pytest_freezegun.py:17
  /usr/local/lib/python3.10/site-packages/pytest_freezegun.py:17: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
    if LooseVersion(pytest.__version__) < LooseVersion('3.6.0'):

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

---------- coverage: platform darwin, python 3.10.8-final-0 ----------
Name      Stmts   Miss  Cover
-----------------------------
file.py      29      3    90%
-----------------------------
TOTAL        29      3    90%

========================================================================================= short test summary info ==========================================================================================
FAILED tests/test_file.py::test_CSVLineWritable - AssertionError: assert [['t'], ['e'], ['s'], ['t']] == [['a', 'b', 'c']]

Note that I look at the contents of the file after removing the delete statement after the yield and re-running. I get see the expected results (a, b, c):

➜  aws git:(91f044f) ✗ file test
test: ASCII text, with CRLF line terminators
➜  aws git:(91f044f) ✗ cat test
a,b,c

So, it seems to me that the read_file method is returning the letters of 'test', as opposed to a, b and c, which I fed in to be written. Any thoughts as to why?

UPDATE: I'm putting the updated code here, as I don't see how to put it in a comment as an answer to my own question. The fixed code looks like this:


 The fixed methods look like:

    def read_file(self):
        data = []
        with open(self.filename, mode="r") as csvfile:
            csv_data = csv.reader(csvfile, delimiter=",")
            for row in csv_data:
                print("Read confirm - " + str(row))
                data.append(row)
            return data

    def file_exists(self):
        return os.path.exists(self.filename)

Author:Burvil,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74354606/csv-file-not-being-read-correctly
yy