Esempio n. 1
0
 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
Esempio n. 2
0
 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
Esempio n. 3
0
def show(id):
    tweet = tweet_repository.get(id)
    return jsonify({
        "id": tweet.id,
        "text": tweet.text,
        "created_at": tweet.created_at
    })
Esempio n. 4
0
 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
Esempio n. 5
0
    def get(self, id):
        tweet = tweet_repository.get(id)

        if tweet is None:
            api.abort(404)
        else:
            return tweet
Esempio n. 6
0
    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))
Esempio n. 7
0
    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
Esempio n. 8
0
    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)
Esempio n. 9
0
 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
Esempio n. 10
0
 def delete(self, id):
     tweet = tweet_repository.get(id)
     if tweet is not None:
         tweet_repository.remove(id)
     return '', 204
Esempio n. 11
0
 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()
Esempio n. 12
0
 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))
Esempio n. 13
0
 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