webforms_aggregator_tests.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import os
  6. import tempfile
  7. import unittest
  8. import webforms_aggregator
  9. class WebformsAggregatorTest(unittest.TestCase):
  10. """Unit tests for the webforms_aggregator module."""
  11. def setUp(self):
  12. self.cookie_file = 'test.cookie'
  13. self.url1 = 'http://www.google.com'
  14. self.url2 = 'http://www.macys.com'
  15. self.domain = 'google.com'
  16. self.url_file = tempfile.NamedTemporaryFile(suffix='.txt', delete=False)
  17. self.url_file.file.write(
  18. 'URLs to crawl:\n%s\n%s\n' % (self.url1, self.url2))
  19. self.url_file.close()
  20. def tearDown(self):
  21. if os.path.isfile(self.cookie_file):
  22. os.unlink(self.cookie_file)
  23. if os.path.isfile(self.url_file.name):
  24. self.url_file.close()
  25. os.unlink(self.url_file.name)
  26. def testRetrieverDownloadsPage(self):
  27. """Verify the retriever can download a page."""
  28. r = webforms_aggregator.Retriever(self.url1, self.domain, self.cookie_file)
  29. self.assertTrue(r.Download(),
  30. msg='Retriever could not download "%s"' % self.url1)
  31. def testCrawlerFindsRegPageFromUrl(self):
  32. """Verify that the crawler is able to find a reg page from the given URL."""
  33. c = webforms_aggregator.Crawler(self.url2)
  34. self.assertTrue(
  35. c.Run(), msg='Crawler could not find the reg page of "%s"' % self.url2)
  36. def testThreadedCrawlerFindsRegPageFromUrlsFile(self):
  37. """Verify the threaded crawler finds reg page from a file of URLs."""
  38. c = webforms_aggregator.ThreadedCrawler(self.url_file.name)
  39. self.assertNotEqual(
  40. c.Run(), -1,
  41. msg='Threaded crawler could not find the reg page from the URLs file')
  42. if __name__ == '__main__':
  43. suite = unittest.TestLoader().loadTestsFromTestCase(
  44. WebformsAggregatorTest)
  45. unittest.TextTestRunner(verbosity=2).run(suite)