test_parse.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from datetime import datetime
  2. import os
  3. import unittest
  4. from mock import call, MagicMock, patch
  5. from yarg import newest_packages, latest_updated_packages, HTTPError
  6. from yarg.parse import _get, Package
  7. class GoodNewestResponse(object):
  8. status_code = 200
  9. xml = os.path.join(os.path.dirname(__file__),
  10. 'newest.xml')
  11. content = open(xml).read()
  12. class GoodUpdatedResponse(object):
  13. status_code = 200
  14. xml = os.path.join(os.path.dirname(__file__),
  15. 'updated.xml')
  16. content = open(xml).read()
  17. class BadResponse(object):
  18. status_code = 300
  19. reason = "Mocked"
  20. class TestParse(unittest.TestCase):
  21. def setUp(self):
  22. self.newest = self.setup_newest()
  23. self.updated = self.setup_updated()
  24. def setup_newest(self):
  25. item1 = {'name': 'gobble',
  26. 'url': 'http://pypi.python.org/pypi/gobble',
  27. 'description': 'Automatic functional testing for CLI apps.',
  28. 'date': '09 Aug 2014 06:57:42 GMT'}
  29. item2 = {'name': 'flask_autorest',
  30. 'url': 'http://pypi.python.org/pypi/flask_autorest',
  31. 'description': 'auto create restful apis for database, with the help of dataset.',
  32. 'date': '09 Aug 2014 05:24:58 GMT'}
  33. item3 = {'name': 'ranrod',
  34. 'url': 'http://pypi.python.org/pypi/ranrod',
  35. 'description': 'download route53 hosted zones as local json files',
  36. 'date': '09 Aug 2014 05:20:21 GMT'}
  37. return [Package(item1), Package(item2), Package(item3)]
  38. def setup_updated(self):
  39. item1 = {'name': 'pycoin',
  40. 'version': '0.50',
  41. 'url': 'http://pypi.python.org/pypi/pycoin/0.50',
  42. 'description': 'Utilities for Bitcoin and altcoin addresses and transaction manipulation.',
  43. 'date': '09 Aug 2014 08:40:20 GMT'}
  44. item2 = {'name': 'django-signup',
  45. 'version': '0.6.0',
  46. 'url': 'http://pypi.python.org/pypi/django-signup/0.6.0',
  47. 'description': 'A user registration app for Django with support for custom user models',
  48. 'date': '09 Aug 2014 08:33:53 GMT'}
  49. item3 = {'name': 'pyADC',
  50. 'version': '0.1.3',
  51. 'url': 'http://pypi.python.org/pypi/pyADC/0.1.3',
  52. 'description': 'Python implementation of the ADC(S) Protocol for Direct Connect.',
  53. 'date': '09 Aug 2014 08:19:56 GMT'}
  54. return [Package(item1), Package(item2), Package(item3)]
  55. @patch('requests.get', return_value=BadResponse)
  56. def test_newest_packages_bad_get(self, get_mock):
  57. # Python 2.6....
  58. try:
  59. newest_packages()
  60. except HTTPError as e:
  61. self.assertEqual(300, e.status_code)
  62. self.assertEqual(e.status_code, e.errno)
  63. self.assertEqual(e.reason, e.message)
  64. @patch('requests.get', return_value=BadResponse)
  65. def test_updated_packages_bad_get(self, get_mock):
  66. # Python 2.6....
  67. try:
  68. latest_updated_packages()
  69. except HTTPError as e:
  70. self.assertEqual(300, e.status_code)
  71. self.assertEqual(e.status_code, e.errno)
  72. self.assertEqual(e.reason, e.message)
  73. @patch('requests.get', return_value=GoodNewestResponse)
  74. def test_newest_packages(self, get_mock):
  75. p = newest_packages()
  76. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=packages_rss'),
  77. get_mock.call_args)
  78. self.assertEqual(self.newest[0].name, p[0].name)
  79. self.assertEqual(self.newest[1].name, p[1].name)
  80. self.assertEqual(self.newest[2].name, p[2].name)
  81. @patch('requests.get', return_value=GoodNewestResponse)
  82. def test_newest_package(self, get_mock):
  83. p = newest_packages()
  84. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=packages_rss'),
  85. get_mock.call_args)
  86. self.assertEqual('gobble', p[0].name)
  87. self.assertEqual('http://pypi.python.org/pypi/gobble', p[0].url)
  88. self.assertEqual('Automatic functional testing for CLI apps.',
  89. p[0].description)
  90. self.assertEqual(datetime.strptime('09 Aug 2014 06:57:42 GMT',
  91. "%d %b %Y %H:%M:%S %Z"),
  92. p[0].date)
  93. @patch('requests.get', return_value=GoodNewestResponse)
  94. def test_newest_package_repr(self, get_mock):
  95. p = newest_packages()
  96. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=packages_rss'),
  97. get_mock.call_args)
  98. self.assertEqual('<Package gobble>', p[0].__repr__())
  99. @patch('requests.get', return_value=GoodNewestResponse)
  100. def test_newest_package_version(self, get_mock):
  101. p = newest_packages()
  102. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=packages_rss'),
  103. get_mock.call_args)
  104. self.assertEqual(None, p[0].version)
  105. @patch('requests.get', return_value=GoodUpdatedResponse)
  106. def test_updated_packages(self, get_mock):
  107. p = latest_updated_packages()
  108. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=rss'),
  109. get_mock.call_args)
  110. self.assertEqual(self.updated[0].name, p[0].name)
  111. self.assertEqual(self.updated[0].version, p[0].version)
  112. self.assertEqual(self.updated[1].name, p[1].name)
  113. self.assertEqual(self.updated[1].version, p[1].version)
  114. self.assertEqual(self.updated[2].name, p[2].name)
  115. self.assertEqual(self.updated[2].version, p[2].version)
  116. @patch('requests.get', return_value=GoodUpdatedResponse)
  117. def test_updated_package(self, get_mock):
  118. p = latest_updated_packages()
  119. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=rss'),
  120. get_mock.call_args)
  121. self.assertEqual('pycoin', p[0].name)
  122. self.assertEqual('0.50', p[0].version)
  123. self.assertEqual('http://pypi.python.org/pypi/pycoin/0.50', p[0].url)
  124. self.assertEqual('Utilities for Bitcoin and altcoin addresses and transaction manipulation.',
  125. p[0].description)
  126. self.assertEqual(datetime.strptime('09 Aug 2014 08:40:20 GMT',
  127. "%d %b %Y %H:%M:%S %Z"),
  128. p[0].date)
  129. @patch('requests.get', return_value=GoodUpdatedResponse)
  130. def test_updated_package_repr(self, get_mock):
  131. p = latest_updated_packages()
  132. self.assertEqual(call('https://pypi.python.org/pypi?%3Aaction=rss'),
  133. get_mock.call_args)
  134. self.assertEqual('<Package pycoin>', p[0].__repr__())