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

from zope.app.tests import ztapi
from zope.interface import directlyProvides

from Products.ATContentTypes.content.base import BaseContent
from Products.ATContentTypes.content.document import ATDocument

from Products.PleiadesGeocoder.interfaces import IGeoreferenceable \
    , IGeoserializable , IGeoItemSimple, IGeoCollectionSimple \
    , IGeoAnnotatableContent, IGeoFolder

from Products.PleiadesGeocoder.geo import GeoItemSimple, GeoCollectionSimple
from Products.PleiadesGeocoder.utils import addSpatialMemberData

from Products.PloneTestCase import PloneTestCase

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

default_user = PloneTestCase.default_user
default_password = PloneTestCase.default_password


class ConfigTest(PloneTestCase.PloneTestCase):

    def testImplements(self):
        self.failUnless(IGeoAnnotatableContent.implementedBy(BaseContent))
        self.failUnless(IGeoAnnotatableContent.implementedBy(ATDocument))
        
class DocumentTest(PloneTestCase.PloneTestCase):

    def afterSetUp(self):
        self.folder.invokeFactory('Document', id='document')
        self.document = self.folder.document

        item = IGeoItemSimple(self.document)
        item.setGeoInterface('Point', (-105.083733, 40.585609))

    def testInterfaces(self):
        self.failUnless(IGeoAnnotatableContent.providedBy(self.document))
        self.failUnless(IGeoFolder.providedBy(self.document))

    def testGeoreferenced(self):
        item = IGeoItemSimple(self.document)
        self.failUnless(item.isGeoreferenced())

    def testGetValues(self):
        item = IGeoItemSimple(self.document)
        self.failUnlessEqual(item.geom_type, 'Point')
        self.failUnlessEqual(item.coords, (-105.083733, 40.585609, 0.0))

class MemberTest(PloneTestCase.PloneTestCase):

    def afterSetUp(self):
        md = self.portal.portal_memberdata
        mt = self.portal.portal_membership
        
        # add properties to memberdata
        addSpatialMemberData(md)
        
        # set member properties
        member = mt.getMemberById(default_user)
        member.setMemberProperties(
                {'geometryType':'point',
                 'spatialCoordinates':'40.585609 -105.083733 0.0'})
        
    def testGetProps(self):
        mt = self.portal.portal_membership
        member = mt.getMemberById(default_user)
        self.failUnlessEqual(member.getProperty('geometryType'), 'point')
        self.failUnlessEqual(member.getProperty('spatialCoordinates'),
            '40.585609 -105.083733 0.0')


def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(ConfigTest))
    suite.addTest(makeSuite(DocumentTest))
    suite.addTest(makeSuite(MemberTest))
    return suite

if __name__ == '__main__':
    framework()

