<commit_before>
# -*- coding: utf-8 -*-

import pytest
import sys
from test_base_class import TestBaseClass
from aerospike import predicates as p

aerospike = pytest.importorskip("aerospike")
try:
    from aerospike.exception import *
except:
    print "Please install aerospike python client."
    sys.exit(1)

class TestGeospatial(TestBaseClass):
    def setup_class(cls):
        """
        Setup method.
        """
        hostlist, user, password = TestBaseClass.get_hosts()
        config = {'hosts': hostlist}
        TestGeospatial.skip_old_server = False
        if user == None and password == None:
            TestGeospatial.client = aerospike.client(config).connect()
        else:
            TestGeospatial.client = aerospike.client(config).connect(user, password)
        
        versioninfo = TestGeospatial.client.info('version')
        for keys in versioninfo:
            for value in versioninfo[keys]:
                if value != None:
                    versionlist = value[value.find("build") + 6:value.find("\n")].split(".")
                    if not (int(versionlist[0]) >= 3 and int(versionlist[1]) >= 6):
                        TestGeospatial.skip_old_server = True
        if not TestGeospatial.skip_old_server:
            TestGeospatial.client.index_2dsphere_create("test", "demo", "loc", "loc_index")

    def teardown_class(cls):
        TestGeospatial.client.index_remove('test', 'loc_index')
        TestGeospatial.client.close()

    def setup_method(self, method):

        self.keys = []
        for i in xrange(10):
            key = ('test', 'demo', i)
            lng = -122 + (0.2 * i)
            lat = 37.5 + (0.2 * i)
            self.geo_object = aerospike.Geo({"type": "Point", "coordinates": [lng, lat] })
    
            TestGeospatial.client.put(key, {"loc": self.geo_object})
            self.keys.append(key)

    def teardown_method(self, method):
        """
        Teardown method.
        """
        for key in self.keys:
            TestGeospatial.client.remove(key)

    def test_geospatial_put_get_positive(self):
        """
            Perform a get and put with multiple bins including geospatial bin
        """

        key = ('test', 'demo', 'single_geo_put')
        geo_object_single = aerospike.Geo({"type": "Point", "coordinates": [42.34, 58.62] })
        geo_object_dict = aerospike.Geo({"type": "Point", "coordinates": [56.34, 69.62] })
        
        TestGeospatial.client.put(key, {"loc": geo_object_single, "int_bin": 2,
            "string_bin": "str", "dict_bin": {"a": 1, "b":2, "geo":
                geo_object_dict}})
        
        key, meta, bins = TestGeospatial.client.get(key)

        assert bins == {'loc': {'coordinates': [42.34, 58.62], 'type': 'Point'},
                "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2,
                  "geo": {'coordinates': [56.34, 69.62], 'type':
                        'Point'}}}

        TestGeospatial.client.remove(key)

    def test_geospatial_positive_query(self):
        """
            Perform a positive geospatial query for a polygon
        """
        if TestGeospatial.skip_old_server:
            pytest.skip("Server does not support geospatial indexes")
        records = []
        query = TestGeospatial.client.query("test", "demo")

        geo_object2 = aerospike.Geo({"type": "Polygon", "coordinates": [[[-122.500000,
    37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000,
        38.080000], [-122.500000, 37.000000]]]})

        query.where(p.within("loc", geo_object2.dumps()))

        def callback((key, metadata, record)):
            records.append(record)

        query.foreach(callback)

        assert len(records) == 3
        assert records == [{'loc': {'coordinates': [-122.0, 37.5], 'type': 'Point'}}, {'loc': {'coordinates': [-121.8, 37.7], 'type':
                'Point'}}, {'loc': {'coordinates': [-121.6, 37.9], 'type': 'Point'}}]

    def test_geospatial_positive_query_outside_shape(self):
        """
            Perform a positive geospatial query for polygon where all points are
            outside polygon
        """
        if TestGeospatial.skip_old_server:
            pytest.skip("Server does not support geospatial indexes")
        records = []
        query = TestGeospatial.client.query("test", "demo")

        geo_object2 = aerospike.Geo({"type": "Polygon", "coordinates": [[[-126.500000,
    37.000000],[-124.000000, 37.000000], [-124.000000, 38.080000],[-126.500000,
        38.080000], [-126.500000, 37.000000]]]})

        query.where(p.within("loc", geo_object2.dumps()))

        def callback((key, metadata, record)):
            records.append(record)

        query.foreach(callback)

        assert len(records) == 0

    def test_geospatial_positive_query_without_set(self):
        """
            Perform a positive geospatial query for a polygon without a set
        """
        if TestGeospatial.skip_old_server:
            pytest.skip("Server does not support geospatial indexes")
	    keys = []
        for i in xrange(1, 10):
            key = ('test', None, i)
            lng = -122 + (0.2 * i)
            lat = 37.5 + (0.2 * i)
            geo_object = aerospike.Geo({"type": "Point", "coordinates": [lng, lat] })
    
            TestGeospatial.client.put(key, {"loc": geo_object})
            keys.append(key)

        TestGeospatial.client.index_2dsphere_create("test", None, "loc", "loc_index_no_set")
        records = []<commit_msg>Small indentation error<commit_after>
# -*- coding: utf-8 -*-

import pytest
import sys
from test_base_class import TestBaseClass
from aerospike import predicates as p

