Skip to content

Commit

Permalink
refactored the test suite to make it easier to write simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Malmgren committed Apr 14, 2015
1 parent 5932121 commit b43a5dd
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 104 deletions.
27 changes: 27 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import inspect

import scrubadub


Expand All @@ -7,3 +9,28 @@ class BaseTestCase(object):

def clean(self, text):
return scrubadub.clean_with_placeholders(text)

def compare_before_after(self, docstring=None):
"""This is a convenience method to make it easy to write tests without
copy-pasting a lot of code. This method checks to make sure the BEFORE:
text in the calling method's docstring matches the AFTER: text in the
calling method's docstring.
"""
# get the before and after outcomes from the docstring of the method
# that calls compare_before_after
if docstring is None:
stack = inspect.stack()
calling_function_name = stack[1][3]
docstring = getattr(self, calling_function_name).__doc__
before, after = docstring.split("BEFORE:")[1].split("AFTER:")
before = unicode(before.strip())
after = unicode(after.strip())

# run a test to make sure the before string is the same as the after
# string
result = self.clean(before)
self.assertEqual(
result,
after,
'\nEXPECTED:\n"%s"\n\nBUT GOT THIS:\n"%s"'%(after, result),
)
84 changes: 36 additions & 48 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,25 @@
class CredentialsTestCase(unittest.TestCase, BaseTestCase):

def test_root_root_combo(self):
"""username/password often split across adjacent lines"""
result = self.clean(u'username: root\npassword: root\n\n')
self.assertEqual(
result,
u'username: {{USERNAME}}\npassword: {{PASSWORD}}\n\n',
'root/root combo not working: "%s"' % result,
)
"""
BEFORE: username: root\npassword: root\n\n
AFTER: username: {{USERNAME}}\npassword: {{PASSWORD}}\n\n
"""
self.compare_before_after()

def test_whitespaceless(self):
"""sometimes there's no whitespace"""
result = self.clean(u'username:root\npassword:crickets')
self.assertEqual(
result,
u'username:{{USERNAME}}\npassword:{{PASSWORD}}',
'whitepace errors "%s"' % result,
)
"""
BEFORE: username:root\npassword:crickets
AFTER: username:{{USERNAME}}\npassword:{{PASSWORD}}
"""
self.compare_before_after()

def test_colonless(self):
"""sometimes there is no colon"""
result = self.clean(u'username root\npassword crickets')
self.assertEqual(
result,
u'username {{USERNAME}}\npassword {{PASSWORD}}',
'colonless errors "%s"' % result,
)
"""
BEFORE: username root\npassword crickets
AFTER: username {{USERNAME}}\npassword {{PASSWORD}}
"""
self.compare_before_after()

def test_email_username(self):
"""sometimes there is no colon"""
Expand All @@ -39,36 +33,30 @@ def test_email_username(self):
self.assertNotIn("moi", result, 'password remains "%s"' % result)

def test_alternate_keywords(self):
"""login/pw credential keywords"""
result = self.clean(u'login snoop pw biggreenhat')
self.assertEqual(
result,
u'login {{USERNAME}} pw {{PASSWORD}}',
'alternate keyword errors "%s"' % result,
)
"""
BEFORE: login snoop pw biggreenhat
AFTER: login {{USERNAME}} pw {{PASSWORD}}
"""
self.compare_before_after()

def test_singleletter_keywords(self):
"""u/p credential keywords"""
result = self.clean(u'u: snoop\np: biggreenhat')
self.assertEqual(
result,
u'u: {{USERNAME}}\np: {{PASSWORD}}',
'single letter keyword errors "%s"' % result,
)
"""
BEFORE: u: snoop\np: biggreenhat
AFTER: u: {{USERNAME}}\np: {{PASSWORD}}
"""
self.compare_before_after()

def test_singleletter_keyword_exceptions(self):
"""u/p credential keywords exceptions"""
result = self.clean(u'This is your problem')
self.assertEqual(
result,
u'This is your problem',
'adjascent letters should not be scrubbed "%s"' % result,
)
"""Make sure that the single letter keywords do not make mistakes
BEFORE: This is your problem
AFTER: This is your problem
"""
self.compare_before_after()

def test_camelcase_keywords(self):
result = self.clean(u'UserName snoop PassWord biggreenhat')
self.assertEqual(
result,
u'UserName {{USERNAME}} PassWord {{PASSWORD}}',
'camel case errors "%s"' % result,
)
"""
BEFORE: UserName snoop PassWord biggreenhat
AFTER: UserName {{USERNAME}} PassWord {{PASSWORD}}
"""
self.compare_before_after()
22 changes: 10 additions & 12 deletions tests/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
class EmailTestCase(unittest.TestCase, BaseTestCase):

