Added test for optimize method.
Fixed delete_by_query method and added test.