"""Unit test for the sampling algorithm""" # Copyright 2005, 2006 EIAO Consoritum # This program is distributed under the terms of the GNU General # Public License. # # This file is part of the European Internet Accessibility Observatory # (EIAO) # # EIAO is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # EIAO is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with EIAO; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1301 USA __author__ = "Morten Goodwin Olsen" __maintainer__ = "Nils Ulltveit-Moe" __version__ = "$Id$" from samplingalgorithm import * import samplingalgorithm import unittest, doctest result = [] result.append(doctest.testmod(samplingalgorithm)) print 'Note that if you do not have any scenarios from www.selje.kommune.no in the URL repository, used as an example in these tests, unit tests will fail.' import os #Creating new test RDF DB if not already exists import MySQLdb from _mysql_exceptions import * import sc sc = sc.SystemConfiguration() try: con = MySQLdb.connect(db='rdf_unittest',passwd=sc.dbpassword,user=sc.dbdatabase) cur = con.cursor() cur.execute('select 1;') data = cur.fetchall() except OperationalError: #Database does not exist import os os.popen('mysql --user=root mysql -e"CREATE DATABASE rdf_unittest"') os.popen('mysql --user=root mysql -e"GRANT ALL PRIVILEGES ON rdf_unittest.* TO rdf@localhost IDENTIFIED BY \''+sc.dbpassword+'\';";') os.popen('mysql -u'+sc.dbdatabase+' -p'+sc.dbpassword+' rdf_unittest < ../Crawler/tables.sql') else: print 'RDF unittest DB already exists' class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.sa = SamplingAlgorithm('/eiao/hm-outofbox2/EIAOHarvestMan2', 'www.selje.kommune.no','rdf_unittest','1234','4321') def tearDown(self): self.sa.stop() del self.sa def testgetURLStorageLocation(self): """Testing that the URL storage location is availale""" t = self.sa.getURLStorageLocation('http://www.selje.kommeune.no/') self.assert_(type(t)==type('')) def testHeaderofURL(self): """Test that it is possible to get header of a URL""" #Note that the header might be empty. If this is the case, we get the header from the web cache import urllib2 opener = urllib2.build_opener() d = opener.open('http://www.selje.kommune.no/') t = self.sa.getHeaderOfURL(d) self.assert_(type(t)==type({})) def testLastModifiedTimeStamp(self): """Test that there exists a last-modified timestamp""" #Note that the last-modified timestamp might be 0. This is the case if last-modified was not present when the URL was downloaded. t = self.sa.getLatestTimeStampOfURL('http://www.selje.kommeune.no/') self.assert_(type(t)==type(0.0)) def testgetRandomScenario(self): """Testing that you get a scenario when asking for a random scenario""" t = self.sa.getRandomScenario() self.assert_(type(t)==type({})) self.assert_(type(t.keys()[0])==type(0.0) or type(t.keys()[0])==type(1)) self.assert_(type(t.values()[0]==type([]))) self.assert_(type(t.values()[0][0])==type('')) def testDownloadTimeStampAvailable(self): """Testing that the downloaded timestamp for a URL is available""" t = self.sa.getLatestTimeStampOfURL('http://www.selje.kommeune.no/') self.assert_(type(t)==type(0.0)) #Need to round of to closest 10 decimals, because of Python float representation def testrandomscenario(self): """Testing that the scenarios are retieved in a random and unique manner""" knownscenarios = [] for i in self.sa.scenarios.items(): s = self.sa.getRandomScenario() self.assert_(type(s)==type({})) s = s.values()[0] self.assert_(s not in knownscenarios) knownscenarios.append(s) def testscenariospresent(self): """Testing that the site has scenarios""" self.assert_(self.sa.scenarios) for key,value in self.sa.scenarios.items(): self.assert_(type(key)==type(0.0) or type(key)==type(1)) self.assert_(type(value)==type([])) for url in value: self.assert_(type(url)==type('')) self.assert_(url.startswith('http')) if __name__ == "__main__": unittest.main()