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_story(self): story = fac.story() users = fac.user(num=4) story.watchers = users save() expected_members = [] entities = [] expected_watchers = [] for member in story.members: expected_members.append({"url": "/events/{0}".format(member.id)}) for entity in member.entities: entities.append({"url": "/entities/{0}".format(entity.slug)}) for user in users: expected_watchers.append({"url": "/users/{0}".format(user.id)}) # Filter down to unique entities. expected_entities = list({v["url"]: v for v in entities}.values()) expected = { "id": story.id, "url": "/stories/{0}".format(story.id), "title": story.title, "summary": story.summary, "image": story.image, "updated_at": story.updated_at.isoformat(), "created_at": story.created_at.isoformat(), "events": expected_members, "entities": expected_entities, "watchers": expected_watchers, } r = self.client.get("/stories/{0}".format(story.id)) self.assertEqual(self.json(r), expected)
def article(num=1, **kwargs): args = [{ 'title': 'Dinosaurs', 'text': 'Dinosaurs are cool, Clinton', 'score': 100 }, { 'title': 'Robots', 'text': 'Robots are nice, Clinton', 'score': 100 }, { 'title': 'Mudcrabs', 'text': 'Mudcrabs are everywhere, Clinton', 'score': 100 }] if not kwargs: a_s = [Article(**args[i]) for i in range(num)] else: a_s = [Article(**kwargs) for i in range(num)] save(a_s) if len(a_s) is 1: return a_s[0] return a_s
def test_GET_story(self): story = fac.story() users = fac.user(num=4) story.watchers = users save() expected_members = [] entities = [] expected_watchers = [] for member in story.members: expected_members.append({'url': '/events/{0}'.format(member.id)}) for entity in member.entities: entities.append({'url': '/entities/{0}'.format(entity.slug)}) for user in users: expected_watchers.append({'url': '/users/{0}'.format(user.id)}) # Filter down to unique entities. expected_entities = list({v['url']: v for v in entities}.values()) expected = { 'id': story.id, 'url': '/stories/{0}'.format(story.id), 'title': story.title, 'summary': story.summary, 'image': story.image, 'updated_at': story.updated_at.isoformat(), 'created_at': story.created_at.isoformat(), 'events': expected_members, 'entities': expected_entities, 'watchers': expected_watchers } r = self.client.get('/stories/{0}'.format(story.id)) self.assertEqual(self.json(r), expected)
def test_entity_update_updates_slug(self): entity = fac.entity() # Sanity check self.assertEqual(entity.slug, 'argos') entity.name = 'foo bar' save() self.assertEqual(entity.slug, 'foo-bar')
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 concept(num=1): args = [{"name": "Argos"}, {"name": "Ceres"}, {"name": "Iliad"}] e_s = [Concept(**args[i]) for i in range(num)] save(e_s) if len(e_s) is 1: return e_s[0] return e_s
def concept(num=1): args = [{'name': 'Argos'}, {'name': 'Ceres'}, {'name': 'Iliad'}] e_s = [Concept(**args[i]) for i in range(num)] save(e_s) if len(e_s) is 1: return e_s[0] return e_s
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_delete_current_user_watching(self): user = User(active=True, **self.userdata) self.db.session.add(user) story = fac.story() user.watching.append(story) save() r = self.auth_delete('/user/watching/{0}'.format(story.id)) self.assertEqual(r.status_code, 204) self.assertEqual(story.watchers, []) self.assertEqual(user.watching, [])
def test_post_current_user_watching_not_authenticated(self): user = User(active=True, **self.userdata) self.db.session.add(user) story = fac.story() save() r = self.client.post('/user/watching', data={'story_id':story.id}) self.assertEqual(r.status_code, 401) self.assertEqual(story.watchers, []) self.assertEqual(user.watching, [])
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 source(num=1): args = [ {"feeds": [Feed(ext_url="foo")], "name": "The Times"}, {"feeds": [Feed(ext_url="bar")], "name": "The Post"}, {"feeds": [Feed(ext_url="sup")], "name": "The Journal"}, ] s_s = [Source(**args[i]) for i in range(num)] save(s_s) if len(s_s) is 1: return s_s[0] return s_s
def entity(num=1): args = [ {'name': 'Argos'}, {'name': 'Ceres'}, {'name': 'Iliad'} ] e_s = [Entity(**args[i]) for i in range(num)] save(e_s) if len(e_s) is 1: return e_s[0] return e_s
def source(num=1): args = [ {'ext_url': 'foo', 'name': 'The Times'}, {'ext_url': 'bar', 'name': 'The Post'}, {'ext_url': 'sup', 'name': 'The Journal'} ] s_s = [Source(**args[i]) for i in range(num)] save(s_s) if len(s_s) is 1: return s_s[0] return s_s
def test_POST_story_watchers(self): story = fac.story() users = fac.user(num=4) user = users[-1] story.watchers = users[:3] save() self.assertEqual(len(story.watchers), 3) # Login the user so we have an authenticated user. r = self.client.post('/test_login', data={'id': user.id}) r = self.client.post('/stories/{0}/watchers'.format(story.id)) self.assertEqual(len(story.watchers), 4)
def test_POST_story_watchers(self): story = fac.story() users = fac.user(num=4) user = users[-1] story.watchers = users[:3] save() self.assertEqual(len(story.watchers), 3) # Login the user so we have an authenticated user. r = self.client.post("/test_login", data={"id": user.id}) r = self.client.post("/stories/{0}/watchers".format(story.id)) self.assertEqual(len(story.watchers), 4)
def test_creates_alias(self): # Patch these methods so # tests don't require the Fuseki server # or a network connection to Wikipedia. self._patch_knowledge_for() self._patch_uri_for_name(self.uri) c = Concept('Argos') self.db.session.add(c) save() self.assertEqual(len(c.aliases), 1) self.assertEqual(Alias.query.count(), 1) self.assertEqual(Alias.query.first().name, 'Argos')
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_story(self): story = fac.story() users = fac.user(num=4) story.watchers = users story.members[0].image = 'http://foo.jpg' story.members[1].image = 'http://foo2.jpg' save() expected_members = [] expected_watchers = [] expected_concepts = [{ 'slug': concept.slug, 'url': '/concepts/{0}'.format(concept.slug), 'score': str(concept.score) } for concept in story.concepts] expected_mentions = [{'name': alias.name, 'slug': alias.concept.slug, 'id': alias.id} for alias in story.mentions] for member in story.events: expected_members.append({ 'id': member.id, 'url': '/events/{0}'.format(member.id), 'title': member.title, 'score': str(member.score), 'updated_at': member.updated_at.isoformat(), 'created_at': member.created_at.isoformat(), 'num_articles': member.num_articles, }) for user in users: expected_watchers.append({ 'id': user.id, 'url': '/users/{0}'.format(user.id) }) expected = { 'id': story.id, 'url': '/stories/{0}'.format(story.id), 'title': story.title, 'summary': story.summary_sentences, 'image': story.image, 'images': ['http://foo2.jpg', 'http://foo.jpg'], 'updated_at': story.updated_at.isoformat(), 'created_at': story.created_at.isoformat(), 'events': expected_members, 'concepts': expected_concepts, 'mentions': expected_mentions, 'watchers': expected_watchers } r = self.client.get('/stories/{0}'.format(story.id)) self.assertEqual(self.json(r), expected)
def test_current_user_check_watching(self): user = User(active=True, **self.userdata) self.db.session.add(user) watched_story = fac.story() user.watching.append(watched_story) not_watched_story = fac.story() save() r = self.auth_get('/user/watching/{0}'.format(watched_story.id)) self.assertEqual(r.status_code, 204) r = self.auth_get('/user/watching/{0}'.format(not_watched_story.id)) self.assertEqual(r.status_code, 404)
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_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_get_current_user_feed(self): user = User(active=True, **self.userdata) self.db.session.add(user) story = fac.story() not_watched_story = fac.story() user.watching.append(story) save() r = self.auth_get('/user/feed') expected_event_ids = [event.id for event in story.events] not_expected_event_ids = [event.id for event in not_watched_story.events] result_event_ids = [event['id'] for event in self.json(r)] self.assertEqual(result_event_ids, expected_event_ids) self.assertNotEqual(result_event_ids, not_expected_event_ids)
def test_DELETE_story_watchers(self): story = fac.story() users = fac.user(num=3) user = users[0] story.watchers = users save() self.assertEqual(len(story.watchers), 3) # Login the user so we have an authenticated user. r = self.client.post("/test_login", data={"id": user.id}) r = self.client.delete("/stories/{0}/watchers".format(story.id)) self.assertEqual(len(story.watchers), 2) for watcher in story.watchers: self.assertNotEqual(watcher, user)
def test_DELETE_story_watchers(self): story = fac.story() users = fac.user(num=3) user = users[0] story.watchers = users save() self.assertEqual(len(story.watchers), 3) # Login the user so we have an authenticated user. r = self.client.post('/test_login', data={'id': user.id}) r = self.client.delete('/stories/{0}/watchers'.format(story.id)) self.assertEqual(len(story.watchers), 2) for watcher in story.watchers: self.assertNotEqual(watcher, user)
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 article(num=1, **kwargs): args = [ {'title':'Dinosaurs', 'text':'Dinosaurs are cool, Clinton'}, {'title':'Robots', 'text':'Robots are nice, Clinton'}, {'title':'Mudcrabs', 'text':'Mudcrabs are everywhere, Clinton'} ] if not kwargs: a_s = [Article(**args[i]) for i in range(num)] else: a_s = [Article(**kwargs) for i in range(num)] save(a_s) if len(a_s) is 1: return a_s[0] return a_s
def article(num=1, **kwargs): args = [ {"title": "Dinosaurs", "text": "Dinosaurs are cool, Clinton", "score": 100}, {"title": "Robots", "text": "Robots are nice, Clinton", "score": 100}, {"title": "Mudcrabs", "text": "Mudcrabs are everywhere, Clinton", "score": 100}, ] if not kwargs: a_s = [Article(**args[i]) for i in range(num)] else: a_s = [Article(**kwargs) for i in range(num)] save(a_s) if len(a_s) is 1: return a_s[0] return a_s
def source(num=1): args = [{ 'feeds': [Feed(ext_url='foo')], 'name': 'The Times' }, { 'feeds': [Feed(ext_url='bar')], 'name': 'The Post' }, { 'feeds': [Feed(ext_url='sup')], 'name': 'The Journal' }] s_s = [Source(**args[i]) for i in range(num)] save(s_s) if len(s_s) is 1: return s_s[0] return s_s
def source(num=1): args = [{ 'ext_url': 'foo', 'name': 'The Times' }, { 'ext_url': 'bar', 'name': 'The Post' }, { 'ext_url': 'sup', 'name': 'The Journal' }] s_s = [Source(**args[i]) for i in range(num)] save(s_s) if len(s_s) is 1: return s_s[0] return s_s
def test_get_current_user_watching(self): user = User(active=True, **self.userdata) self.db.session.add(user) story = fac.story() user.watching.append(story) save() r = self.auth_get('/user/watching') expected_members = [] expected_concepts = [{ 'slug': concept.slug, 'url': '/concepts/{0}'.format(concept.slug), 'score': str(concept.score) } for concept in story.concepts] expected_watchers = [{'url': '/users/{0}'.format(user.id), 'id': user.id}] expected_mentions = [{'name': alias.name, 'slug': alias.concept.slug, 'id': alias.id} for alias in story.mentions] for member in story.members: expected_members.append({ 'id': member.id, 'title': member.title, 'score': str(member.score), 'updated_at': member.updated_at.isoformat(), 'created_at': member.created_at.isoformat(), 'num_articles': member.num_articles, 'url': '/events/{0}'.format(member.id) }) expected = { 'id': story.id, 'url': '/stories/{0}'.format(story.id), 'title': story.title, 'summary': story.summary_sentences, 'image': story.image, 'images': [], 'updated_at': story.updated_at.isoformat(), 'created_at': story.created_at.isoformat(), 'events': expected_members, 'concepts': expected_concepts, 'mentions': expected_mentions, 'watchers': expected_watchers } self.assertEqual(self.json(r), [expected])
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_story_watchers(self): story = fac.story() users = fac.user(num=3) story.watchers = users save() expected = [] for user in users: expected.append({ 'id': user.id, 'name': user.name, 'image': user.image, 'created_at': user.created_at.isoformat(), 'updated_at': user.updated_at.isoformat() }) r = self.client.get('/stories/{0}/watchers'.format(story.id)) self.assertEqual(self.json(r), expected)
def cluster(cls, member_factory, num=1, num_members=2): """ Convenience method for creating a Cluster-like object. Args: | cls (class) -- a Cluster-like class | member_factory (func) -- function for creating members | num (int) -- number of clusters to create | num_members (int) -- number of members in each cluster """ c_s = [] for i in range(num): members = member_factory(num=num_members) c = cls(members) c_s.append(c) save(c_s) if len(c_s) is 1: return c_s[0] return c_s
def test_GET_article(self): article = fac.article() source = fac.source() article.source = source save() r = self.client.get("/articles/{0}".format(article.id)) expected = { "id": article.id, "url": "/articles/{0}".format(article.id), "title": article.title, "ext_url": article.ext_url, "image": article.image, "created_at": article.created_at.isoformat(), "updated_at": article.updated_at.isoformat(), "authors": [], "events": [], "source": {"url": "/sources/{0}".format(source.id), "name": source.name}, } self.assertEqual(self.json(r), expected)
def test_GET_story_watchers(self): story = fac.story() users = fac.user(num=3) story.watchers = users save() expected = [] for user in users: expected.append( { "id": user.id, "name": user.name, "image": user.image, "created_at": user.created_at.isoformat(), "updated_at": user.updated_at.isoformat(), } ) r = self.client.get("/stories/{0}/watchers".format(story.id)) self.assertEqual(self.json(r), expected)
def user(num=1): args = { 'name': 'Hubble Bubble {0}', 'email': 'hubbubs{0}@mail.com', 'image': 'https://hubb.ub/pic{0}.png', 'active': True, 'password': '******' } u_s = [] for i in range(num): # Copy the args and fill in # the numbers. a = copy(args) for k, v in a.items(): if isinstance(a[k], str): a[k] = v.format(i) u_s.append(User(**a)) save(u_s) if len(u_s) is 1: return u_s[0] return u_s
def test_GET_article(self): article = fac.article() source = fac.source() article.source = source save() r = self.client.get('/articles/{0}'.format(article.id)) expected = { 'id': article.id, 'url': '/articles/{0}'.format(article.id), 'title': article.title, 'ext_url': article.ext_url, 'image': article.image, 'created_at': article.created_at.isoformat(), 'updated_at': article.updated_at.isoformat(), 'authors': [], 'events': [], 'source': { 'url': '/sources/{0}'.format(source.id), 'name': source.name } } self.assertEqual(self.json(r), expected)
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])