aerospike = pytest.importorskip("aerospike")
try:
    from aerospike.exception import *
except:
    print "Please install aerospike python client."
    sys.exit(1)

class TestGeospatial(TestBaseClass):
    def setup_class(cls):
        """
        Setup method.
        """
        hostlist, user, password = TestBaseClass.get_hosts()
        config = {'hosts': hostlist}
        TestGeospatial.skip_old_server = False
        if user == None and password == None:
            TestGeospatial.client = aerospike.client(config).connect()
        else:
            TestGeospatial.client = aerospike.client(config).connect(user, password)
        
        versioninfo = TestGeospatial.client.info('version')
        for keys in versioninfo:
            for value in versioninfo[keys]:
                if value != None:
                    versionlist = value[value.find("build") + 6:value.find("\n")].split(".")
                    if not (int(versionlist[0]) >= 3 and int(versionlist[1]) >= 6):
                        TestGeospatial.skip_old_server = True
        if not TestGeospatial.skip_old_server:
            TestGeospatial.client.index_2dsphere_create("test", "demo", "loc", "loc_index")

    def teardown_class(cls):
        TestGeospatial.client.index_remove('test', 'loc_index')
        TestGeospatial.client.close()

    def setup_method(self, method):

        self.keys = []
        for i in xrange(10):
            key = ('test', 'demo', i)
            lng = -122 + (0.2 * i)
            lat = 37.5 + (0.2 * i)
            self.geo_object = aerospike.Geo({"type": "Point", "coordinates": [lng, lat] })
    
            TestGeospatial.client.put(key, {"loc": self.geo_object})
            self.keys.append(key)

    def teardown_method(self, method):
        """
        Teardown method.
        """
        for key in self.keys:
            TestGeospatial.client.remove(key)

    def test_geospatial_put_get_positive(self):
        """
            Perform a get and put with multiple bins including geospatial bin
        """

        key = ('test', 'demo', 'single_geo_put')
        geo_object_single = aerospike.Geo({"type": "Point", "coordinates": [42.34, 58.62] })
        geo_object_dict = aerospike.Geo({"type": "Point", "coordinates": [56.34, 69.62] })
        
        TestGeospatial.client.put(key, {"loc": geo_object_single, "int_bin": 2,
            "string_bin": "str", "dict_bin": {"a": 1, "b":2, "geo":
                geo_object_dict}})
        
        key, meta, bins = TestGeospatial.client.get(key)

        assert bins == {'loc': {'coordinates': [42.34, 58.62], 'type': 'Point'},
                "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2,
                  "geo": {'coordinates': [56.34, 69.62], 'type':
                        'Point'}}}

        TestGeospatial.client.remove(key)

    def test_geospatial_positive_query(self):
        """
            Perform a positive geospatial query for a polygon
        """
        if TestGeospatial.skip_old_server:
            pytest.skip("Server does not support geospatial indexes")
        records = []
        query = TestGeospatial.client.query("test", "demo")

        geo_object2 = aerospike.Geo({"type": "Polygon", "coordinates": [[[-122.500000,
    37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000,
        38.080000], [-122.500000, 37.000000]]]})

        query.where(p.within("loc", geo_object2.dumps()))

        def callback((key, metadata, record)):
            records.append(record)

        query.foreach(callback)

        assert len(records) == 3
        assert records == [{'loc': {'coordinates': [-122.0, 37.5], 'type': 'Point'}}, {'loc': {'coordinates': [-121.8, 37.7], 'type':
                'Point'}}, {'loc': {'coordinates': [-121.6, 37.9], 'type': 'Point'}}]

    def test_geospatial_positive_query_outside_shape(self):
        """
            Perform a positive geospatial query for polygon where all points are
            outside polygon
        """
        if TestGeospatial.skip_old_server:
            pytest.skip("Server does not support geospatial indexes")
        records = []
        query = TestGeospatial.client.query("test", "demo")

        geo_object2 = aerospike.Geo({"type": "Polygon", "coordinates": [[[-126.500000,
    37.000000],[-124.000000, 37.000000], [-124.000000, 38.080000],[-126.500000,
        38.080000], [-126.500000, 37.000000]]]})

        query.where(p.within("loc", geo_object2.dumps()))

        def callback((key, metadata, record)):
            records.append(record)

        query.foreach(callback)

        assert len(records) == 0

    def test_geospatial_positive_query_without_set(self):
        """
            Perform a positive geospatial query for a polygon without a set
        """
        if TestGeospatial.skip_old_server:
            pytest.skip("Server does not support geospatial indexes")
        keys = []
        for i in xrange(1, 10):
            key = ('test', None, i)
            lng = -122 + (0.2 * i)
            lat = 37.5 + (0.2 * i)
            geo_object = aerospike.Geo({"type": "Point", "coordinates": [lng, lat] })
    
            TestGeospatial.client.put(key, {"loc": geo_object})
            keys.append(key)

        TestGeospatial.client.index_2dsphere_create("test", None, "loc", "loc_index_no_set")
        records = []
{'fraction_non_alphanumeric': 0.1466291625079394, 'fraction_numerical': 0.04881589692405408, 'mean_word_length': 2.307923169267707, 'pattern_counts': {'":': 54, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '1272729', 'n_tokens_mistral': 3951, 'n_tokens_neox': 3365, 'n_words': 750}