From 804d6fc6b9268577dbfd85f169f1a3f58e5cd6bc Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Fri, 24 Apr 2015 19:05:14 +0200 Subject: adding base unittests --- tests/__init__.py | 0 tests/base.py | 41 +++++++++++++++++++++++++++++++++++++++++ tests/controllers/__init__.py | 5 +++++ tests/controllers/article.py | 12 ++++++++++++ tests/controllers/feed.py | 15 +++++++++++++++ tests/fixtures.py | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/base.py create mode 100644 tests/controllers/__init__.py create mode 100644 tests/controllers/article.py create mode 100644 tests/controllers/feed.py create mode 100644 tests/fixtures.py (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 00000000..d6f62583 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,41 @@ +import os +os.environ['PYAGG_TESTING'] = 'true' + +import unittest +from bootstrap import db +import runserver +from tests.fixtures import populate_db, reset_db +from werkzeug.exceptions import NotFound + + +class BasePyaggTest(unittest.TestCase): + _contr_cls = None + + def _get_from_contr(self, obj_id, user_id=None): + return self._contr_cls(user_id).get(id=obj_id).dump() + + def _test_controller_rights(self, obj, user_id): + obj_id = obj['id'] + self.assertEquals(obj, self._get_from_contr(obj_id)) + self.assertEquals(obj, self._get_from_contr(obj_id, user_id)) + # fetching non existent object + self.assertRaises(NotFound, self._get_from_contr, 99, user_id) + # fetching object with inexistent user + self.assertRaises(NotFound, self._get_from_contr, obj_id, 99) + # fetching object with wrong user + self.assertRaises(NotFound, self._get_from_contr, obj_id, user_id + 1) + self.assertRaises(NotFound, self._contr_cls().delete, 99) + self.assertRaises(NotFound, self._contr_cls(user_id).delete, 99) + self.assertEquals(obj['id'], + self._contr_cls(user_id).delete(obj_id).id) + self.assertRaises(NotFound, self._contr_cls(user_id).delete, obj_id) + + def setUp(self): + populate_db(db) + + def tearDown(self): + reset_db(db) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/controllers/__init__.py b/tests/controllers/__init__.py new file mode 100644 index 00000000..26922c43 --- /dev/null +++ b/tests/controllers/__init__.py @@ -0,0 +1,5 @@ +from tests.controllers.feed import FeedControllerTest +from tests.controllers.article import ArticleControllerTest + + +__all__ = ['FeedControllerTest', 'ArticleControllerTest'] diff --git a/tests/controllers/article.py b/tests/controllers/article.py new file mode 100644 index 00000000..b6c5225c --- /dev/null +++ b/tests/controllers/article.py @@ -0,0 +1,12 @@ +from tests.base import BasePyaggTest +from pyaggr3g470r.controllers import ArticleController + + +class ArticleControllerTest(BasePyaggTest): + _contr_cls = ArticleController + + def test_controller(self): + article = ArticleController(2).read()[0].dump() + self.assertFalse(article['readed']) + article['readed'] = True # article get read when retreived through get + self._test_controller_rights(article, article['user_id']) diff --git a/tests/controllers/feed.py b/tests/controllers/feed.py new file mode 100644 index 00000000..d8d6378b --- /dev/null +++ b/tests/controllers/feed.py @@ -0,0 +1,15 @@ +from tests.base import BasePyaggTest +from pyaggr3g470r.controllers import FeedController +from pyaggr3g470r.controllers import ArticleController + + +class FeedControllerTest(BasePyaggTest): + _contr_cls = FeedController + + def test_controller(self): + feed = FeedController(2).read()[0].dump() + self.assertTrue(3, ArticleController().read(feed_id=feed['id']).count()) + self._test_controller_rights(feed, feed['user_id']) + # checking articles are deleted after the feed has been deleted + self.assertFalse(0, + ArticleController().read(feed_id=feed['id']).count()) diff --git a/tests/fixtures.py b/tests/fixtures.py new file mode 100644 index 00000000..bc783847 --- /dev/null +++ b/tests/fixtures.py @@ -0,0 +1,32 @@ +from pyaggr3g470r.models import db_create, db_empty, User, Article, Feed + + +def populate_db(db): + role_admin, role_user = db_create(db) + user1, user2 = [User(nickname=name, email="%s@test.te" % name, + pwdhash=name, roles=[role_user], activation_key="") + for name in ["user1", "user2"]] + db.session.add(user1) + db.session.add(user2) + db.session.commit() + + for user in (user1, user2): + for feed_name in ['feed1', 'feed2', 'feed3']: + feed = Feed(link=feed_name, user_id=user.id, + title="%r %r" % (user.nickname, feed_name)) + db.session.add(feed) + db.session.commit() + for article in ['article1', 'article2', 'article3']: + article = Article(entry_id=article, link=article, + feed_id=feed.id, user_id=user.id, + title="%r %r %r" % (user.nickname, + feed.title, article), + content=article) + db.session.add(article) + db.session.commit() + + db.session.commit() + + +def reset_db(db): + db_empty(db) -- cgit From b6603ce4607982590d7d55c0d583809192d84cad Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Fri, 15 May 2015 00:21:40 +0200 Subject: testing special controller methods --- tests/controllers/article.py | 11 +++++++++++ tests/controllers/feed.py | 12 +++++++++++- tests/fixtures.py | 7 +++---- 3 files changed, 25 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/controllers/article.py b/tests/controllers/article.py index b6c5225c..9a635fa7 100644 --- a/tests/controllers/article.py +++ b/tests/controllers/article.py @@ -10,3 +10,14 @@ class ArticleControllerTest(BasePyaggTest): self.assertFalse(article['readed']) article['readed'] = True # article get read when retreived through get self._test_controller_rights(article, article['user_id']) + self.assertEquals(0, len(list(ArticleController().challenge( + [{'id': art.id} for art in ArticleController(3).read()])))) + self.assertEquals(9, len(list(ArticleController(2).challenge( + [{'id': art.id} for art in ArticleController(3).read()])))) + self.assertEquals(9, len(list(ArticleController(2).challenge( + [{'entry_id': art.id} for art in ArticleController(3).read()] + )))) + self.assertEquals({1: 2, 2: 3, 3: 3}, + ArticleController(2).get_unread()) + self.assertEquals({4: 3, 5: 3, 6: 3}, + ArticleController(3).get_unread()) diff --git a/tests/controllers/feed.py b/tests/controllers/feed.py index d8d6378b..924b8ddd 100644 --- a/tests/controllers/feed.py +++ b/tests/controllers/feed.py @@ -8,8 +8,18 @@ class FeedControllerTest(BasePyaggTest): def test_controller(self): feed = FeedController(2).read()[0].dump() - self.assertTrue(3, ArticleController().read(feed_id=feed['id']).count()) + self.assertTrue(3, + ArticleController().read(feed_id=feed['id']).count()) self._test_controller_rights(feed, feed['user_id']) # checking articles are deleted after the feed has been deleted self.assertFalse(0, ArticleController().read(feed_id=feed['id']).count()) + + self.assertEquals(3, len(FeedController(3).list_fetchable())) + self.assertEquals(0, len(FeedController(3).list_fetchable())) + self.assertEquals(2, len(FeedController().list_fetchable())) + self.assertEquals(0, len(FeedController().list_fetchable())) + self.assertEquals(3, + len(FeedController(3).list_fetchable(refresh_rate=0))) + self.assertEquals(5, + len(FeedController().list_fetchable(refresh_rate=0))) diff --git a/tests/fixtures.py b/tests/fixtures.py index bc783847..6aab645d 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -17,11 +17,10 @@ def populate_db(db): db.session.add(feed) db.session.commit() for article in ['article1', 'article2', 'article3']: - article = Article(entry_id=article, link=article, + entry = "%s %s %s" % (user.nickname, feed.title, article) + article = Article(entry_id=entry, link=article, feed_id=feed.id, user_id=user.id, - title="%r %r %r" % (user.nickname, - feed.title, article), - content=article) + title=entry, content=article) db.session.add(article) db.session.commit() -- cgit