Skip to content

Commit

Permalink
Refactored tests, removing @fixture decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
gma committed Apr 23, 2010
1 parent 746e42d commit d6eba74
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions satisfaction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,76 @@
import satisfaction


def fixture(cls, name):
def wrapper(test):
def test_with_fixture(*args):
def stubbed_url(self, topic_id, page):
fixture = "%s-%s-page-%s.xml" % (cls.__name__.lower(), name, page)
return os.path.join(os.getcwd(), "fixtures", fixture)
class TestHelper:

try:
original_url = cls.url
cls.url = stubbed_url
return test(*args)
finally:
cls.url = original_url
return test_with_fixture
return wrapper
def stub_resource_url(self, cls, fixture):
def stubbed_url(self, topic_id, page):
fname = "%s-%s-page-%s.xml" % (cls.__name__.lower(), fixture, page)
return os.path.join(os.getcwd(), "fixtures", fname)
self.original_url = cls.url
cls.url = stubbed_url

def replace_url_stub(self, cls):
cls.url = self.original_url

class TopicTest(unittest.TestCase):

def topic(self, topic_id="409678"):
return satisfaction.Topic(topic_id)
def topic(self):
# TODO: topic id is ignored in tests - does it work?
return satisfaction.Topic("1234")


class MissingTopicTest(unittest.TestCase, TestHelper):

def test_when_topic_doesnt_exist_then_not_found(self):
with self.assertRaises(satisfaction.ResourceNotFound):
self.topic("bad-id").title
self.topic().title


class TopicWithNoRepliesTest(unittest.TestCase, TestHelper):

def setUp(self):
self.stub_resource_url(satisfaction.Topic, "without-replies")

def tearDown(self):
self.replace_url_stub(satisfaction.Topic)

@fixture(satisfaction.Topic, "without-replies")
def test_when_topic_exists_then_title_available(self):
self.assertEqual("Fantastic improvement", self.topic().title)

@fixture(satisfaction.Topic, "without-replies")
def test_when_topic_exists_then_content_available(self):
self.assertIn("Well done!", self.topic().content)

@fixture(satisfaction.Topic, "without-replies")

def test_when_topic_has_no_replies_then_no_replies_found(self):
self.assertEqual(0, self.topic().reply_count)
self.assertEqual(0, len(list(self.topic().replies)))


class TopicWithRepliesTest(unittest.TestCase, TestHelper):

def setUp(self):
self.stub_resource_url(satisfaction.Topic, "with-replies")

def tearDown(self):
self.replace_url_stub(satisfaction.Topic)

@fixture(satisfaction.Topic, "with-replies")
def test_when_topic_has_several_replies_then_replies_found(self):
self.assertEqual(3, self.topic().reply_count)
self.assertEqual(3, len(list(self.topic().replies)))

@fixture(satisfaction.Topic, "with-lots-of-replies")
def test_when_topic_has_lots_of_replies_then_replies_found(self):
def test_when_topic_has_replies_then_topic_not_included_in_replies(self):
extract_content = lambda reply: reply.content[0]['value']
reply_messages = map(extract_content, self.topic().replies)
self.assertNotIn(self.topic().content, reply_messages)


class TopicWithMultiplePagesOfRepliesTest(unittest.TestCase, TestHelper):

def setUp(self):
self.stub_resource_url(satisfaction.Topic, "with-lots-of-replies")

def tearDown(self):
self.replace_url_stub(satisfaction.Topic)

def test_when_topic_replies_span_multiple_pages_then_replies_found(self):
self.assertEqual(95, self.topic().reply_count)
self.assertEqual(95, len(list(self.topic().replies)))

Expand Down

0 comments on commit d6eba74

Please sign in to comment.