示例#1
0
    def test__delitem__(self):
        # Create a Mock response object
        config = {
            'status_code': 200,
            'json.return_value': {
                u'_id': u'foo',
                u'_index': u'elasticdict',
                u'_type': u'data',
                u'_version': 1,
                u'found': True,
                u'ok': True
            }
        }
        mock_response = Mock(**config)
        self.mock_requests.delete.return_value = mock_response

        d = ElasticDict()
        d['foo'] = 'bar'  # make sure it's actually in the dictionary
        del d['foo']
        # It's deleted from ElasticSearch
        self.mock_requests.delete.assert_called_once_with(
            "http://localhost:9200/elasticdict/data/foo"
        )
        # It's removed from the dictionary
        eq_(d.items(), [])
示例#2
0
    def test__getitem__no_values(self):
        """Tests ``__getitem__`` when there are no existing values in the dict,
        and when there are no matching values in ElasticSearch"""
        d = ElasticDict()
        d._get_key_from_elasticsearch = Mock(return_value=None)

        # verify that there are not items in the dict
        eq_(d.items(), [])

        # fetch an item
        with assert_raises(KeyError):
            d['foo']
示例#3
0
    def test__getitem__no_values(self):
        """Tests ``__getitem__`` when there are no existing values in the dict,
        and when there are no matching values in ElasticSearch"""
        d = ElasticDict()
        d._get_key_from_elasticsearch = Mock(return_value=None)

        # verify that there are not items in the dict
        eq_(d.items(), [])

        # fetch an item
        with assert_raises(KeyError):
            d['foo']
示例#4
0
    def test__contains__(self):
        # when the key exists in the dictionary
        d = ElasticDict()
        d['foo'] = 'bar'
        ok_('foo' in d)

        # when the key exists, but is pulled from ElasticSearch
        d = ElasticDict()
        d._get_key_from_elasticsearch = Mock(return_value={"fizz": "buzz"})
        eq_(d.items(), [])
        ok_("fizz" in d)
        eq_(d.items(), [("fizz", "buzz")])
示例#5
0
    def test__setitem__(self):
        d = ElasticDict()
        d['foo'] = 'bar'  # Set an item

        # super calls __setitem__(key, value); so we get keys/values just like
        # a regular dictionary
        eq_(d.values(), ['bar'])
        eq_(d.keys(), ['foo'])

        # Then the key/value is json-ified and posted to ElasticSearch
        self.mock_requests.post.assert_called_once_with(
            "http://localhost:9200/elasticdict/data/foo",
            data='{"foo": "bar"}')
示例#6
0
    def test__getitem__es_values(self):
        """Tests ``__getitem__`` when there are no existing values in the dict,
        but there *are* matching values in ElasticSearch"""
        d = ElasticDict()
        d._get_key_from_elasticsearch = Mock(return_value={'foo': 'bar'})

        # verify that there are not items in the dict
        eq_(d.items(), [])

        # fetch an item
        result = d['foo']
        eq_(result, 'bar')  # value retrieved from ES
        eq_(d.items(), [('foo', 'bar')])  # and inserted into the dict
示例#7
0
    def test__getitem__es_values(self):
        """Tests ``__getitem__`` when there are no existing values in the dict,
        but there *are* matching values in ElasticSearch"""
        d = ElasticDict()
        d._get_key_from_elasticsearch = Mock(return_value={'foo': 'bar'})

        # verify that there are not items in the dict
        eq_(d.items(), [])

        # fetch an item
        result = d['foo']
        eq_(result, 'bar')  # value retrieved from ES
        eq_(d.items(), [('foo', 'bar')])  # and inserted into the dict
示例#8
0
    def test__setitem__(self):
        d = ElasticDict()
        d['foo'] = 'bar'  # Set an item

        # super calls __setitem__(key, value); so we get keys/values just like
        # a regular dictionary
        eq_(d.values(), ['bar'])
        eq_(d.keys(), ['foo'])

        # Then the key/value is json-ified and posted to ElasticSearch
        self.mock_requests.post.assert_called_once_with(
            "http://localhost:9200/elasticdict/data/foo",
            data='{"foo": "bar"}'
        )
示例#9
0
    def test__getitem__existing_values(self):
        """Tests ``__getitem__`` when there are existing values in the dict."""
        d = ElasticDict()
        d['foo'] = 'bar'

        result = d['foo']  # calls super class's __getitem__(key)
        eq_(result, 'bar')
示例#10
0
    def test__query(self):
        # create an object to use for an ES query response
        es_response = {
            '_shards': {
                'failed': 0,
                'successful': 1,
                'total': 1
            },
            'hits': {
                'hits': [{
                    '_id': 'foo',
                    '_index': 'elasticdict',
                    '_score': 0.30685282,
                    '_source': {
                        'foo': 'bar'
                    },
                    '_type': 'data'
                }],
                'max_score':
                0.30685282,
                'total':
                1,
            },
            'timed_out': False,
            'took': 1
        }

        # Create a Mock response object
        config = {
            'status_code': 200,
            'json.return_value': es_response,
        }
        mock_response = Mock(**config)
        self.mock_requests.get.return_value = mock_response

        d = ElasticDict()
        results = d._query('bar')
        eq_(results, [{u'foo': u'bar'}])

        expected_query_json = ('{"query": {"filtered": {"query": '
                               '{"query_string": {"query": "bar"}}}}}')
        self.mock_requests.get.assert_called_once_with(
            'http://localhost:9200/elasticdict/data/_search',
            data=expected_query_json)
示例#11
0
    def test__init__default(self):
        # With default parameters
        d = ElasticDict()
        eq_(d.es_host, "localhost")
        eq_(d.es_port, 9200)
        eq_(d.es_index, "elasticdict")
        eq_(d.es_type, "data")

        index_uri = "http://localhost:9200/elasticdict"
        self.mock_requests.put.assert_called_once_with(index_uri)
示例#12
0
    def test__get_key_from_elasticsearch(self):
        # Create a Mock response object
        config = {
            'status_code': 200,
            'content': """{
                "_index" : "es_index",
                "_type" : "es_type",
                "_id" : "es_id",
                "_version" : 1,
                "exists" : true,
                "_source": "RESULT"}""",
        }
        mock_response = Mock(**config)
        self.mock_requests.get.return_value = mock_response

        d = ElasticDict()
        result = d._get_key_from_elasticsearch("KEY")
        eq_(result, "RESULT")

        uri = "http://localhost:9200/elasticdict/data/KEY"
        self.mock_requests.get.assert_called_once_with(uri)
示例#13
0
    def test__init__custom(self):
        # with custom parameters
        d = ElasticDict(es_host="example.com",
                        es_port=4242,
                        es_index="foo",
                        es_type="thingies")
        eq_(d.es_host, "example.com")
        eq_(d.es_port, 4242)
        eq_(d.es_index, "foo")
        eq_(d.es_type, "thingies")

        index_uri = "http://example.com:4242/foo"
        self.mock_requests.put.assert_called_once_with(index_uri)
示例#14
0
    def test__contains__(self):
        # when the key exists in the dictionary
        d = ElasticDict()
        d['foo'] = 'bar'
        ok_('foo' in d)

        # when the key exists, but is pulled from ElasticSearch
        d = ElasticDict()
        d._get_key_from_elasticsearch = Mock(return_value={"fizz": "buzz"})
        eq_(d.items(), [])
        ok_("fizz" in d)
        eq_(d.items(), [("fizz", "buzz")])
示例#15
0
    def test__get_key_from_elasticsearch(self):
        # Create a Mock response object
        config = {
            'status_code':
            200,
            'content':
            """{
                "_index" : "es_index",
                "_type" : "es_type",
                "_id" : "es_id",
                "_version" : 1,
                "exists" : true,
                "_source": "RESULT"}""",
        }
        mock_response = Mock(**config)
        self.mock_requests.get.return_value = mock_response

        d = ElasticDict()
        result = d._get_key_from_elasticsearch("KEY")
        eq_(result, "RESULT")

        uri = "http://localhost:9200/elasticdict/data/KEY"
        self.mock_requests.get.assert_called_once_with(uri)
示例#16
0
    def test__query(self):
        # create an object to use for an ES query response
        es_response = {
            '_shards': {'failed': 0, 'successful': 1, 'total': 1},
            'hits': {
                'hits': [{
                    '_id': 'foo',
                    '_index': 'elasticdict',
                    '_score': 0.30685282,
                    '_source': {'foo': 'bar'},
                    '_type': 'data'
                }],
                'max_score': 0.30685282,
                'total': 1,
            },
            'timed_out': False,
            'took': 1
        }

        # Create a Mock response object
        config = {
            'status_code': 200,
            'json.return_value': es_response,
        }
        mock_response = Mock(**config)
        self.mock_requests.get.return_value = mock_response

        d = ElasticDict()
        results = d._query('bar')
        eq_(results, [{u'foo': u'bar'}])

        expected_query_json = ('{"query": {"filtered": {"query": '
            '{"query_string": {"query": "bar"}}}}}')
        self.mock_requests.get.assert_called_once_with(
            'http://localhost:9200/elasticdict/data/_search',
            data=expected_query_json
        )
示例#17
0
    def test__delitem__(self):
        # Create a Mock response object
        config = {
            'status_code': 200,
            'json.return_value': {
                u'_id': u'foo',
                u'_index': u'elasticdict',
                u'_type': u'data',
                u'_version': 1,
                u'found': True,
                u'ok': True
            }
        }
        mock_response = Mock(**config)
        self.mock_requests.delete.return_value = mock_response

        d = ElasticDict()
        d['foo'] = 'bar'  # make sure it's actually in the dictionary
        del d['foo']
        # It's deleted from ElasticSearch
        self.mock_requests.delete.assert_called_once_with(
            "http://localhost:9200/elasticdict/data/foo")
        # It's removed from the dictionary
        eq_(d.items(), [])
示例#18
0
    def test__delitem__fails(self):
        """Deleting a Non-existing item should raise a KeyError."""
        # Create a Mock response object
        config = {
            'status_code': 404,
            'json.return_value': {
                u'_id': u'nothing',
                u'_index': u'elasticdict',
                u'_type': u'data',
                u'_version': 1,
                u'found': False,
                u'ok': True
            }
        }
        mock_response = Mock(**config)
        self.mock_requests.delete.return_value = mock_response

        d = ElasticDict()
        with assert_raises(KeyError):
            del d['nothing']
            self.mock_requests.delete.assert_called_once_with(
                "http://localhost:9200/elasticdict/data/nothing")
示例#19
0
 def test__uri(self):
     d = ElasticDict()
     result = d._uri("some_key")
     eq_(result, "http://localhost:9200/elasticdict/data/some_key")
示例#20
0
 def test__uri(self):
     d = ElasticDict()
     result = d._uri("some_key")
     eq_(result, "http://localhost:9200/elasticdict/data/some_key")