Tools used by OpenStack Documentation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

148 lines
5.3 KiB

  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import mock
  13. import scrapy
  14. from sitemap.generator.spiders import sitemap_file
  15. import unittest
  16. class TestSitemapItem(unittest.TestCase):
  17. def test_class_type(self):
  18. self.assertTrue(type(sitemap_file.SitemapItem) is scrapy.item.ItemMeta)
  19. def test_class_supports_fields(self):
  20. with mock.patch.object(scrapy.item, 'Field'):
  21. a = sitemap_file.SitemapItem()
  22. supported_fields = ['loc', 'lastmod', 'priority', 'changefreq']
  23. for field in supported_fields:
  24. a[field] = field
  25. not_supported_fields = ['some', 'random', 'fields']
  26. for field in not_supported_fields:
  27. with self.assertRaises(KeyError):
  28. a[field] = field
  29. class TestSitemapSpider(unittest.TestCase):
  30. def setUp(self):
  31. self.spider = sitemap_file.SitemapSpider()
  32. def test_set_vars_on_init(self):
  33. domain = 'docs.openstack.org'
  34. self.assertEqual(self.spider.domain, domain)
  35. self.assertEqual(self.spider.allowed_domains, [domain])
  36. self.assertEqual(self.spider.start_urls, ['http://%s' % domain])
  37. def test_start_urls_get_appended(self):
  38. urls = 'new.openstack.org, old.openstack.org'
  39. urls_len = len(urls.split(','))
  40. spider_len = len(self.spider.start_urls)
  41. spider_with_urls = sitemap_file.SitemapSpider(urls=urls)
  42. spider_with_urls_len = len(spider_with_urls.start_urls)
  43. self.assertEqual(spider_with_urls_len, (urls_len + spider_len))
  44. def test_parse_items_inits_sitemap(self):
  45. response = mock.MagicMock()
  46. path = sitemap_file.urlparse.SplitResult(
  47. scheme='https',
  48. netloc='docs.openstack.org',
  49. path='/ocata/something.html',
  50. query='',
  51. fragment=''
  52. )
  53. with mock.patch.object(sitemap_file,
  54. 'SitemapItem') as mocked_sitemap_item:
  55. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  56. return_value=path):
  57. with mock.patch.object(sitemap_file, 'time'):
  58. self.spider.parse_item(response)
  59. self.assertTrue(mocked_sitemap_item.called)
  60. def test_parse_items_gets_path(self):
  61. response = mock.MagicMock()
  62. path = sitemap_file.urlparse.SplitResult(
  63. scheme='https',
  64. netloc='docs.openstackorg',
  65. path='/ocata/something.html',
  66. query='',
  67. fragment=''
  68. )
  69. with mock.patch.object(sitemap_file, 'SitemapItem'):
  70. with mock.patch.object(sitemap_file.urlparse,
  71. 'urlsplit',
  72. return_value=path) as mocked_urlsplit:
  73. with mock.patch.object(sitemap_file, 'time'):
  74. self.spider.parse_item(response)
  75. self.assertTrue(mocked_urlsplit.called)
  76. def test_parse_items_low_priority_weekly_freq(self):
  77. response = mock.MagicMock()
  78. path = sitemap_file.urlparse.SplitResult(
  79. scheme='https',
  80. netloc='docs.openstack.org',
  81. path='/ocata/something.html',
  82. query='',
  83. fragment=''
  84. )
  85. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  86. return_value=path):
  87. with mock.patch.object(sitemap_file, 'time'):
  88. returned_item = self.spider.parse_item(response)
  89. self.assertEqual('1.0', returned_item['priority'])
  90. self.assertEqual('weekly', returned_item['changefreq'])
  91. def test_parse_items_high_priority_daily_freq(self):
  92. response = mock.MagicMock()
  93. path = sitemap_file.urlparse.SplitResult(
  94. scheme='https',
  95. netloc='docs.openstack.org',
  96. path='/contributor-guide',
  97. query='',
  98. fragment=''
  99. )
  100. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  101. return_value=path):
  102. with mock.patch.object(sitemap_file, 'time'):
  103. returned_item = self.spider.parse_item(response)
  104. self.assertEqual('1.0', returned_item['priority'])
  105. self.assertEqual('daily', returned_item['changefreq'])
  106. def test_parse_returns_populated_item(self):
  107. response = mock.MagicMock()
  108. path = sitemap_file.urlparse.SplitResult(
  109. scheme='https',
  110. netloc='docs.openstack.org',
  111. path='/ocata',
  112. query='',
  113. fragment=''
  114. )
  115. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  116. return_value=path):
  117. with mock.patch.object(sitemap_file, 'time'):
  118. returned_item = self.spider.parse_item(response)
  119. self.assertEqual(4, len(returned_item))
  120. if __name__ == '__main__':
  121. unittest.main()