def setUp(self): self.factory = RequestFactory() self.outlet1 = Outlet.objects.create(name='Sample Feed', description="Description", url='http://example.org', language='en-US', updated=None) self.outlet2 = Outlet.objects.create(name='Sample Feed 2', description="Description 2", url='http://example2.org', language='en-GB', updated=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc)) self.author1 = Author(name='Author1') self.author2 = Author(name='Author2', profile='http://plus.google.com/Author2', twitter='@author2') self.outlet1.author_set.add(self.author1) self.outlet1.author_set.add(self.author2) self.article1 = Article(title='Title1', summary='Summary1', url='http://example.com/articles/1', pub_date=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc), content='Content1') self.article2 = Article(title='Title2', summary='Summary2', url='http://example.com/articles/2', pub_date=datetime.datetime( 2002, 9, 7, 0, 1, 1, tzinfo=timezone.utc), content='Content2') self.outlet1.article_set.add(self.article1) self.outlet1.article_set.add(self.article2) self.article1.authors.add(self.author1, self.author2) self.article2.authors.add(self.author1) self.tag1 = Tag.objects.create(term='Tag1') self.tag2 = Tag.objects.create(term='Tag2') self.article1.tags.add(self.tag1) self.article2.tags.add(self.tag2)
class RestAPITestCase(TestCase): def setUp(self): self.factory = RequestFactory() self.outlet1 = Outlet.objects.create(name='Sample Feed', description="Description", url='http://example.org', language='en-US', updated=None) self.outlet2 = Outlet.objects.create(name='Sample Feed 2', description="Description 2", url='http://example2.org', language='en-GB', updated=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc)) self.author1 = Author(name='Author1') self.author2 = Author(name='Author2', profile='http://plus.google.com/Author2', twitter='@author2') self.outlet1.author_set.add(self.author1) self.outlet1.author_set.add(self.author2) self.article1 = Article(title='Title1', summary='Summary1', url='http://example.com/articles/1', pub_date=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc), content='Content1') self.article2 = Article(title='Title2', summary='Summary2', url='http://example.com/articles/2', pub_date=datetime.datetime( 2002, 9, 7, 0, 1, 1, tzinfo=timezone.utc), content='Content2') self.outlet1.article_set.add(self.article1) self.outlet1.article_set.add(self.article2) self.article1.authors.add(self.author1, self.author2) self.article2.authors.add(self.author1) self.tag1 = Tag.objects.create(term='Tag1') self.tag2 = Tag.objects.create(term='Tag2') self.article1.tags.add(self.tag1) self.article2.tags.add(self.tag2) def test_outlets_api(self): expected = [self.outlet1.__data__(), self.outlet2.__data__()] # Ordered by name outlets_response = views.outlets(self.factory.get('/rss/outlets/')) self.assertEqual(outlets_response.status_code, 200) deserialized_data = json.loads(outlets_response.content) self.assertEqual(deserialized_data, expected) def test_outlet_api(self): expected = self.outlet1.__data__() outlets_response = views.outlet(self.factory.get('/rss/outlets/1/'), self.outlet1.id) self.assertEqual(outlets_response.status_code, 200) deserialized_data = json.loads(outlets_response.content) self.assertEqual(deserialized_data, expected) def test_authors_api(self): expected = [self.author1.__data__(), self.author2.__data__()] # Ordered by name response = views.authors(self.factory.get('/rss/outlets/1/authors/'), self.outlet1.id) self.assertEqual(response.status_code, 200) deserialized_data = json.loads(response.content) self.assertEqual(deserialized_data, expected) def test_author_api(self): expected = self.author1.__data__() response = views.author(self.factory.get('/rss/outlets/1/authors/1'), self.outlet1.id, self.author1.id) self.assertEqual(response.status_code, 200) deserialized_data = json.loads(response.content) self.assertEqual(deserialized_data, expected) def test_all_authors_api(self): expected = [self.author1.__data__(), self.author2.__data__()] # Ordered by name response = views.all_authors(self.factory.get('/rss/authors/')) self.assertEqual(response.status_code, 200) deserialized_data = json.loads(response.content) self.assertEqual(deserialized_data, expected) def test_articles_api(self): expected = [self.article2.__data__(), self.article1.__data__() ] # Ordered by publication date from newest to oldest response = views.articles(self.factory.get('/rss/outlets/1/articles/'), self.outlet1.id) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_article_api(self): expected = self.article1.__data__() response = views.article(self.factory.get('/rss/outlets/1/articles/1'), self.outlet1.id, self.article1.id) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_all_articles_api(self): expected = [self.article2.__data__(), self.article1.__data__() ] # Ordered by publication date from newest to oldest response = views.all_articles(self.factory.get('/rss/articles/')) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_tags_api(self): expected = [self.tag1.__data__(), self.tag2.__data__()] response = views.tags(self.factory.get('/rss/tags')) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content) self.assertEqual(deserialized_data, expected) def test_article_by_tags_api(self): expected = [self.article1.__data__()] response = views.articles_by_tag( self.factory.get('/rss/tags/Tag1/articles/'), 'Tag1') self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_articles_search(self): test_tuples = [ # Articles search should search in articles title ([self.article1.__data__()], 'title1'), ([self.article2.__data__()], 'title2'), ([self.article2.__data__(), self.article1.__data__()], 'title'), # Articles search should search in articles summary ([self.article1.__data__()], 'summary1'), ([self.article2.__data__()], 'summary2'), ([self.article2.__data__(), self.article1.__data__()], 'summary'), # Articles search should search in articles content ([self.article1.__data__()], 'content1'), ([self.article2.__data__()], 'content2'), ([self.article2.__data__(), self.article1.__data__()], 'content'), # Articles search should search in articles tags ([self.article1.__data__()], 'tag1'), ([self.article2.__data__()], 'tag2'), ([self.article2.__data__(), self.article1.__data__()], 'tag'), # Articles search should search in articles authors ([self.article2.__data__(), self.article1.__data__()], 'author1'), ([self.article1.__data__()], 'author2'), ([self.article2.__data__(), self.article1.__data__()], 'author') ] for expected, term in test_tuples: self.article_search_test(expected, term) def article_search_test(self, expected, term): endpoint = '/rss/articles/search/%s/' % term response = views.articles_search(self.factory.get(endpoint), term) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected)