Skip to content

Instantly share code, notes, and snippets.

@rbrito
Created February 10, 2012 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbrito/1793354 to your computer and use it in GitHub Desktop.
Save rbrito/1793354 to your computer and use it in GitHub Desktop.
Unittests before changes
# -*- coding: utf-8 -*-
import sys
import unittest
sys.path.append('src')
sys.path.append('../src')
from common import *
class TestCommon(unittest.TestCase):
def testTime_Convert(self):
EXPECTED_RESULTS = [
(1000, "0:01"),
(1000, "0:01"),
(1000 * 60, "1:00"),
(1000 * 61, "1:01"),
(1000 * 70, "1:10"),
(1000 * 3599, "59:59"),
(1000 * 3600, "1:00:00"),
(1000 * 3601, "1:00:01"),
(1000 * 3660, "1:01:00"),
(1000 * 3661, "1:01:01"),
(1000 * 7200, "2:00:00"),
("bogus", "bogus"),
([], []),
]
for in_data, expected_data in EXPECTED_RESULTS:
actual_data = time_convert(in_data)
self.assertEqual(actual_data, expected_data)
def testHTML(self):
EXPECTED_RESULTS = [
("M&M", "M&M"),
("&lt;", "<"),
("&#60;", "<"),
("dynlists&copy;", "dynlists©"),
("dynlists&copy", "dynlists&copy"),
]
for in_data, expected_data in EXPECTED_RESULTS:
actual_data = htmlentitydecode(in_data)
self.assertEqual(actual_data, expected_data)
def testSafeFilename(self):
basename = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 $%`-_@{}~!#()."
good = "/home/" + basename
EXPECTED_RESULTS = [
(("/home/mydirectory/somefile", True), "somefile"),
((good, True), good[6:]),
]
for in_data, expected_data in EXPECTED_RESULTS:
actual_data = safeFilename(*in_data)
self.assertEqual(actual_data, expected_data)
def test_super_unquote(self):
EXPECTED_RESULTS = [
('. Automated Testing', '. Automated Testing'),
('.%20Automated%20Testing', '. Automated Testing'),
('.%2520Automated%2520Testing', '. Automated Testing'),
]
for in_data, expected_data in EXPECTED_RESULTS:
actual_data = super_unquote(in_data)
self.assertEqual(actual_data, expected_data)
def test_desc(self):
EXPECTED_RESULTS = [
(1, '1.0 B'),
(512, '512.0 B'),
(1023, '1023.0 B'),
(1024, '1.0 KB'),
(1025, '1.0 KB'),
(512 * 1024, '512.0 KB'),
(1023 * 1024, '1023.0 KB'),
(1024 * 1024, '1.0 MB'),
(1025 * 1024, '1.0 MB'),
(0.5 * 1024 * 1024 * 1024, '512.0 MB'),
(10000000000000000000, "(way too big)"),
]
for in_data, expected_data in EXPECTED_RESULTS:
actual_data = desc(in_data)
self.assertEqual(actual_data, expected_data)
if __name__ == "__main__":
unittest.main()
@kinow
Copy link

kinow commented Feb 11, 2012

Very nice! Do you use some kind of tool for coverage too?

@rbrito
Copy link
Author

rbrito commented Feb 11, 2012

@kinow,

No, that's not really so nice. The code is lacking way too many tests and they are not properly done. I have not yet started using a code coverage tool, but I am looking forward to use python-coverage (sometimes simply called coverage).

I am helping @programmin1 with improvements of the code of tunesviewer. I may have some more flexibility with Python than him, but he undobtedly has better understanding of Javascript than I do. If you have some experience, I would gladly accept patches that make the Javascript code pass without warnings through [JSLint][2] and the Closure compiler

@programmin1
Copy link

One problem with the code as described here is that it won't give useful fail errors. (Only prints the line in the loop with variables, not the variable values). Apparently you can add message to the assertEquals as the third parameter, which will show the message on failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment