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']
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")])
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
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)