Skip to content

Commit

Permalink
fixed converter utility; refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burnash committed Dec 14, 2016
1 parent d94acc9 commit 4ad90b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
3 changes: 3 additions & 0 deletions gspread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def rowcol_to_a1(row, col):
column_label = chr(mod + MAGIC_NUMBER) + column_label

label = '%s%s' % (column_label, row)

return label


Expand Down Expand Up @@ -167,6 +168,8 @@ def a1_to_rowcol(label):
else:
raise IncorrectCellLabel(label)

return (row, col)


def extract_id_from_url(url):
m2 = URL_KEY_V2_RE.search(url)
Expand Down
6 changes: 6 additions & 0 deletions tests/mock_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Avoiding direct network access is benefitial in that it markedly speeds up
testing, avoids error-prone credential setup, and enables validation even if
internet access is unavailable.
"""

from datetime import datetime
import unittest
try:
Expand All @@ -18,6 +20,10 @@
from tests import test_utils


class MockUtilsTest(test.UtilsTest):
pass


class MockGspreadTest(unittest.TestCase):
"""This is the base class for all tests not accessing the API.
Expand Down
28 changes: 14 additions & 14 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ def test_no_extract_id_from_url(self):
'http://example.org'
)

def test_a1_to_rowcol(self):
self.assertEqual(utils.a1_to_rowcol('ABC3'), (3, 731))

def test_rowcol_to_a1(self):
self.assertEqual(utils.rowcol_to_a1(3, 731), 'ABC3')
self.assertEqual(utils.rowcol_to_a1(1, 104), 'CZ1')

def test_addr_converters(self):
for row in range(1, 257):
for col in range(1, 512):
addr = utils.rowcol_to_a1(row, col)
(r, c) = utils.a1_to_rowcol(addr)
self.assertEqual((row, col), (r, c))


class GspreadTest(unittest.TestCase):

Expand Down Expand Up @@ -187,26 +201,12 @@ def setUp(self):
def tearDown(self):
self.spreadsheet.del_worksheet(self.sheet)

def test_get_int_addr(self):
self.assertEqual(self.sheet.get_int_addr('ABC3'), (3, 731))

def test_get_addr_int(self):
self.assertEqual(self.sheet.get_addr_int(3, 731), 'ABC3')
self.assertEqual(self.sheet.get_addr_int(1, 104), 'CZ1')

def test_get_updated(self):
RFC_3339 = (r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?'
r'(Z|[+-]\d{2}:\d{2})')
has_match = re.match(RFC_3339, self.sheet.updated) is not None
self.assertTrue(has_match)

def test_addr_converters(self):
for row in range(1, 257):
for col in range(1, 512):
addr = self.sheet.get_addr_int(row, col)
(r, c) = self.sheet.get_int_addr(addr)
self.assertEqual((row, col), (r, c))

def test_acell(self):
cell = self.sheet.acell('A1')
self.assertTrue(isinstance(cell, gspread.Cell))
Expand Down

0 comments on commit 4ad90b4

Please sign in to comment.