# TestCalculatorFunctions.py import unittest from scraper import Scraper from pprint import pprint class KnownValues(unittest.TestCase): scraper = Scraper(); def test_instance(self): self.assertIsInstance(self.scraper, Scraper) def test_get_stations(self): fname = 'elenco_stazioni.txt' stations = self.scraper.get_stations(fname) with open(fname) as fopen: self.assertEqual(len(stations), len(list(fopen))) for station in stations: self.assertTrue('name' in station) self.assertTrue('code' in station) def test_parse_stations(self): source = '''BARI CENTRALE|S11119 BARI TORRE QUETTA|S11004 BOLOGNA C.LE|S05043''' stations = self.scraper.parse_stations(source) self.assertListEqual(stations, [ {'name': 'BARI CENTRALE', 'code': 'S11119'}, {'name': 'BARI TORRE QUETTA', 'code': 'S11004'}, {'name': 'BOLOGNA C.LE', 'code': 'S05043'}, ]) for station in stations: self.assertTrue('name' in station) self.assertTrue('code' in station) def test_parse_station(self): station = 'SAN LEONARDO DI CUTRO|S11827' expected = {'name': 'SAN LEONARDO DI CUTRO', 'code': 'S11827'} self.assertDictEqual(self.scraper.parse_station(station), expected) def test_find_station(self): station_name = 'monz' fname = 'elenco_stazioni.txt' stations = self.scraper.get_stations(fname) expected = [ {'name': 'MONZA', 'code': 'S01322'}, {'name': 'MONZA SOBBORGHI', 'code': 'S01480'}, {'name': 'MONZONE-M.DEI BANCHI-ISOLANO', 'code': 'S06225'}, {'name': 'MONZUNO', 'code': 'S05132'}, ] self.assertListEqual(self.scraper.find_stations(station_name, stations), expected) # def test_can_connect(self): # scraper = Scraper() # self.assertEqual(scraper.touch('http://ddg.gg'), 200) # # def test_get_page(self): # scraper = Scraper() # self.assertEqual(scraper.get_page().status_code, 200) # # def test_format_hackerspace(self): # scraper = Scraper() # hackerspace = {'name':'pippo'} # formatted = scraper.format_hackerspace(hackerspace) # self.assertTrue('url' in formatted) # # # def test_get_hackerspaces(self): # scraper = Scraper() # hackerspaces = scraper.get_hackerspaces() # self.assertGreater(len(hackerspaces), 0) # # for hackerspace in hackerspaces: # self.assertTrue('url' in hackerspace) # # def test_convert_text_field_to_hs_url(self): # scraper = Scraper() # textfield = 'Freaknet' # self.assertEqual(scraper.convert_text_field_to_hs_url(textfield), 'https://wiki.hackerspaces.org/Freaknet') if __name__ == '__main__': unittest.main()