def test_gmail_john(self):
"""Make sure email addresses are removed from text"""
self.assertEqual(
self.clean(u'My email is john@gmail.com'),
u'My email is {{EMAIL}}',
'john@gmail.com is not replaced with {{EMAIL}}',
)
"""
BEFORE: My email is john@gmail.com
AFTER: My email is {{EMAIL}}
"""
self.compare_before_after()

def test_fancy_gmail_john(self):
"""Make sure email addresses are removed from text"""
self.assertEqual(
self.clean(u'My email is john at gmail.com'),
u'My email is {{EMAIL}}',
'john at gmail.com is not replaced with {{EMAIL}}',
)
"""
BEFORE: My email is john at gmail.com
AFTER: My email is {{EMAIL}}
"""
self.compare_before_after()
11 changes: 5 additions & 6 deletions tests/test_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
class NameTestCase(unittest.TestCase, BaseTestCase):

def test_john(self):
"""Make sure proper names are removed from the text"""
self.assertEqual(
self.clean(u'John is a cat'),
u'{{NAME}} is a cat',
'John not replaced with {{NAME}}',
)
"""
BEFORE: John is a cat
AFTER: {{NAME}} is a cat
"""
self.compare_before_after()
20 changes: 12 additions & 8 deletions tests/test_phone_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@

class PhoneNumberTestCase(unittest.TestCase, BaseTestCase):

def _test_phone_numbers(self, *phone_numbers):
def create_docstring(self, phone_number):
return """
BEFORE: My phone number is %s
AFTER: My phone number is {{PHONE}}
""" % phone_number

def check_phone_numbers(self, *phone_numbers):
for phone_number in phone_numbers:
self.assertEqual(
self.clean(u'My phone number is %s' % phone_number),
u'My phone number is {{PHONE}}',
'missing phone number "%s"' % phone_number,
self.compare_before_after(
docstring=self.create_docstring(phone_number),
)

def test_american_phone_number(self):
"""test american-style phone numbers"""
self._test_phone_numbers(
self.check_phone_numbers(
'1-312-515-2239',
'+1-312-515-2239',
'1 (312) 515-2239',
Expand All @@ -26,15 +30,15 @@ def test_american_phone_number(self):

def test_extension_phone_numbers(self):
"""test phone numbers with extensions"""
self._test_phone_numbers(
self.check_phone_numbers(
'312-515-2239 x12',
'312-515-2239 ext. 12',
'312-515-2239 ext.12',
)

def test_international_phone_numbers(self):
"""test international phone numbers"""
self._test_phone_numbers(
self.check_phone_numbers(
'+47 21 30 85 99',
'+45 69 19 88 56',
'+46 852 503 499',
Expand Down
56 changes: 26 additions & 30 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,40 @@
class UrlTestCase(unittest.TestCase, BaseTestCase):

def test_http(self):
"""http:// should be replaced"""
self.assertEqual(
self.clean(u'http://bit.ly/aser is neat'),
u'{{URL}} is neat',
'http url is not replaced with {{URL}}',
)
"""
BEFORE: http://bit.ly/aser is neat
AFTER: {{URL}} is neat
"""
self.compare_before_after()

def test_https(self):
"""https:// should be replaced"""
self.assertEqual(
self.clean(u'https://bit.ly/aser is neat'),
u'{{URL}} is neat',
'http url is not replaced with {{URL}}',
)
"""
BEFORE: https://bit.ly/aser is neat
AFTER: {{URL}} is neat
"""
self.compare_before_after()

def test_www(self):
"""www. should be replaced"""
self.assertEqual(
self.clean(u'www.bit.ly/aser is neat'),
u'{{URL}} is neat',
'http url is not replaced with {{URL}}',
)
"""
BEFORE: www.bit.ly/aser is neat
AFTER: {{URL}} is neat
"""
self.compare_before_after()


def test_long_url(self):
"""long url with query string and hash"""
self.assertEqual(
self.clean(u'https://this.is/a/long?url=very#url is good'),
u'{{URL}} is good',
"long urls aren't working properly"
)
"""
BEFORE: https://this.is/a/long?url=very#url is good
AFTER: {{URL}} is good
"""
self.compare_before_after()

def test_two_urls(self):
"""does this work with two URLs"""
self.assertEqual(
self.clean(u'http://bit.ly/number-one http://www.google.com/two'),
u'{{URL}} {{URL}}',
"multiple URLs aren't working properly",
)
"""
BEFORE: http://bit.ly/number-one http://www.google.com/two
AFTER: {{URL}} {{URL}}
"""
self.compare_before_after()

def test_keep_domain(self):
"""keep_domain test with non-empty path"""
Expand Down

0 comments on commit b43a5dd

Please sign in to comment.