Skip to content

Commit

Permalink
flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
georgewhewell committed Jan 18, 2015
1 parent 5317f51 commit 6649be7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion sheets/templatetags/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
logger = logging.getLogger(__name__)
register = template.Library()

gdocs_format = 'https://docs.google.com/spreadsheets/d/{key}/export?format=csv&id={key}'
gdocs_format = \
'https://docs.google.com/spreadsheets/d/{key}/export?format=csv&id={key}'


def get_sheet(key):
Expand All @@ -18,10 +19,12 @@ def get_sheet(key):
except requests.exceptions.RequestException as error:
logger.error("Error fetching url: %s" % error)


def read_csv(csv_content):
reader = csv.reader(csv_content.text)
return [row for row in reader]


@register.assignment_tag(name='csv')
def csv_tag(key):
if not key:
Expand Down
34 changes: 22 additions & 12 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
from django import template
from django.test import SimpleTestCase


sample_key = '1bJNR7SLqpzWJNvstNcFR4gtS-M7Bmn0D1X2lGTJPvGM'
sample_template = '{% load sheets %}{% csv key as csvrows %}{% for row in csvrows %}{% for cell in row %}{{ cell }}{% endfor %}{% endfor %}'
sample_response = os.path.join(os.path.dirname(__file__), 'sample_response.csv')
sample_output = os.path.join(os.path.dirname(__file__), 'sample_output.txt')
sample_template = (
"{% load sheets %}{% csv key as csvrows %}"
"{% for row in csvrows %}{% for cell in row %}"
"{{ cell }}{% endfor %}{% endfor %}")

gdocs_format = 'https://docs.google.com/spreadsheets/d/{key}/export?format=csv&id={key}'
sample_response = os.path.join(
os.path.dirname(__file__), 'sample_response.csv')
sample_output = os.path.join(
os.path.dirname(__file__), 'sample_output.txt')

gdocs_format = \
'https://docs.google.com/spreadsheets/d/{key}/export?format=csv&id={key}'


class TestSheets(SimpleTestCase):
Expand All @@ -39,20 +45,24 @@ def test_404(self):
"""
Failing to fetch sheet should return empty list
"""
responses.add(responses.GET, gdocs_format.format(key='test'),
body='404 Not Found', content_type='html/text', status=404,
match_querystring=True)
responses.add(
responses.GET, gdocs_format.format(key='test'),
body='404 Not Found', content_type='html/text', status=404,
match_querystring=True)
t = template.Template(sample_template)
self.assertEqual(t.render(template.Context({'key': 'test'})), '')
self.assertEqual(1, len(responses.calls))

@responses.activate
def test_sample(self):
responses.add(responses.GET, gdocs_format.format(key=sample_key),
body=open(sample_response, 'rt', encoding='utf-8').read(),
match_querystring=True, status=200)
responses.add(
responses.GET, gdocs_format.format(key=sample_key),
body=open(sample_response, 'rt', encoding='utf-8').read(),
match_querystring=True, status=200)
t = template.Template(sample_template)
output = t.render(template.Context({'key': sample_key}))
self.assertEqual(responses.calls[0].request.url, gdocs_format.format(key=sample_key))
self.assertEqual(
responses.calls[0].request.url,
gdocs_format.format(key=sample_key))
self.assertEqual(1, len(responses.calls))
self.assertEqual(output, open(sample_output).read())

0 comments on commit 6649be7

Please sign in to comment.