PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test.py

https://gitlab.com/cornelius/cornelius
Python | 149 lines | 107 code | 38 blank | 4 comment | 1 complexity | 40d78035d3d5295fca65f5e8c9f18e2a MD5 | raw file
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import unittest
  5. import urllib2
  6. sys.path.append('./')
  7. from cornelius import *
  8. class TestsCornelius(unittest.TestCase):
  9. """Main parts"""
  10. def test_pretty_size_a(self):
  11. """Configuration file"""
  12. size = pretty_size(2048)
  13. self.assertEqual(size, '2.00K', 'Pretty size : %s' % (size))
  14. def test_pretty_size_b(self):
  15. size = pretty_size(2049)
  16. self.assertEqual(size, '2.00K', 'Pretty size : %s' % (size))
  17. def test_pretty_size_c(self):
  18. size = pretty_size(1024*1024)
  19. self.assertEqual(size, '1.00M', 'Pretty size : %s' % (size))
  20. def test_pretty_size_d(self):
  21. size = pretty_size(256)
  22. self.assertEqual(size, '256b', 'Pretty size : %s' % (size))
  23. def test_sort_urls_a(self):
  24. foobar = sort_urls(['foo.gif', 'foo.css', 'foo.js', 'foo.jpeg'])
  25. self.assertEqual(foobar[0], 'foo.gif', 'Sort urls')
  26. self.assertEqual(foobar[1], 'foo.jpeg', 'Sort urls')
  27. self.assertEqual(foobar[2], 'foo.js', 'Sort urls')
  28. self.assertEqual(foobar[3], 'foo.css', 'Sort urls')
  29. def test_completeurl_a(self):
  30. foobar = completeurl('http://www.foo.eu', 'http://www.foo.eu')
  31. self.assertEqual(foobar, 'http://www.foo.eu', 'Complete_url')
  32. def test_completeurl_b(self):
  33. foobar = completeurl('/img/foo.png', 'http://www.foo.eu')
  34. self.assertEqual(foobar, 'http://www.foo.eu/img/foo.png', 'Complete_url')
  35. def test_completeurl_c(self):
  36. foobar = completeurl('img/foo.png', 'http://www.foo.eu')
  37. self.assertEqual(foobar, 'http://www.foo.eu/img/foo.png', 'Complete_url : ' + foobar)
  38. def test_completeurl_d(self):
  39. foobar = completeurl('img', 'http://www.foo.eu')
  40. self.assertEqual(foobar, 'http://www.foo.eu/img', 'Complete_url')
  41. def test_completeurl_e(self):
  42. foobar = completeurl('http://www.foobar.eu/', 'http://www.foo.eu')
  43. self.assertEqual(foobar, 'http://www.foobar.eu/', 'Complete_url')
  44. def test_completeurl_f(self):
  45. foobar = completeurl('https://www.foobar.eu/', 'https://www.foo.eu')
  46. self.assertEqual(foobar, 'https://www.foobar.eu/', 'Complete_url')
  47. def test_completeurl_g(self):
  48. foobar = completeurl('img/foo.png', 'https://www.foo.eu')
  49. self.assertEqual(foobar, 'https://www.foo.eu/img/foo.png', 'Complete_url : ' + foobar)
  50. def test_parse_a(self):
  51. foobar = parse('<html><body></body></html>')
  52. self.assertEqual(foobar, [[],[],[]], 'Parse empty')
  53. def test_parse_b(self):
  54. foobar = parse('<html><body><img src="/foo.png"></body></html>')
  55. self.assertEqual(foobar, [['/foo.png'],[],[]], 'Parse : ')
  56. def test_parse_c(self):
  57. foobar = parse('<html><body><img src="/foo.png"><img src="foobar.jpeg"></body></html>')
  58. self.assertEqual(foobar, [['/foo.png','foobar.jpeg'],[],[]], 'Parse : ')
  59. def test_parse_d(self):
  60. foobar = parse('<html><body><img src="bar.png"><script src="foo.js"></body></html>')
  61. self.assertEqual(foobar, [['bar.png'],['foo.js'],[]], 'Parse : ')
  62. def test_parse_e(self):
  63. foobar = parse('<html><body><img src="bar.png"><script></script></body></html>')
  64. self.assertEqual(foobar, [['bar.png'],[],[]], 'Parse : ')
  65. def test_parse_f(self):
  66. foobar = parse('<html><body><img src="bar.png"><link href="bar.css" rel="stylesheet"></link></body></html>')
  67. self.assertEqual(foobar, [['bar.png'],[],['bar.css']], 'Parse : ')
  68. def test_parse_g(self):
  69. foobar = parse('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><body><p><img src="bar.png"></p><link rel="stylesheet" href="bar.css"></link></body></html>')
  70. self.assertEqual(foobar, [['bar.png'],[],['bar.css']], 'Parse : ')
  71. def test_parse_h(self):
  72. foobar = parse('<html><head><style type="text/css" media="screen">@import url(/themes/style.css);</style></head><body><img src="bar.png"></body></html>')
  73. self.assertEqual(foobar, [['bar.png'],[],['/themes/style.css']], 'Parse styles returns : ' + str(foobar))
  74. def test_parse_i(self):
  75. foobar = parse('<html><body><p><img src="bar.png"></p><link href="bar.css"></link></body></html>')
  76. self.assertEqual(foobar, [['bar.png'],[],[]], 'Parse : ')
  77. def test_css_import_a(self):
  78. foobar = css_import('@import url(/themes/style.css);')
  79. self.assertEqual(foobar, '/themes/style.css', 'css_import returns : ' + str(foobar))
  80. def test_hosts_stats_a(self):
  81. foobar = hosts_stats(['http://foo.fr/bar','http://foo.fr/foo', 'http://bar.eu/foo'])
  82. self.assertEqual(foobar, [('http://foo.fr', 2),('http://bar.eu', 1)], 'hosts_stats')
  83. def test_hosts_stats_a(self):
  84. foobar = hosts_stats(['http://bar.eu/foo', 'http://foo.fr/bar','http://foo.fr/foo'])
  85. self.assertEqual(foobar, [('http://foo.fr', 2),('http://bar.eu', 1)], 'hosts_stats')
  86. def test_is_image_a(self):
  87. self.assertEqual(True, is_image('http://foo.fr/foo.png'), 'hosts_stats')
  88. def test_is_image_b(self):
  89. self.assertEqual(True, is_image('http://foo.fr/foo.jpg'), 'hosts_stats')
  90. def test_is_image_c(self):
  91. self.assertEqual(True, is_image('http://foo.fr/foo.gif'), 'hosts_stats')
  92. def test_is_image_d(self):
  93. self.assertEqual(False, is_image('http://foo.fr/foopng'), 'hosts_stats')
  94. def test_is_style_a(self):
  95. self.assertEqual(True, is_style('http://foo.fr/foo.css'), 'hosts_stats')
  96. def test_is_style_b(self):
  97. self.assertEqual(False, is_style('http://foo.fr/foocss'), 'hosts_stats')
  98. def test_is_script_a(self):
  99. self.assertEqual(True, is_script('http://foo.fr/foo.js'), 'hosts_stats')
  100. def test_is_script_b(self):
  101. self.assertEqual(False, is_script('http://foo.fr/foocss'), 'hosts_stats')
  102. def test_stats_for_host_a(self):
  103. urls = ['http://foo.fr/foo.css', 'http://foo.fr/bar.css',
  104. 'http://foo.fr/foo.png', 'http://bar.fr/bar.css' ]
  105. self.assertEqual((1,0,2), stats_for_host('http://foo.fr', urls), 'hosts_stats')
  106. def test_stats_for_host_b(self):
  107. urls = ['http://foo.fr/foo.css', 'http://foo.fr/bar.css',
  108. 'http://foo.fr/foo.png', 'http://bar.fr/bar.css' ]
  109. self.assertEqual((0,0,0), stats_for_host('http://foobar.fr', urls), 'hosts_stats')
  110. if __name__ == '__main__':
  111. unittest.main()