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

from nose.tools import eq_,raises
from utilities import run_all

import mapnik
from binascii import unhexlify

def test_default_constructor():
    f = mapnik.Feature(mapnik.Context(),1)
    eq_(f is not None,True)

def test_feature_geo_interface():
    ctx = mapnik.Context()
    feat = mapnik.Feature(ctx,1)
    feat.geometry = mapnik.Geometry.from_wkt('Point (0 0)')
    eq_(feat.__geo_interface__['geometry'],{u'type': u'Point', u'coordinates': [0, 0]})

def test_python_extended_constructor():
    context = mapnik.Context()
    context.push('foo')
    context.push('foo')
    f = mapnik.Feature(context,1)
    wkt = 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))'
    f.geometry = mapnik.Geometry.from_wkt(wkt)
    f['foo'] = 'bar'
    eq_(f['foo'], 'bar')
    eq_(f.envelope(),mapnik.Box2d(10.0,10.0,45.0,45.0))
    # reset
    f['foo'] = u"avión"
    eq_(f['foo'], u"avión")
    f['foo'] = 1.4
    eq_(f['foo'], 1.4)
    f['foo'] = True
    eq_(f['foo'], True)

def test_add_geom_wkb():
# POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
    wkb = '010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440'
    geometry = mapnik.Geometry.from_wkb(unhexlify(wkb))
    eq_(geometry.is_valid(), True)
    eq_(geometry.is_simple(), True)
    eq_(geometry.envelope(), mapnik.Box2d(10.0,10.0,40.0,40.0))
    geometry.correct()
    # valid after calling correct
    eq_(geometry.is_valid(), True)

def test_feature_expression_evaluation():
    context = mapnik.Context()
    context.push('name')
    f = mapnik.Feature(context,1)
    f['name'] = 'a'
    eq_(f['name'],u'a')
    expr = mapnik.Expression("[name]='a'")
    evaluated = expr.evaluate(f)
    eq_(evaluated,True)
    num_attributes = len(f)
    eq_(num_attributes,1)
    eq_(f.id(),1)

# https://github.com/mapnik/mapnik/issues/933
def test_feature_expression_evaluation_missing_attr():
    context = mapnik.Context()
    context.push('name')
    f = mapnik.Feature(context,1)
    f['name'] = u'a'
    eq_(f['name'],u'a')
    expr = mapnik.Expression("[fielddoesnotexist]='a'")
    eq_(f.has_key('fielddoesnotexist'),False)
    try:
        expr.evaluate(f)
    except Exception, e:<commit_msg>Only test Geometry.is_valid when it exists<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from nose.tools import eq_,raises
from utilities import run_all

import mapnik
from binascii import unhexlify

def test_default_constructor():
    f = mapnik.Feature(mapnik.Context(),1)
    eq_(f is not None,True)

def test_feature_geo_interface():
    ctx = mapnik.Context()
    feat = mapnik.Feature(ctx,1)
    feat.geometry = mapnik.Geometry.from_wkt('Point (0 0)')
    eq_(feat.__geo_interface__['geometry'],{u'type': u'Point', u'coordinates': [0, 0]})

def test_python_extended_constructor():
    context = mapnik.Context()
    context.push('foo')
    context.push('foo')
    f = mapnik.Feature(context,1)
    wkt = 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))'
    f.geometry = mapnik.Geometry.from_wkt(wkt)
    f['foo'] = 'bar'
    eq_(f['foo'], 'bar')
    eq_(f.envelope(),mapnik.Box2d(10.0,10.0,45.0,45.0))
    # reset
    f['foo'] = u"avión"
    eq_(f['foo'], u"avión")
    f['foo'] = 1.4
    eq_(f['foo'], 1.4)
    f['foo'] = True
    eq_(f['foo'], True)

def test_add_geom_wkb():
# POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
    wkb = '010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440'
    geometry = mapnik.Geometry.from_wkb(unhexlify(wkb))
    if hasattr(geometry, 'is_valid'):
        # Those are only available when python-mapnik has been built with
        # boost >= 1.56.
        eq_(geometry.is_valid(), True)
        eq_(geometry.is_simple(), True)
    eq_(geometry.envelope(), mapnik.Box2d(10.0,10.0,40.0,40.0))
    geometry.correct()
    if hasattr(geometry, 'is_valid'):
        # valid after calling correct
        eq_(geometry.is_valid(), True)

def test_feature_expression_evaluation():
    context = mapnik.Context()
    context.push('name')
    f = mapnik.Feature(context,1)
    f['name'] = 'a'
    eq_(f['name'],u'a')
    expr = mapnik.Expression("[name]='a'")
    evaluated = expr.evaluate(f)
    eq_(evaluated,True)
    num_attributes = len(f)
    eq_(num_attributes,1)
    eq_(f.id(),1)

# https://github.com/mapnik/mapnik/issues/933
def test_feature_expression_evaluation_missing_attr():
    context = mapnik.Context()
    context.push('name')
    f = mapnik.Feature(context,1)
    f['name'] = u'a'
    eq_(f['name'],u'a')
    expr = mapnik.Expression("[fielddoesnotexist]='a'")
    eq_(f.has_key('fielddoesnotexist'),False)
    try:
        expr.evaluate(f)
    except Exception, e:
{'fraction_non_alphanumeric': 0.1598872331856625, 'fraction_numerical': 0.11538461538461539, 'mean_word_length': 3.962037962037962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 24, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6939790', 'n_tokens_mistral': 2232, 'n_tokens_neox': 1792, 'n_words': 334}