def patch(self, id): tweet = tweet_repository.get(id) if tweet is None: api.abort(404, "Tweet {} doesn't exist".format(id)) else: tweet.text = api.payload["text"] return tweet
def delete(self, id): tweet = tweet_repository.get(id) if tweet is None: api.abort(404, "Tweet {} doesn't exist".format(id)) else: tweet_repository.remove(id) return None
def show(id): tweet = tweet_repository.get(id) return jsonify({ "id": tweet.id, "text": tweet.text, "created_at": tweet.created_at })
def get(self, tweet_id): # GET method tweet = tweet_repository.get(tweet_id) if tweet is None: api.abort( 404 ) # abort will throw an exception and break execution flow (equivalent to 'return' keyword for an error) return tweet, 200
def get(self, id): tweet = tweet_repository.get(id) if tweet is None: api.abort(404) else: return tweet
def test_delete_one_tweet(self): tweet_to_delete = Tweet('A tweet') tweet_repository.add(tweet_to_delete) response = self.client.delete('/tweets/1') self.assertEqual(response.status_code, 204) # We use direct access to database to validate our operation self.assertIsNone(tweet_repository.get(1))
def patch(self, tweet_id): # PATCH method tweet = tweet_repository.get(tweet_id) if tweet is None: api.abort(404) # body is also called payload # No need to verify if 'text' is present in body, or if it is a valid string since we use validate=True # body has already been validated using json_new_tweet schema tweet.text = api.payload['text'] return None, 204
def test_update_one_tweet(self): tweet_to_update = Tweet('Tweet to update') tweet_repository.add(tweet_to_update) response = self.client.patch('/tweets/1', json={'text': 'New text'}) self.assertEqual(response.status_code, 204) # We use direct access to database to validate our operation # Database return Tweet instance, not json converted to a dict updated_tweet = tweet_repository.get(1) self.assertEqual(updated_tweet.id, 1) self.assertEqual(updated_tweet.text, 'New text') self.assertIsNotNone(updated_tweet.created_at)
def get(self, id): tweet = tweet_repository.get(id) if tweet is None: api.abort(404, "Tweet {} doesn't exist".format(id)) else: return tweet
def delete(self, id): tweet = tweet_repository.get(id) if tweet is not None: tweet_repository.remove(id) return '', 204
def get(self, id): tweet = tweet_repository.get(id) if tweet is None: api.abort(404, f"Tweet {id} doesn't exist") return tweet # tweet.as_dict()
def test_tweet_delete(self): first_tweet = Tweet("First tweet") tweet_repository.add(first_tweet) self.client.delete("/tweets/1") self.assertIsNone(tweet_repository.get(1))
def delete(self, tweet_id): # DELETE method tweet = tweet_repository.get(tweet_id) if tweet is None: api.abort(404) tweet_repository.remove(tweet_id) return None, 204