import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

from Products.PloneTestCase import PloneTestCase

PloneTestCase.installProduct('PleiadesGeocoder')
PloneTestCase.setupPloneSite(products=['PleiadesGeocoder'])

from Products.PleiadesGeocoder.interfaces import IGeoItemSimple
from Products.PleiadesGeocoder import Geocoder

class ExistenceTest(PloneTestCase.PloneTestCase):
    def afterSetUp(self):
        self.id = Geocoder.id
    def testGeocoderToolExists(self):
        self.failUnless(self.id in self.portal.objectIds())
       
class GoogleGeocodeTest(PloneTestCase.PloneTestCase):
    def afterSetUp(self):
        self.g = self.portal.portal_geocoder
    def test_ftc(self):
        results = self.g.geocode('Fort Collins, CO')
        r = results[0]
        self.assertEqual(r['place'], u'Fort Collins, CO', results)
        self.assertAlmostEqual(r['lat'], 40.5814900)
        self.assertAlmostEqual(r['lon'], -105.07628)
    def test_london(self):
        results = self.g.geocode('London, England')
        r = results[0]
        self.assertEqual(r['place'], u'London, UK', results)
        self.assertAlmostEqual(r['lat'], 51.500152)
        self.assertAlmostEqual(r['lon'], -0.12623599)
    def test_goteborg(self):
        results = self.g.geocode('Goteborg, Sweden')
        r = results[0]
        self.assertEqual(r['place'], u'Gothenburg, Sweden', results)
        self.assertAlmostEqual(r['lat'],  57.69698100)
        self.assertAlmostEqual(r['lon'], 11.986383)

     
def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(ExistenceTest))
    suite.addTest(makeSuite(GoogleGeocodeTest))
    return suite

if __name__ == '__main__':
    framework()

