#!/usr/bin/env python
# -*- coding: utf-8 -*-

from nose.tools import *
from utilities import execution_path, run_all
import os, mapnik

def setup():
    # All of the paths used are relative, if we run the tests
    # from another directory we need to chdir()
    os.chdir(execution_path('.'))

if 'topojson' in mapnik.DatasourceCache.plugin_names():

    def test_topojson_init():
        # topojson tests/data/json/escaped.geojson -o tests/data/json/escaped.topojson --properties
        # topojson version 1.4.2
        ds = mapnik.Datasource(type='topojson',file='../data/json/escaped.topojson')
        e = ds.envelope()
        assert_almost_equal(e.minx, -81.705583, places=7)
        assert_almost_equal(e.miny, 41.480573, places=6)
        assert_almost_equal(e.maxx, -81.705583, places=5)
        assert_almost_equal(e.maxy, 41.480573, places=3)

    def test_topojson_properties():
        ds = mapnik.Datasource(type='topojson',file='../data/json/escaped.topojson')
        f = ds.features_at_point(s.envelope().center()).features[0]

        desc = ds.describe()
        eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)

        eq_(f['name'], u'test')
        eq_(f['description'], u'Test: \u005C')
        eq_(f['int'], 1)
        eq_(f['double'], u'Quebec')
        eq_(f['boolean'], True)
        eq_(f['NOM_FR'], u'Qu\xe9bec')
        eq_(f['NOM_FR'], u'Québec')

    def test_topojson_properties():
        ds = mapnik.Datasource(type='topojson',file='../data/json/escaped.topojson')
        f = ds.all_features()[0]

        desc = ds.describe()
        eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)

        eq_(f['name'], u'Test')
        eq_(f['int'], 1)
        eq_(f['double'], 1.1)
        eq_(f['boolean'], True)
        eq_(f['NOM_FR'], u'Qu\xe9bec')
        eq_(f['NOM_FR'], u'Québec')
        eq_(f['spaces'], u'this has spaces')
        eq_(f['description'], u'Test: \u005C')

#    @raises(RuntimeError)
    def test_that_nonexistant_query_field_throws(**kwargs):
        ds = mapnik.Datasource(type='topojson',file='../data/json/escaped.topojson')
        eq_(len(ds.fields()),7)
        # TODO - this sorting is messed up
        eq_(ds.fields(),['name', 'int', 'description', 'spaces', 'double', 'boolean', 'NOM_FR'])
        eq_(ds.field_types(),['str', 'int', 'str', 'str', 'float', 'bool', 'str'])
# TODO - should topojson plugin throw like others?
#        query = mapnik.Query(ds.envelope())
#        for fld in ds.fields():
#            query.add_property_name(fld)
#        # also add an invalid one, triggering throw
#        query.add_property_name('bogus')
#        fs = ds.features(query)

if __name__ == "__main__":
    setup()
    exit(run_all(eval(x) for x in dir() if x.startswith("test_")))

@@ -1004,16 +1004,17 @@
 t_point(
+d
 s.envelo
@@ -1036,24 +1036,55 @@
 features%5B0%5D%0A
+        eq_(len(ds.fields()),7)
 %0A        des
@@ -1196,13 +1196,38 @@
 , u'
-test'
+Test')%0A        eq_(f%5B'int'%5D, 1
 )%0A  
@@ -1282,32 +1282,52 @@
      eq_(f%5B'
-int'%5D, 1
+spaces'%5D, u'this has spaces'
 )%0A        eq
@@ -1345,17 +1345,11 @@
 '%5D, 
-u'Quebec'
+1.1
 )%0A  
@@ -1458,44 +1458,8 @@
 ')%0A%0A
-    def test_topojson_properties():%0A
     
@@ -1571,16 +1571,48 @@
 res()%5B0%5D
+%0A        eq_(len(ds.fields()),7)
 %0A%0A      
@@ -1749,32 +1749,124 @@
 q_(f%5B'int'%5D, 1)%0A
+        eq_(f%5B'description'%5D, u'Test: %5Cu005C')%0A        eq_(f%5B'spaces'%5D, u'this has spaces')%0A
         eq_(f%5B'd
@@ -1989,100 +1989,8 @@
 ec')
-%0A        eq_(f%5B'spaces'%5D, u'this has spaces')%0A        eq_(f%5B'description'%5D, u'Test: %5Cu005C')
 %0A%0A# 

fix topojson tests