def test_GET_trending(self): events = sorted([fac.event(), fac.event(), fac.event()], key=lambda x: x.score, reverse=True) event_ids = [event.id for event in events] r = self.client.get('/trending') results = self.json(r).get('results') self.assertEqual([event['id'] for event in results], event_ids)
def test_current_user_check_bookmarked(self): user = User(active=True, **self.userdata) self.db.session.add(user) bookmarked_event = fac.event() user.bookmarked.append(bookmarked_event) not_bookmarked_event = fac.event() save() r = self.auth_get('/user/bookmarked/{0}'.format(bookmarked_event.id)) self.assertEqual(r.status_code, 204) r = self.auth_get('/user/bookmarked/{0}'.format(not_bookmarked_event.id)) self.assertEqual(r.status_code, 404)
def test_only_specified_types(self): event = fac.event() event.title = "Foo bar hey" save() story = fac.story() story.title = "Hey there foo foo foo" save() r = self.client.get('/search/foo?types=story') data = self.json(r) expected_story = { 'id': story.id, 'title': story.title, 'image': story.image, 'name': None, 'slug': None, 'rank': '0.0', 'summary': story.summary, 'updated_at': story.updated_at.isoformat(), 'created_at': story.created_at.isoformat(), 'type': 'story', 'url': '/stories/{0}'.format(story.id) } self.assertEqual(data['pagination']['total_count'], 1) self.assertEqual(data['results'], [expected_story])
def test_GET_event(self): event = fac.event() expected_members = [] entities = [] for member in event.members: expected_members.append({'url': '/articles/{0}'.format(member.id)}) for entity in member.entities: entities.append({'url': '/entities/{0}'.format(entity.slug)}) # Filter down to unique entities. expected_entities = list({v['url']: v for v in entities}.values()) expected = { 'id': event.id, 'url': '/events/{0}'.format(event.id), 'title': event.title, 'summary': event.summary, 'image': event.image, 'updated_at': event.updated_at.isoformat(), 'created_at': event.created_at.isoformat(), 'articles': expected_members, 'entities': expected_entities, 'stories': [] } r = self.client.get('/events/{0}'.format(event.id)) self.assertEqual(self.json(r), expected)
def test_GET_event(self): event = fac.event() expected_members = [] entities = [] for member in event.members: expected_members.append({"url": "/articles/{0}".format(member.id)}) for entity in member.entities: entities.append({"url": "/entities/{0}".format(entity.slug)}) # Filter down to unique entities. expected_entities = list({v["url"]: v for v in entities}.values()) expected = { "id": event.id, "url": "/events/{0}".format(event.id), "title": event.title, "summary": event.summary, "image": event.image, "updated_at": event.updated_at.isoformat(), "created_at": event.created_at.isoformat(), "articles": expected_members, "entities": expected_entities, "stories": [], } r = self.client.get("/events/{0}".format(event.id)) self.assertEqual(self.json(r), expected)
def test_collection_pagination(self): fac.event(num=30) r = self.client.get('/events') data = self.json(r) self.assertEqual(len(data['results']), 20) self.assertEqual(data['pagination']['page'], 1) self.assertEqual(data['pagination']['per_page'], 20) self.assertEqual(data['pagination']['total_count'], 30) r = self.client.get('/events?page=2') data = self.json(r) self.assertEqual(len(data['results']), 10) self.assertEqual(data['pagination']['page'], 2) self.assertEqual(data['pagination']['per_page'], 20) self.assertEqual(data['pagination']['total_count'], 30)
def test_delete_current_user_bookmarked(self): user = User(active=True, **self.userdata) self.db.session.add(user) event = fac.event() user.bookmarked.append(event) save() r = self.auth_delete('/user/bookmarked/{0}'.format(event.id)) self.assertEqual(r.status_code, 204) self.assertEqual(user.bookmarked, [])
def test_story_clustering_with_matching_entities(self): # This creates a story with duplicate member events. story = fac.story() # This event is a duplicate of the story's events. event = fac.event() Story.cluster([event]) self.assertEqual(len(story.members), 3)
def test_post_current_user_bookmarked_not_authenticated(self): user = User(active=True, **self.userdata) self.db.session.add(user) event = fac.event() save() r = self.client.post('/user/bookmarked', data={'event_id':event.id}) self.assertEqual(r.status_code, 401) self.assertEqual(user.bookmarked, [])
def test_GET_trending_filter_date_to(self): expected_events = [] for i in range(4): event = fac.event() event.created_at = datetime.strptime('2014-04-21', '%Y-%m-%d') for i in range(2): event = fac.event() event.created_at = datetime.strptime('2014-03-21', '%Y-%m-%d') expected_events.append(event) save() expected_events = sorted(expected_events, key=lambda x: x.score, reverse=True) event_ids = [event.id for event in expected_events] r = self.client.get('/trending?to=2014-04-01') results = self.json(r).get('results') self.assertEqual(len(results), 2) self.assertEqual([event['id'] for event in results], event_ids)
def test_get_current_user_bookmarked(self): # The score of an event is hard to anticipate, so mock it. self.create_patch('argos.core.models.Event.score', return_value=1.0) user = User(active=True, **self.userdata) self.db.session.add(user) event = fac.event() user.bookmarked.append(event) save() r = self.auth_get('/user/bookmarked') expected_members = [] expected_concepts = [{ 'slug': concept.slug, 'url': '/concepts/{0}'.format(concept.slug), 'score': str(concept.score), 'name': concept.name, 'summary': concept.summary, 'image': concept.image } for concept in event.concepts] expected_mentions = [{'name': alias.name, 'slug': alias.concept.slug, 'id': alias.id} for alias in event.mentions] for member in event.members: expected_members.append({ 'id': member.id, 'url': '/articles/{0}'.format(member.id), 'title': member.title, 'ext_url': member.ext_url, 'source': { 'name': None } }) expected = { 'id': event.id, 'url': '/events/{0}'.format(event.id), 'title': event.title, 'summary': event.summary_sentences, 'image': event.image, 'images': [], 'score': '1.0', # json returns floats as strings. 'updated_at': event.updated_at.isoformat(), 'created_at': event.created_at.isoformat(), 'articles': expected_members, 'concepts': expected_concepts, 'mentions': expected_mentions, 'stories': [] } self.assertEqual(self.json(r), [expected])
def test_ranking(self): event = fac.event() event.title = "Foo bar hey" event.summary = "bar bar bar bar bar" story = fac.story() story.title = "Hey there foo" story.summary = "foo foo foo foo foo" save() r = self.client.get('/search/foo') data = self.json(r) self.assertEqual(data['results'][0]['title'], story.title)
def test_GET_event(self): # The score of an event is hard to anticipate, so mock it. self.create_patch('argos.core.models.Event.score', return_value=1.0) event = fac.event() event.members[0].image = 'http://foo.jpg' event.members[1].image = 'http://foo2.jpg' save() expected_members = [] expected_concepts = [{ 'slug': concept.slug, 'url': '/concepts/{0}'.format(concept.slug), 'score': str(concept.score), 'name': concept.name, 'summary': concept.summary, 'image': concept.image } for concept in event.concepts[:10]] expected_mentions = [{'name': alias.name, 'slug': alias.concept.slug, 'id': alias.id} for alias in event.mentions] for member in event.members: expected_members.append({ 'id': member.id, 'url': '/articles/{0}'.format(member.id), 'title': member.title, 'ext_url': member.ext_url, 'source': { 'name': None } }) expected = { 'id': event.id, 'url': '/events/{0}'.format(event.id), 'title': event.title, 'summary': event.summary_sentences, 'image': event.image, 'images': ['http://foo.jpg', 'http://foo2.jpg'], 'score': '1.0', # json returns floats as strings. 'updated_at': event.updated_at.isoformat(), 'created_at': event.created_at.isoformat(), 'articles': expected_members, 'concepts': expected_concepts, 'mentions': expected_mentions, 'stories': [] } r = self.client.get('/events/{0}'.format(event.id)) self.assertEqual(self.json(r), expected)
def test_pagination(self): events = fac.event(num=20) for event in events: event.title = "Foo bar hey" event.summary = "foo foo foo foo foo" stories = fac.story(num=20) for story in stories: story.title = "Hey there foo" story.summary = "foo foo foo foo foo" save() r = self.client.get('/search/foo') data = self.json(r) self.assertEqual(data['pagination']['total_count'], 40) self.assertEqual(len(data['results']), 20)
def test_simple(self): event = fac.event() event.title = "Foo bar hey" save() r = self.client.get('/search/foo') data = self.json(r) expected = { 'id': event.id, 'title': event.title, 'name': None, 'slug': None, 'rank': '0.0', 'image': event.image, 'summary': event.summary, 'updated_at': event.updated_at.isoformat(), 'created_at': event.created_at.isoformat(), 'type': 'event', 'url': '/events/{0}'.format(event.id) } self.assertEqual(data['pagination']['total_count'], 1) self.assertEqual(data['results'], [expected])