def setUpFactories(self): """This runs when the test class first starts up. This does not run before every test case. Use this method to set your database up. """ User.create({ 'username': '******', 'email': '*****@*****.**', 'password': password('secret'), 'bio': 'This is a bio', 'image': '', 'token': None }) User.create({ 'username': '******', 'email': '*****@*****.**', 'password': password('secret') }) Follow.first_or_create(user_id=1, follower_id=2) Article.create({ 'title': 'this is a title', 'slug': 'this-is-a-title', 'description': 'This is a description', 'body': 'this is a body', 'author_id': 1 }) Tag.create({'name': 'Python'})
def unfavorite(self, slug): article = Article.where('slug', slug).first() if article: favorite = article.favorites.where('user_id', self.request.user().id).first() favorite.delete() article = Article.with_('author', 'favorites').where('slug', self.request.param('slug')).first() return {'article': article.payload(self.request.user())}
def favorite(self, slug): article = Article.where('slug', slug).first() favorite = Favorite( user_id=self.request.user().id ) article.favorites().save(favorite) article = Article.with_('author', 'favorites').where('slug', slug).first() return {'article': article.payload(self.request.user())}
def test_can_show_single_json(self): Article.create({ 'title': 'this article', 'slug': 'this-article', 'description': 'This is a description', 'body': 'this is a body', 'author_id': 1 }) self.assertTrue( self.json('GET', '/api/articles/this-article').hasJson( 'article.slug', 'this-article'))
def update(self, slug): article = Article.with_('author').where('slug', slug).first() article.update(self.request.input('article')) if self.request.input('article.tagList'): article.save_tags(self.request.input('article.tagList'), 'update') return {'article': article.payload(self.request.user())}
def index(self): articles = Article.with_('author', 'favorites') if self.request.has('author'): user = User.where('username', self.request.input('author')).first() if user: articles.where('author_id', user.id) if self.request.has('favorited'): user = User.where('username', self.request.input('favorited')).first() if user: articles.where_in('id', user.favorites.pluck('article_id')) if self.request.has('tag'): tag = Tag.where('name', self.request.input('tag')).first() if tag: articles.where_in('id', tag.articles.pluck('id')) articles = articles.order_by('created_at', 'desc').paginate( self.request.input('limit', 20), self.request.input('offset', 0) ) list_of_articles = [article.payload(self.request.user()) for article in articles] return {'articles': list_of_articles, 'articlesCount': articles.count()}
def index(self, request: Request): article = Article.with_('comments.author').where( 'slug', request.param('slug')).first() comments = [] for comment in article.comments: comments.append(comment.payload()) return {'comments': comments}
def test_can_create_article(self): self.assertTrue(self.get('/api/articles').hasJson('articlesCount', 1)) self.actingAs(User.find(1)).json( 'POST', '/api/articles', { "article": { "slug": "how-to-train-your-dragon", "title": "How to train your dragon", "description": "Ever wonder how?", "body": "It takes a Jacobian", "tagList": ["dragons", "training"], "createdAt": "2016-02-18T03:22:56.637Z", "updatedAt": "2016-02-18T03:48:35.824Z", "favorited": False, "favoritesCount": 0, "author": { "username": "******", "bio": "I work at statefarm", "image": "https://i.stack.imgur.com/xHWG8.jpg", "following": False } } }) self.assertTrue(Article.all().count())
def test_article_can_be_deleted(self): Article.create({ 'title': 'delete this article', 'slug': 'delete-this-article', 'description': 'This is a description', 'body': 'this is a body', 'author_id': 1 }) article = Article.where('slug', 'delete-this-article').first() self.assertEqual(article.slug, 'delete-this-article') self.actingAs(User.find(1)).delete('/api/articles/delete-this-article') article = Article.where('slug', 'delete-this-article').first() self.assertFalse(article)
def delete(self, slug): article = Article.where('slug', slug).first() if article: article.tags().detach(article.tags.lists('id')) article.delete() return self.request.status(204) return {'error': 'Article does not exist'}
def feed(self): users = self.user.followed_users().lists('user_id').serialize() articles = Article.with_('author', 'favorites').where_in('author_id', users) if articles: articles = articles.order_by('created_at', 'desc').paginate( self.request.input('limit'), self.request.input('offset') ) list_of_articles = [article.payload(self.user) for article in articles] return {'articles': list_of_articles, 'articlesCount': len(list_of_articles)}
def test_article_can_update(self): article = Article.find(1) self.assertEqual(article.slug, 'this-is-a-title') self.assertEqual(article.tags.count(), 0) self.actingAs(User.find(1)).json( 'PUT', '/api/articles/this-is-a-title', { "article": { "slug": "this-is-another-slug", "title": "How to train your dragon", "description": "Dragons", "body": "I Love Dragons", "tagList": ['kids', 'adults'] } }) article = Article.find(1) self.assertEqual(article.slug, 'this-is-another-slug') self.assertEqual(article.tags.count(), 2)
def create(self, request: Request, validator: Validator, validate: Validator): comment_data = request.input('comment') errors = validator.validate( comment_data, validate.required(['body']), ) if errors: request.status(422) return {'errors': errors} article = Article.where('slug', request.param('slug')).first() comment = Comment(body=comment_data['body'], author_id=request.user().id) article.comments().save(comment) request.status(201) return {'comment': comment.payload()}
def create(self, validator: Validator, validate: Validator): article_data = self.request.input('article') errors = self.request.validate( validate.required(['article.title', 'article.description', 'article.body']), ) if errors: self.request.status(422) return {'errors': errors} article = Article() article.slug=slugify(article_data['title']) article.title=article_data['title'] article.description=article_data['description'] article.body=article_data['body'] article.author_id = self.request.user().id article.save() article.save_tags(self.request.input('article')['tagList'], 'create') article = Article.with_('author', 'tags').find(article.id) self.request.status(201) return {'article': article.payload(self.request.user())}
def show(self, slug): article = Article.with_('author').where('slug', slug).first() return {'article': article.payload(self.request.user())}