<commit_before>#encoding: utf8
import itertools
import unittest

from utilities import Todo

class FeatureTest(unittest.TestCase):
    def makeOne(self, *args, **kw):
        from mapnik2 import Feature
        return Feature(*args, **kw)
    
    def test_default_constructor(self):
        f = self.makeOne(1)
        self.failUnless(f is not None)

    def test_python_extended_constructor(self):
        from mapnik2 import Box2d
        f = self.makeOne(1, 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))', foo="bar")
        self.failUnlessEqual(f['foo'], 'bar')
        self.failUnlessEqual(f.envelope(),Box2d(10.0,10.0,45.0,45.0))
        area = 0.0
        for g in f.geometries():
            area += g.area()
        self.failUnlessEqual(area,-450.0)
    
    def test_set_get_properties(self):
        f = self.makeOne(1)
        counter = itertools.count(0)
        def test_val(expected):
            key = 'prop%d'%counter.next()
            try:
                f[key] = expected
            except TypeError:
                self.fail("%r (%s)"%(expected, type(expected)))
            self.failUnlessEqual(f[key], expected)
        for v in (1, True, 1.4, "foo", u"avión"):
            test_val(v)


    def test_add_wkt_geometry(self):
        from mapnik2 import Geometry2d
        from mapnik2 import Box2d
        def add_geom(wkt):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkt(wkt)
            self.failUnlessEqual(len(f.geometries()), 3)
            e = Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()
                    
            self.failUnlessEqual(e, f.envelope())
            
        add_geom('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10),POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10)))')

if __name__ == "__main__":
    [eval(run)() for run in dir() if 'test_' in run]
<commit_msg>+ add reading WKB test<commit_after>#encoding: utf8
import itertools
import unittest

from utilities import Todo

class FeatureTest(unittest.TestCase):
    def makeOne(self, *args, **kw):
        from mapnik2 import Feature
        return Feature(*args, **kw)
    
    def test_default_constructor(self):
        f = self.makeOne(1)
        self.failUnless(f is not None)

    def test_python_extended_constructor(self):
        from mapnik2 import Box2d
        f = self.makeOne(1, 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))', foo="bar")
        self.failUnlessEqual(f['foo'], 'bar')
        self.failUnlessEqual(f.envelope(),Box2d(10.0,10.0,45.0,45.0))
        area = 0.0
        for g in f.geometries():
            area += g.area()
        self.failUnlessEqual(area,-450.0)
    
    def test_set_get_properties(self):
        f = self.makeOne(1)
        counter = itertools.count(0)
        def test_val(expected):
            key = 'prop%d'%counter.next()
            try:
                f[key] = expected
            except TypeError:
                self.fail("%r (%s)"%(expected, type(expected)))
            self.failUnlessEqual(f[key], expected)
        for v in (1, True, 1.4, "foo", u"avión"):
            test_val(v)

    def test_add_wkt_geometry(self):
        from mapnik2 import Box2d
        def add_geom_wkt(wkt):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkt(wkt)
            self.failUnlessEqual(len(f.geometries()), 3)
            e = Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()
                    
            self.failUnlessEqual(e, f.envelope())
        from binascii import unhexlify
        def add_geom_wkb(wkb):
            f = self.makeOne(1)
            self.failUnlessEqual(len(f.geometries()), 0)
            f.add_geometries_from_wkb(unhexlify(wkb))
            self.failUnlessEqual(len(f.geometries()), 1)
            e = Box2d()
            self.failUnlessEqual(e.valid(), False)
            for g in f.geometries():
                if not e.valid():
                    e = g.envelope()
                else:
                    e +=g.envelope()
                    
            self.failUnlessEqual(e, f.envelope())
        def run() :
            add_geom_wkt('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10),POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10)))')
            add_geom_wkb('010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440') # POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
        run()
        
if __name__ == "__main__":
    [eval(run)() for run in dir() if 'test_' in run]

{'fraction_non_alphanumeric': 0.1222397476340694, 'fraction_numerical': 0.0804416403785489, 'mean_word_length': 1.9718804920913884, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, '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': '4022075', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1625, 'n_words': 370}