PageRenderTime 104ms CodeModel.GetById 51ms app.highlight 18ms RepoModel.GetById 33ms 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
  4import sys
  5import unittest
  6import urllib2
  7sys.path.append('./')
  8
  9from cornelius import *
 10
 11class TestsCornelius(unittest.TestCase):
 12    """Main parts"""
 13    def test_pretty_size_a(self):
 14        """Configuration file"""
 15        size = pretty_size(2048)
 16        self.assertEqual(size, '2.00K', 'Pretty size : %s' % (size))
 17
 18    def test_pretty_size_b(self):
 19        size = pretty_size(2049)
 20        self.assertEqual(size, '2.00K', 'Pretty size : %s' % (size))
 21
 22    def test_pretty_size_c(self):
 23        size = pretty_size(1024*1024)
 24        self.assertEqual(size, '1.00M', 'Pretty size : %s' % (size))
 25
 26    def test_pretty_size_d(self):
 27        size = pretty_size(256)
 28        self.assertEqual(size, '256b', 'Pretty size : %s' % (size))
 29
 30    def test_sort_urls_a(self):
 31        foobar = sort_urls(['foo.gif', 'foo.css', 'foo.js', 'foo.jpeg'])
 32        self.assertEqual(foobar[0], 'foo.gif', 'Sort urls')
 33        self.assertEqual(foobar[1], 'foo.jpeg', 'Sort urls')
 34        self.assertEqual(foobar[2], 'foo.js', 'Sort urls')
 35        self.assertEqual(foobar[3], 'foo.css', 'Sort urls')
 36
 37    def test_completeurl_a(self):
 38        foobar = completeurl('http://www.foo.eu', 'http://www.foo.eu')
 39        self.assertEqual(foobar, 'http://www.foo.eu', 'Complete_url')
 40
 41    def test_completeurl_b(self):
 42        foobar = completeurl('/img/foo.png', 'http://www.foo.eu')
 43        self.assertEqual(foobar, 'http://www.foo.eu/img/foo.png', 'Complete_url')
 44
 45    def test_completeurl_c(self):
 46        foobar = completeurl('img/foo.png', 'http://www.foo.eu')
 47        self.assertEqual(foobar, 'http://www.foo.eu/img/foo.png', 'Complete_url : ' + foobar)
 48
 49    def test_completeurl_d(self):
 50        foobar = completeurl('img', 'http://www.foo.eu')
 51        self.assertEqual(foobar, 'http://www.foo.eu/img', 'Complete_url')
 52
 53    def test_completeurl_e(self):
 54        foobar = completeurl('http://www.foobar.eu/', 'http://www.foo.eu')
 55        self.assertEqual(foobar, 'http://www.foobar.eu/', 'Complete_url')
 56
 57    def test_completeurl_f(self):
 58        foobar = completeurl('https://www.foobar.eu/', 'https://www.foo.eu')
 59        self.assertEqual(foobar, 'https://www.foobar.eu/', 'Complete_url')
 60
 61    def test_completeurl_g(self):
 62        foobar = completeurl('img/foo.png', 'https://www.foo.eu')
 63        self.assertEqual(foobar, 'https://www.foo.eu/img/foo.png', 'Complete_url : ' + foobar)
 64
 65    def test_parse_a(self):
 66        foobar = parse('<html><body></body></html>')
 67        self.assertEqual(foobar, [[],[],[]], 'Parse empty')
 68
 69    def test_parse_b(self):
 70        foobar = parse('<html><body><img src="/foo.png"></body></html>')
 71        self.assertEqual(foobar, [['/foo.png'],[],[]], 'Parse : ')
 72
 73    def test_parse_c(self):
 74        foobar = parse('<html><body><img src="/foo.png"><img src="foobar.jpeg"></body></html>')
 75        self.assertEqual(foobar, [['/foo.png','foobar.jpeg'],[],[]], 'Parse : ')
 76
 77    def test_parse_d(self):
 78        foobar = parse('<html><body><img src="bar.png"><script src="foo.js"></body></html>')
 79        self.assertEqual(foobar, [['bar.png'],['foo.js'],[]], 'Parse : ')
 80
 81    def test_parse_e(self):
 82        foobar = parse('<html><body><img src="bar.png"><script></script></body></html>')
 83        self.assertEqual(foobar, [['bar.png'],[],[]], 'Parse : ')
 84
 85    def test_parse_f(self):
 86        foobar = parse('<html><body><img src="bar.png"><link href="bar.css" rel="stylesheet"></link></body></html>')
 87        self.assertEqual(foobar, [['bar.png'],[],['bar.css']], 'Parse : ')
 88
 89    def test_parse_g(self):
 90        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>')
 91        self.assertEqual(foobar, [['bar.png'],[],['bar.css']], 'Parse : ')
 92
 93    def test_parse_h(self):
 94        foobar = parse('<html><head><style type="text/css" media="screen">@import url(/themes/style.css);</style></head><body><img src="bar.png"></body></html>')   
 95        self.assertEqual(foobar, [['bar.png'],[],['/themes/style.css']], 'Parse styles returns : ' + str(foobar))
 96
 97    def test_parse_i(self):
 98        foobar = parse('<html><body><p><img src="bar.png"></p><link href="bar.css"></link></body></html>')
 99        self.assertEqual(foobar, [['bar.png'],[],[]], 'Parse : ')
