forked from DGNum/gestioCOF
Expand CSVResponseMixin functionality
This commit is contained in:
parent
9b0440429c
commit
6fff995ccd
3 changed files with 49 additions and 38 deletions
|
@ -58,17 +58,33 @@ class CSVResponseMixin:
|
|||
par la première ligne du CSV.
|
||||
"""
|
||||
|
||||
def load_from_csv_response(self, r, as_dict=False, **reader_kwargs):
|
||||
def _load_from_csv_response(self, r, as_dict=False, **reader_kwargs):
|
||||
content = r.content.decode("utf-8")
|
||||
|
||||
# la dernière ligne du fichier CSV est toujours vide
|
||||
content = content.split("\n")[:-1]
|
||||
if as_dict:
|
||||
reader_class = csv.DictReader
|
||||
content = csv.DictReader(content, **reader_kwargs)
|
||||
# en python3.7, content est une liste d'OrderedDicts
|
||||
return list(map(dict, content))
|
||||
else:
|
||||
reader_class = csv.reader
|
||||
content = csv.reader(content, **reader_kwargs)
|
||||
return list(content)
|
||||
|
||||
return list(reader_class(content, **reader_kwargs))
|
||||
def assertCSVEqual(self, response, expected):
|
||||
if type(expected[0]) == list:
|
||||
as_dict = False
|
||||
elif type(expected[0]) == dict:
|
||||
as_dict = True
|
||||
else:
|
||||
raise AssertionError(
|
||||
"Unsupported type in `assertCSVEqual`: "
|
||||
"%(expected)s is not of type `list` nor `dict` !"
|
||||
% {"expected": str(expected[0])}
|
||||
)
|
||||
|
||||
content = self._load_from_csv_response(response, as_dict=as_dict)
|
||||
self.assertCountEqual(content, expected)
|
||||
|
||||
|
||||
class ICalMixin:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue