mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 19:03:30 -05:00
Merge pull request #4086 from alphagov/small-spreadsheet-refactor-177535141
Small refactoring to the Spreadsheet class
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import csv
|
||||
from io import BytesIO, StringIO
|
||||
from io import StringIO
|
||||
from os import path
|
||||
|
||||
import pyexcel
|
||||
import pyexcel_xlsx
|
||||
|
||||
|
||||
class Spreadsheet():
|
||||
@@ -87,19 +86,3 @@ class Spreadsheet():
|
||||
form.file.data,
|
||||
filename=form.file.data.filename,
|
||||
)
|
||||
|
||||
@property
|
||||
def as_rows(self):
|
||||
if not self._rows:
|
||||
self._rows = list(csv.reader(
|
||||
StringIO(self._csv_data),
|
||||
quoting=csv.QUOTE_MINIMAL,
|
||||
skipinitialspace=True,
|
||||
))
|
||||
return self._rows
|
||||
|
||||
@property
|
||||
def as_excel_file(self):
|
||||
io = BytesIO()
|
||||
pyexcel_xlsx.save_data(io, {'Sheet 1': self.as_rows})
|
||||
return io.getvalue()
|
||||
|
||||
42
tests/app/models/test_spreadsheet.py
Normal file
42
tests/app/models/test_spreadsheet.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from collections import OrderedDict
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models.spreadsheet import Spreadsheet
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_large_excel_file():
|
||||
with open(str(Path.cwd() / 'tests' / 'spreadsheet_files' / 'excel 2007.xlsx'), 'rb') as xl:
|
||||
ret = Spreadsheet.from_file(xl, filename='xl.xlsx')
|
||||
assert ret.as_csv_data
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_dict():
|
||||
assert Spreadsheet.from_dict(OrderedDict(
|
||||
foo='bar',
|
||||
name='Jane',
|
||||
)).as_csv_data == (
|
||||
"foo,name\r\n"
|
||||
"bar,Jane\r\n"
|
||||
)
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_dict_with_filename():
|
||||
assert Spreadsheet.from_dict({}, filename='empty.csv').as_dict['file_name'] == "empty.csv"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('args, kwargs', (
|
||||
(
|
||||
('hello', ['hello']),
|
||||
{},
|
||||
),
|
||||
(
|
||||
(),
|
||||
{'csv_data': 'hello', 'rows': ['hello']}
|
||||
),
|
||||
))
|
||||
def test_spreadsheet_checks_for_bad_arguments(args, kwargs):
|
||||
with pytest.raises(TypeError) as exception:
|
||||
Spreadsheet(*args, **kwargs)
|
||||
assert str(exception.value) == 'Spreadsheet must be created from either rows or CSV data'
|
||||
@@ -1,15 +1,10 @@
|
||||
from collections import OrderedDict, namedtuple
|
||||
from collections import namedtuple
|
||||
from csv import DictReader
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.utils.csv import (
|
||||
Spreadsheet,
|
||||
generate_notifications_csv,
|
||||
get_errors_for_csv,
|
||||
)
|
||||
from app.utils.csv import generate_notifications_csv, get_errors_for_csv
|
||||
from tests.conftest import fake_uuid
|
||||
|
||||
|
||||
@@ -79,42 +74,6 @@ def _get_notifications_csv_mock(
|
||||
)
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_large_excel_file():
|
||||
with open(str(Path.cwd() / 'tests' / 'spreadsheet_files' / 'excel 2007.xlsx'), 'rb') as xl:
|
||||
ret = Spreadsheet.from_file(xl, filename='xl.xlsx')
|
||||
assert ret.as_csv_data
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_dict():
|
||||
assert Spreadsheet.from_dict(OrderedDict(
|
||||
foo='bar',
|
||||
name='Jane',
|
||||
)).as_csv_data == (
|
||||
"foo,name\r\n"
|
||||
"bar,Jane\r\n"
|
||||
)
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_dict_with_filename():
|
||||
assert Spreadsheet.from_dict({}, filename='empty.csv').as_dict['file_name'] == "empty.csv"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('args, kwargs', (
|
||||
(
|
||||
('hello', ['hello']),
|
||||
{},
|
||||
),
|
||||
(
|
||||
(),
|
||||
{'csv_data': 'hello', 'rows': ['hello']}
|
||||
),
|
||||
))
|
||||
def test_spreadsheet_checks_for_bad_arguments(args, kwargs):
|
||||
with pytest.raises(TypeError) as exception:
|
||||
Spreadsheet(*args, **kwargs)
|
||||
assert str(exception.value) == 'Spreadsheet must be created from either rows or CSV data'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('created_by_name, expected_content', [
|
||||
(
|
||||
None, [
|
||||
|
||||
Reference in New Issue
Block a user