Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
Add tests to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fanqiuwen committed Aug 5, 2015
1 parent b979ee6 commit e3e5d3c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
77 changes: 67 additions & 10 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from six.moves.urllib.parse import parse_qs, urlparse

import strongarm
from strongarm.common import PaginatedResourceList
from strongarm.common import Struct, PaginatedResourceList


class StrongarmTestCase(unittest.TestCase):
Expand All @@ -25,6 +25,27 @@ def test_unauthorized(self):
strongarm.Domain.all()


class StructTestCase(unittest.TestCase):

def test_recursive_traversal(self):
"""
Test that the struct faithfully represent the structure of the given
dictionary.
"""
d = {'a': 1,
'b': {'c': 2, 'd': 3},
'e': {'f': {'g': ('test', 42)}},
'h': 'value'}

struct = Struct(d)
self.assertEqual(struct.a, 1)
self.assertEqual(struct.b.c, 2)
self.assertEqual(struct.b.d, 3)
self.assertEqual(struct.e.f.g, ('test', 42))
self.assertEqual(struct.h, 'value')


class PaginationTestCase(unittest.TestCase):

endpoint = 'http://example.com/integers'
Expand Down Expand Up @@ -61,6 +82,15 @@ def paginated_resource(request):
callback=paginated_resource,
content_type='application/json')

def lazy_pages(self, index):
"""
How many pages the lazy list should have requested to get to index.
"""
if index < 0:
index += self.total
return index / self.per_page + 1

@responses.activate
def test_init_lazy(self):
"""
Expand All @@ -81,19 +111,33 @@ def test_index_lazy(self):
self.plist = PaginatedResourceList(int, self.endpoint)

self.assertEqual(self.plist[2], 2)
self.assertEqual(len(responses.calls), 1)
self.assertEqual(len(responses.calls), self.lazy_pages(2))

self.assertEqual(self.plist[5], 5)
self.assertEqual(len(responses.calls), 2)
self.assertEqual(len(responses.calls), self.lazy_pages(5))

self.assertEqual(self.plist[10], 10)
self.assertEqual(len(responses.calls), 3)
self.assertEqual(len(responses.calls), self.lazy_pages(10))

self.assertEqual(self.plist[13], 13)
self.assertEqual(len(responses.calls), 4)
self.assertEqual(len(responses.calls), self.lazy_pages(13))

@responses.activate
def test_index_negative(self):
"""
Test that when negative indices are handled correctly and lazily.
"""
self.plist = PaginatedResourceList(int, self.endpoint)

self.assertEqual(self.plist[-12], self.total - 12)
self.assertEqual(len(responses.calls), self.lazy_pages(-12))

self.assertEqual(self.plist[-1], self.total - 1)
self.assertEqual(len(responses.calls), self.lazy_pages(-1))

@responses.activate
def test_index_error(self):
def test_index_out_of_bounds(self):
"""
Test that when an out-of-bounds index is passed in, an error is thrown
without requesting additional pages.
Expand All @@ -104,6 +148,19 @@ def test_index_error(self):
self.assertRaises(IndexError, lambda: self.plist[self.total])
self.assertEqual(len(responses.calls), 1)

@responses.activate
def test_index_type_error(self):
"""
Test that when an incorrectly typed index is passed in, an error is
thrown without requesting additional pages.
"""
self.plist = PaginatedResourceList(int, self.endpoint)

self.assertRaises(TypeError, lambda: self.plist['a'])
self.assertEqual(len(responses.calls), 1)


@responses.activate
def test_slice_lazy(self):
"""
Expand All @@ -114,13 +171,13 @@ def test_slice_lazy(self):
self.plist = PaginatedResourceList(int, self.endpoint)

self.assertEqual(self.plist[1:3], list(range(1, 3)))
self.assertEqual(len(responses.calls), 1)
self.assertEqual(len(responses.calls), self.lazy_pages(2))

self.assertEqual(self.plist[1:7:2], list(range(1, 7, 2)))
self.assertEqual(len(responses.calls), 2)
self.assertEqual(len(responses.calls), self.lazy_pages(6))

self.assertEqual(self.plist[10:13], list(range(10, 13)))
self.assertEqual(len(responses.calls), 4)
self.assertEqual(len(responses.calls), self.lazy_pages(12))

@responses.activate
def test_list_cast(self):
Expand All @@ -133,4 +190,4 @@ def test_list_cast(self):

entire_list = list(self.plist)
self.assertEqual(entire_list, list(range(self.total)))
self.assertEqual(len(responses.calls), 4)
self.assertEqual(len(responses.calls), self.lazy_pages(self.total-1))
5 changes: 4 additions & 1 deletion tests/test_dns_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
class StrongarmDnsTestCase(unittest.TestCase):

def test_unimplemented(self):
"""Test that running the base class directly raises NotImplemented."""
"""Test that running the base classes directly raises NotImplemented."""

with self.assertRaises(NotImplementedError):
DnsBlackholeUpdater('127.0.0.1').update_domains(['example.com'])

with self.assertRaises(NotImplementedError):
DnsBlackholeIncrementalUpdater('127.0.0.1').update_domains(['example.com'])

def test_incremental_update(self):
"""
Test that the incremental updater calculates set differences correctly.
Expand Down

0 comments on commit e3e5d3c

Please sign in to comment.