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__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(), [])
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__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(), [])