def setUp(self): self._es_parser = Parser()
class ParserTestCase(TestCase): def setUp(self): self._es_parser = Parser() def test_parse_image_from_document(self): es_json = """ { "_index" : "impim-test", "_type" : "image", "_id" : "id", "exists": true, "_source" : { "credits": "Salve Jorge/TV Globo", "url": "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg", "created_date": "2012-09-24T14:12:12", "width": 940, "event_date": "2012-09-24T14:12:12", "title": "Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia", "height": 588 } } """ parsed = self._es_parser.parse_image_from_document(es_json) assert parsed['id'] == "id" assert parsed['credits'] == u"Salve Jorge/TV Globo" assert parsed['url'] == "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg" assert parsed['created_date'] == dateutil.parser.parse("2012-09-24T14:12:12") assert parsed['width'] == 940 assert parsed['event_date'] == dateutil.parser.parse("2012-09-24T14:12:12") assert parsed['title'] == u"Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia" assert parsed['height'] == 588 def test_parse_image_from_document_when_index_doesnt_exist(self): es_json = """{"status": 404, "error": "IndexMissingException[[impim-test] missing]"}""" assert self._es_parser.parse_image_from_document(es_json) == None def test_parse_image_from_document_when_image_doesnt_exist(self): es_json = """{"_type": "image", "_id": "non-existent", "exists": false, "_index": "impim-test"}""" assert self._es_parser.parse_image_from_document(es_json) == None def test_parse_images_from_search(self): es_json = """ { "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "impim-api", "_type" : "image", "_id" : "id", "_score" : 1.0, "_source" : { "credits": "Salve Jorge/TV Globo", "url": "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg", "created_date": "2012-09-24T14:12:12", "width": 940, "event_date": "2012-09-24T14:12:12", "title": "Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia", "height": 588 } } ] } } """ parsed = self._es_parser.parse_images_from_search(es_json) assert parsed['total'] == 1 assert len(parsed['items']) == 1 assert parsed['items'][0]['id'] == "id" assert parsed['items'][0]['credits'] == u"Salve Jorge/TV Globo" assert parsed['items'][0]['url'] == "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg" assert parsed['items'][0]['created_date'] == dateutil.parser.parse("2012-09-24T14:12:12") assert parsed['items'][0]['width'] == 940 assert parsed['items'][0]['event_date'] == dateutil.parser.parse("2012-09-24T14:12:12") assert parsed['items'][0]['title'] == u"Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia" assert parsed['items'][0]['height'] == 588 def test_parse_images_from_search_when_index_doesnt_exist(self): es_json = """{"status": 404, "error": "IndexMissingException[[impim-test] missing]"}""" parsed = self._es_parser.parse_images_from_search(es_json) assert parsed['total'] == 0 assert len(parsed['items']) == 0
class ParserTestCase(TestCase): def setUp(self): self._es_parser = Parser() def test_parse_image_from_document(self): es_json = """ { "_index" : "impim-test", "_type" : "image", "_id" : "id", "exists": true, "_source" : { "credits": "Salve Jorge/TV Globo", "url": "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg", "created_date": "2012-09-24T14:12:12", "width": 940, "event_date": "2012-09-24T14:12:12", "title": "Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia", "height": 588 } } """ parsed = self._es_parser.parse_image_from_document(es_json) assert parsed['id'] == "id" assert parsed['credits'] == u"Salve Jorge/TV Globo" assert parsed[ 'url'] == "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg" assert parsed['created_date'] == dateutil.parser.parse( "2012-09-24T14:12:12") assert parsed['width'] == 940 assert parsed['event_date'] == dateutil.parser.parse( "2012-09-24T14:12:12") assert parsed[ 'title'] == u"Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia" assert parsed['height'] == 588 def test_parse_image_from_document_when_index_doesnt_exist(self): es_json = """{"status": 404, "error": "IndexMissingException[[impim-test] missing]"}""" assert self._es_parser.parse_image_from_document(es_json) == None def test_parse_image_from_document_when_image_doesnt_exist(self): es_json = """{"_type": "image", "_id": "non-existent", "exists": false, "_index": "impim-test"}""" assert self._es_parser.parse_image_from_document(es_json) == None def test_parse_images_from_search(self): es_json = """ { "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "impim-api", "_type" : "image", "_id" : "id", "_score" : 1.0, "_source" : { "credits": "Salve Jorge/TV Globo", "url": "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg", "created_date": "2012-09-24T14:12:12", "width": 940, "event_date": "2012-09-24T14:12:12", "title": "Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia", "height": 588 } } ] } } """ parsed = self._es_parser.parse_images_from_search(es_json) assert parsed['total'] == 1 assert len(parsed['items']) == 1 assert parsed['items'][0]['id'] == "id" assert parsed['items'][0]['credits'] == u"Salve Jorge/TV Globo" assert parsed['items'][0][ 'url'] == "s.glbimg.com/et/nv/f/original/2012/09/24/istambul_asia.jpg" assert parsed['items'][0]['created_date'] == dateutil.parser.parse( "2012-09-24T14:12:12") assert parsed['items'][0]['width'] == 940 assert parsed['items'][0]['event_date'] == dateutil.parser.parse( "2012-09-24T14:12:12") assert parsed['items'][0][ 'title'] == u"Istambul é a única cidade no mundo que fica em dois continentes: Europa e Ásia" assert parsed['items'][0]['height'] == 588 def test_parse_images_from_search_when_index_doesnt_exist(self): es_json = """{"status": 404, "error": "IndexMissingException[[impim-test] missing]"}""" parsed = self._es_parser.parse_images_from_search(es_json) assert parsed['total'] == 0 assert len(parsed['items']) == 0