100
101    def test_css_import_a(self):
102        foobar = css_import('@import url(/themes/style.css);')   
103        self.assertEqual(foobar, '/themes/style.css', 'css_import returns : ' + str(foobar))
104
105    def test_hosts_stats_a(self):
106        foobar = hosts_stats(['http://foo.fr/bar','http://foo.fr/foo', 'http://bar.eu/foo'])
107        self.assertEqual(foobar, [('http://foo.fr', 2),('http://bar.eu', 1)], 'hosts_stats')
108
109    def test_hosts_stats_a(self):
110        foobar = hosts_stats(['http://bar.eu/foo', 'http://foo.fr/bar','http://foo.fr/foo'])
111        self.assertEqual(foobar, [('http://foo.fr', 2),('http://bar.eu', 1)], 'hosts_stats')
112
113    def test_is_image_a(self):
114        self.assertEqual(True, is_image('http://foo.fr/foo.png'), 'hosts_stats')
115
116    def test_is_image_b(self):
117        self.assertEqual(True, is_image('http://foo.fr/foo.jpg'), 'hosts_stats')
118
119    def test_is_image_c(self):
120        self.assertEqual(True, is_image('http://foo.fr/foo.gif'), 'hosts_stats')
121
122    def test_is_image_d(self):
123        self.assertEqual(False, is_image('http://foo.fr/foopng'), 'hosts_stats')
124
125    def test_is_style_a(self):
126        self.assertEqual(True, is_style('http://foo.fr/foo.css'), 'hosts_stats')
127
128    def test_is_style_b(self):
129        self.assertEqual(False, is_style('http://foo.fr/foocss'), 'hosts_stats')
130
131    def test_is_script_a(self):
132        self.assertEqual(True, is_script('http://foo.fr/foo.js'), 'hosts_stats')
133
134    def test_is_script_b(self):
135        self.assertEqual(False, is_script('http://foo.fr/foocss'), 'hosts_stats')
136
137    def test_stats_for_host_a(self):
138        urls = ['http://foo.fr/foo.css', 'http://foo.fr/bar.css', 
139                'http://foo.fr/foo.png', 'http://bar.fr/bar.css' ]
140        self.assertEqual((1,0,2), stats_for_host('http://foo.fr', urls), 'hosts_stats')
141
142    def test_stats_for_host_b(self):
143        urls = ['http://foo.fr/foo.css', 'http://foo.fr/bar.css', 
144                'http://foo.fr/foo.png', 'http://bar.fr/bar.css' ]
145        self.assertEqual((0,0,0), stats_for_host('http://foobar.fr', urls), 'hosts_stats')
146
147
148if __name__ == '__main__':
149    unittest.main()