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.
 
 
 
 

111 lines
4.0 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. from sitemap.generator.spiders import sitemap_file
  14. import unittest
  15. class TestSitemapSpider(unittest.TestCase):
  16. def setUp(self):
  17. self.spider = sitemap_file.SitemapSpider()
  18. def test_set_vars_on_init(self):
  19. domain = 'docs.openstack.org'
  20. self.assertEqual(self.spider.domain, domain)
  21. self.assertEqual(self.spider.allowed_domains, [domain])
  22. self.assertEqual(self.spider.start_urls, ['http://%s' % domain])
  23. def test_start_urls_get_appended(self):
  24. urls = 'new.openstack.org, old.openstack.org'
  25. urls_len = len(urls.split(','))
  26. spider_len = len(self.spider.start_urls)
  27. spider_with_urls = sitemap_file.SitemapSpider(urls=urls)
  28. spider_with_urls_len = len(spider_with_urls.start_urls)
  29. self.assertEqual(spider_with_urls_len, (urls_len + spider_len))
  30. def test_parse_items_inits_sitemap(self):
  31. response = mock.MagicMock()
  32. with mock.patch.object(sitemap_file.items,
  33. 'SitemapItem') as mocked_sitemap_item:
  34. with mock.patch.object(sitemap_file, 'time'):
  35. self.spider.parse_item(response)
  36. self.assertTrue(mocked_sitemap_item.called)
  37. def test_parse_items_gets_path(self):
  38. response = mock.MagicMock()
  39. with mock.patch.object(sitemap_file.items, 'SitemapItem'):
  40. with mock.patch.object(sitemap_file.urlparse,
  41. 'urlsplit') as mocked_urlsplit:
  42. with mock.patch.object(sitemap_file, 'time'):
  43. self.spider.parse_item(response)
  44. self.assertTrue(mocked_urlsplit.called)
  45. def test_parse_items_low_priority_weekly_freq(self):
  46. response = mock.MagicMock()
  47. path = sitemap_file.urlparse.SplitResult(
  48. scheme='https',
  49. netloc='docs.openstack.com',
  50. path='/kilo',
  51. query='',
  52. fragment=''
  53. )
  54. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  55. return_value=path):
  56. with mock.patch.object(sitemap_file, 'time'):
  57. returned_item = self.spider.parse_item(response)
  58. self.assertEqual('0.5', returned_item['priority'])
  59. self.assertEqual('weekly', returned_item['changefreq'])
  60. def test_parse_items_high_priority_daily_freq(self):
  61. response = mock.MagicMock()
  62. path = sitemap_file.urlparse.SplitResult(
  63. scheme='https',
  64. netloc='docs.openstack.com',
  65. path='/mitaka',
  66. query='',
  67. fragment=''
  68. )
  69. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  70. return_value=path):
  71. with mock.patch.object(sitemap_file, 'time'):
  72. returned_item = self.spider.parse_item(response)
  73. self.assertEqual('1.0', returned_item['priority'])
  74. self.assertEqual('daily', returned_item['changefreq'])
  75. def test_parse_returns_populated_item(self):
  76. response = mock.MagicMock()
  77. path = sitemap_file.urlparse.SplitResult(
  78. scheme='https',
  79. netloc='docs.openstack.com',
  80. path='/mitaka',
  81. query='',
  82. fragment=''
  83. )
  84. with mock.patch.object(sitemap_file.urlparse, 'urlsplit',
  85. return_value=path):
  86. with mock.patch.object(sitemap_file, 'time'):
  87. returned_item = self.spider.parse_item(response)
  88. self.assertEqual(4, len(returned_item))
  89. if __name__ == '__main__':
  90. unittest.main()