def test_delete(self): """Test the UpdateCommand with deletes.""" u = stellr.UpdateCommand(TEST_HTTP) u.add_delete_by_id(0) u.add_delete_by_id([1, 2]) self.assertTrue(len(u._commands), 3) for i, delete in enumerate(u._commands): self.assertEquals(delete, ('delete', {'id': str(i)})) self.assertEqual(u.body, ('{"delete": {"id": "0"},"delete": {"id": "1"}' ',"delete": {"id": "2"}}')) u.clear_command() u.add_delete_by_id(0) self.assertTrue(len(u._commands), 1) self.assertEqual(u.body, '{"delete": {"id": "0"}}') u.clear_command() self.assertEqual(0, len(u._commands)) u.add_delete_by_query('field1:value0') u.add_delete_by_query(['field1:value1', 'field1:value2']) self.assertTrue(len(u._commands), 3) for i, delete in enumerate(u._commands): self.assertEquals(delete, ('delete', { 'query': 'field1:value' + str(i) })) self.assertEqual(u.body, ('{"delete": {"query": "field1:value0"}' ',"delete": {"query": "field1:value1"}' ',"delete": {"query": "field1:value2"}}'))
def test_update_list(self): """Test the UpdateCommand with a list of updates.""" u = stellr.UpdateCommand(TEST_HTTP) docs = [{'a': 1}, {'b': 2}] u.add_documents(docs) self.assertEqual(2, len(u._commands)) self.assertEqual(u.body, ('{"add": {"doc": {"a": 1}}' ',"add": {"doc": {"b": 2}}}'))
def test_optimize(self): """Test adding an optimize operation to a command.""" u = stellr.UpdateCommand(TEST_HTTP) u.add_optimize() self.assertEqual(len(u._commands), 1) self.assertTrue('optimize' in u._commands[0]) self.assertEqual(u.body, '{"optimize": {}}')
def test_commit(self): """Test adding or specifying a commit on a command.""" u = stellr.UpdateCommand(TEST_HTTP, commit=True) self.assertEqual(u.host, 'http://localhost:8983') self.assertEqual(u._handler, '/solr/update/json?wt=json&commit=true') u.add_commit() self.assertEqual(len(u._commands), 1) self.assertEqual(('commit', {}), u._commands[0]) self.assertEqual(u.body, '{"commit": {}}')
def test_zmq_execution_update_success(self, pool): """ Test the execution of an update command that is successful. """ s, c = self._create_zmq_execution_mocks(pool) command = stellr.UpdateCommand(TEST_ZMQ) command.add_documents({'id': 69, 'value': 'sixty-nine'}) data = command.execute() # check the mocks s.send.assert_called_once_with( ('/update/json?wt=json ' '{"add": {"doc": {"id": 69, "value": "sixty-nine"}}}')) self.assertEqual(len(data), 2) self.assertEqual(data['responseHeader']['status'], 0) # verify name is returned data, name = command.execute(return_name=True) self.assertEqual(name, 'update')
def test_update(self): """Test the UpdateCommand with document updates.""" u = stellr.UpdateCommand(TEST_HTTP, commit_within=60000) self.assertEqual(u.host, 'http://localhost:8983') self.assertEqual(u._handler, ('/solr/update/json?' 'wt=json&commitWithin=60000')) a = SimpleObject(DOCUMENTS[0][0], DOCUMENTS[0][1], DOCUMENTS[0][2]) u.add_documents(a) b = dict() for i, field in enumerate(FIELDS): b[field] = DOCUMENTS[1][i] u.add_documents(b) self.assertEqual(len(u._commands), 2) for i, comm in enumerate(u._commands): self.assertEqual(comm[0], 'add') self.assertTrue('doc' in comm[1]) for field, value in comm[1]['doc'].iteritems(): field_ord = FIELDS.index(field) self.assertEqual(DOCUMENTS[i][field_ord], value)
def test_execution_update_success(self, pool): """ Test the execution of an update command that is successful. """ command = stellr.UpdateCommand(TEST_HTTP, name='test update') self.assertEquals(command.pool, pool) response = self._create_execution_mocks(pool, 200) command.add_documents({'id': 69, 'value': 'sixty-nine'}) data = command.execute() # check the mock hdrs = { 'connection': 'keep-alive', 'content-type': 'application/json; charset=utf-8' } pool.urlopen.assert_called_once_with( 'POST', 'http://localhost:8983/solr/update/json?wt=json', body='{"add": {"doc": {"id": 69, "value": "sixty-nine"}}}', headers=hdrs, timeout=15, assert_same_host=False)
def test_update_with_overwrite(self): """Test the UpdateCommand with a value for overwrite.""" u = stellr.UpdateCommand(TEST_HTTP) u.add_documents({'a': 1}, overwrite=False) self.assertEqual(u.body, '{"add": {"doc": {"a": 1}, "overwrite": false}}')
def test_update_with_field_boost(self): """Test the UpdateCommand with a document containing a field boost.""" u = stellr.UpdateCommand(TEST_HTTP) u.add_documents({'a': {'value': 'f', 'boost': 2.0}}) self.assertEqual(u.body, ('{"add": {"doc": {"a": ' '{"boost": 2.0, "value": "f"}}}}'))
def test_update_with_document_boost(self): """Test the UpdateCommand with a document boost.""" u = stellr.UpdateCommand(TEST_HTTP) u.add_documents({'a': 1}, boost=2.0) self.assertEqual(u.body, '{"add": {"doc": {"a": 1}, "boost": 2.0}}')