class ItemSearchTest(TestCase): """ Class used to define all tests related to Item objects search. """ def setUp(self): """ Setup the database and index some objects. """ # create a new user user = User.objects.create_user('root', '*****@*****.**', 'root') user.is_superuser = True user.save() # create the search manager self.sm = ESManager() # create and index some items for i in range(100): item = Item() item.type = 'image' item.filename = 'test_' + str(i) item.description = 'test' item.mime_type = 'image/png' item.owner = user item.save() serializer = ItemSerializer(item) self.sm.create({'doc_type' : 'items', 'params' : serializer.data }) sleep(1) def tearDown(self): """ Remove the objects previuosly indexed. """ for i in range(101): if self.sm.exists({'doc_type' : 'items', 'params': { 'id' : i+1 }})['results']: self.sm.delete({'doc_type' : 'items', 'params': { 'id' : i+1 }}) def test_filter(self): """ Method used to test the search by filter over Item objects. """ data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "filter": {"field": "filename", "value": "test_1"} } } results = self.sm.search(data) self.assertEquals(results['count'], 1) def test_query(self): """ Method used to test the search by query over Item objects. """ data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "query": {"text": "test", "fields": ["filename", "description"]} } } results = self.sm.search(data) self.assertEquals(results['count'], 100) def test_filtered_query(self): """ Method used to test the search by filtered query over Item objects. This search uses both filter and query parameters for retrieving objects. """ data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "filter": {"field": "filename", "value": "test_1"}, "query" : {"text": "test", "fields": ["filename", "description"]} } } results = self.sm.search(data) self.assertEquals(results['count'], 1)
class IndexTest(TestCase): """ Class used to define all tests related to objects indexing. """ def setUp(self): """ Setup the database and index some objects. """ # create a new user user = User.objects.create_user('root', '*****@*****.**', 'root') user.is_superuser = True user.save() # create the search manager self.sm = ESManager() # create and index some items for i in range(10): item = Item() item.type = 'image' item.filename = 'test_' + str(i) item.mime_type = 'image/png' item.owner = user item.save() serializer = ItemSerializer(item) self.sm.create({'doc_type' : 'items', 'params' : serializer.data }) def tearDown(self): """ Remove the objects previuosly indexed. """ for i in range(11): if self.sm.exists({'doc_type' : 'items', 'params': { 'id' : i+1 }})['results']: self.sm.delete({'doc_type': 'items', 'params': { 'id' : i+1 }}) def test_exists(self): """ Test used to check if an object has already been indexed. """ result = self.sm.exists({'doc_type' : 'items', 'params': { 'id' : 1 }}) self.assertEqual(result['results'], True) def test_not_exists(self): """ Test used to check if not indexed objects are detected. """ result = self.sm.exists({'doc_type' : 'items', 'params': { 'id' : 0 }}) self.assertEqual(result['results'], False) def test_create(self): """ Method used to index a new object. """ item = Item() item.type = 'image' item.filename = 'test_' item.mime_type = 'image/png' item.owner = User.objects.all()[0] item.save() serializer = ItemSerializer(item) result = self.sm.create({'doc_type' : 'items', 'params' : serializer.data }) self.assertEqual(result['created'], True) def test_update(self): """ Method used to update the fields of an existing object. """ data = {'uploaded_at': '2015-09-14T17:03:21.780176Z', 'description': 'Custom file', 'state': 'STORED, ANALYZED', 'visibility': False, 'filename': 'test_1', 'published_at': None, 'filesize': '', 'owner': 1, 'type': 'image', 'id': 1, 'mime_type': 'image/png'} result = self.sm.update({'doc_type' : 'items', 'params': data}) self.assertNotEqual(result['_version'], 1) def test_delete(self): """ Method used to remove indexed data of an existing object. """ result = self.sm.delete({'doc_type' : 'items', 'params': { 'id' : 1 }}) self.assertNotEqual(result['results']['_version'], 1)
class SearchManagerTest(TestCase): """ Test suite used to check the REST API for searching Items. """ def setUp(self): """ Method used to setup the test database """ # create a new user user = User.objects.create_user('root', '*****@*****.**', 'root') user.is_superuser = True user.save() # create the search manager self.sm = ESManager() # create and index some items for i in range(10): item = Item() item.type = 'image' item.filename = 'test_' + str(i) item.mime_type = 'image/png' item.owner = user item.save() serializer = ItemSerializer(item) self.sm.create({'doc_type' : 'items', 'params' : serializer.data }) sleep(1) def tearDown(self): """ Remove the objects previuosly indexed. """ for i in range(11): if self.sm.exists({'doc_type' : 'items', 'params': { 'id' : i+1 }})['results']: self.sm.delete({'doc_type': 'items', 'params': { 'id' : i+1 }}) def test_check(self): """ Check Item object existance """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) data = {"doc_type": "items", "params":{"id":1} } response = client.post('/api/search/items/exists', data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(response.content)['results'], True) def test_create(self): """ Index a new Item """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) item = Item() item.type = 'image' item.filename = 'test_' item.mime_type = 'image/png' item.owner = User.objects.all()[0] item.save() data = {"doc_type" : "items", "params": ItemSerializer(item).data } response = client.post('/api/search/items/create', data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(json.loads(response.content)['created'], True) def test_update(self): """ Update an indexed Item """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) item = Item.objects.get(pk=1) item.description = 'edited image' item.save() data = {"doc_type" : "items", "params": ItemSerializer(item).data } response = client.put('/api/search/items/update', data) self.assertEqual(response.status_code, status.HTTP_200_OK) def test_delete(self): """ Remove an Item from the index """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) data = {"doc_type": "items", "params" : {"id": 1} } response = client.delete('/api/search/items/delete', data) self.assertEqual(response.status_code, status.HTTP_200_OK) def test_query(self): """ Search Item by query """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "query": {"text": "test_1", "fields": ["filename"]}} } response = client.post('/api/search/items', data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(response.content)['count'], 1) def test_filter(self): """ Search Item by filter """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "filter": {"field": "filename", "value": "test_3"}} } response = client.post('/api/search/items', data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(response.content)['count'], 1) def test_filtered_query(self): """ Search Item by filtered query """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "filter": {"field": "filename", "value": "test_3"}, "query" : {"text" : "image", "fields": ["mime_type", "type"]} } } response = client.post('/api/search/items', data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(response.content)['count'], 1) def test_filtered_query2(self): """ Search Item by filtered query """ user = User.objects.all()[0] client = APIClient() client.force_authenticate(user=user) data = { "doc_type": "items", "size": 32, "from": 0, "query_params": { "filter": {"field": "filename", "value": "test_3"}, "query" : {"text" : "video", "fields": ["mime_type", "type"]} } } response = client.post('/api/search/items', data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(response.content)['count'], 0)