def sample_tweets(client): tweet1 = Tweet(party="GOP", person="Trump", tweet="Bigly") db.session.add(tweet1) tweet2 = Tweet(party="GOP", person="Trump", tweet="Covfefe") db.session.add(tweet2) db.session.commit() return [tweet1, tweet2]
def test_tweets_index_non_empty(self): first_tweet = Tweet(text="First tweet") second_tweet = Tweet(text="Second tweet") db.session.add_all([first_tweet, second_tweet]) db.session.commit() response = self.client.get("/tweets") response_tweets = response.json self.assertEqual(len(response_tweets), 2)
def test_auto_increment_after_adding_a_tweet(self): tweet_repository = TweetRepository() tweet = Tweet("RAPH") tweet2 = Tweet("JOHN") tweet_repository.add(tweet) tweet_repository.add(tweet2) self.assertEqual(tweet.id, 1) self.assertEqual(tweet2.id, 2)
def setUp(self): db.create_all() first_tweet = Tweet(text="First tweet") db.session.add(first_tweet) db.session.commit() second_tweet = Tweet(text="Secound tweet") db.session.add(second_tweet) db.session.commit()
def test_increment_id(self): repository = Repository() tweet1 = Tweet("Ah que coucou.") repository.add(tweet1) self.assertEqual(tweet1.id, 1) tweet2 = Tweet("Tirelipimpon sur le Chihuahua.") repository.add(tweet2) self.assertEqual(tweet2.id, 2)
def test_auto_increment_of_ids(self): repository = TweetRepository() first_tweet = Tweet("a first tweet") repository.add(first_tweet) self.assertEqual(first_tweet.id, 1) second_tweet = Tweet("a second tweet") repository.add(second_tweet) self.assertEqual(second_tweet.id, 2)
def test_tweet_show_all(self): first_tweet = Tweet(text="First tweet") db.session.add(first_tweet) second_tweet = Tweet(text="First tweet") db.session.add(second_tweet) db.session.commit() response = self.client.get("/tweets") response_tweet = response.json print(response_tweet) self.assertEqual(len(response_tweet), 2)
def test_all_tweets(self): first_tweet = Tweet("First tweet") second_tweet = Tweet("Second tweet") db.session.add(first_tweet) db.session.add(second_tweet) db.session.commit() response = self.client.get("/tweets") self.assertIn("200", response.status) tweets = response.json self.assertEqual(len(tweets), 2) self.assertEqual(tweets[0]["text"], "First tweet") self.assertEqual(tweets[1]["text"], "Second tweet")
def test_get_all_tweets(self): user = User(username='******') first_tweet = Tweet(text="First tweet", user=user) second_tweet = Tweet(text="Second tweet", user=user) db.session.add(first_tweet) db.session.add(second_tweet) db.session.commit() response = self.client.get("/tweets") tweets = response.json self.assertEqual(type(tweets), list) self.assertEqual(tweets[0]['text'], "First tweet") self.assertEqual(tweets[0]['user']['username'], "ssaunier") self.assertEqual(tweets[1]['text'], "Second tweet") self.assertEqual(tweets[1]['user']['username'], "ssaunier")
def test_api_can_retweet_a_status(self): '''Test API can retweet a Status''' status_to_retweet = {'text': "Hahaha retweet this i dare you"} user = { 'username': "******", 'email': "*****@*****.**", 'password': "******" } with self.app.app_context(): u = User(email=user['email'], password_hash=user['password'], username=user['username'], verified=True) u.insert() s = Tweet(text=status_to_retweet['text'], user=u) s.insert() rv = self.client().post( '/statuses/retweet', data=json.dumps({'id': s.id}), content_type='application/json', headers={'x-access-token': self.auth_token}) result_in_json = json.loads( rv.data.decode('utf-8').replace("'", "\"")) self.assertEqual(rv.status_code, 201) self.assertIn('success', str(result_in_json['data'])) self.client().post( '/statuses/reply', data=json.dumps({'id': s.id, 'text': 'Hahaha now am gonna also reply'}), content_type='application/json', headers={'x-access-token': self.auth_token})
def test_tweet_delete(self): first_tweet = Tweet(text="First tweet") db.session.add(first_tweet) db.session.commit() self.client.delete("/tweets/1") response = self.client.get("/tweets/1") self.assertEqual(response.status, "404 NOT FOUND")
def test_get_found(self): r = TweetRepository() r.add(Tweet("Welcome")) tweet = r.get(0) #print(tweet) self.assertEqual(tweet.text, "Welcome") self.assertEqual(tweet.id, 0)
def test_tweet_delete_apikey_KO(self): first_tweet = Tweet("First tweet") first_tweet.user = self.user db.session.add(first_tweet) db.session.commit() response = self.client.delete("/tweets/1") self.assertIn("401", response.status)
def post(self): # POST method # 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 text = api.payload['text'] tweet = Tweet(text) tweet_repository.add(tweet) return tweet, 201
def _process_muse(muse): """ Processes a Muse's tweets, creating Tweet objects and saving them to the db. """ username = muse.username logger.info('Collecting tweets for %s...' % username) try: tweets = twitter.tweets(username=username) except TweepError: return [] new_tweets = [] for tweet in tweets: data = { 'body': tweet['body'], 'tid': tweet['tid'], 'username': username } t = Tweet(**data) try: t.save() new_tweets.append(tweet) except (NotUniqueError, DuplicateKeyError, OperationError): # Duplicate tweet pass return new_tweets
def test_tweet_delete(self): first_tweet = Tweet() first_tweet.text = "First tweet" db.session.add(first_tweet) db.session.commit() self.client.delete("/tweets/1") self.assertIsNone(db.session.query(Tweet).get(1))
def api_tweet(): try: data = json.loads( request.data, object_hook=lambda x: defaultdict(lambda: None, x) ) except JSONDecodeError: return error_response(["Invalid request."]) tweet_text = data["text"] or "" tweet_text = tweet_text.strip().replace("\r\n", "\n") tweet_len = len(tweet_text) if tweet_len < 1: return error_response(["Your tweet must be at least one character."]) if tweet_len > 120: return error_response(["Your tweet must be at most 120 characters."]) image_id = data["imageId"] if image_id is not None: image_exists = Image.query.filter_by(id=image_id).count() > 0 if not image_exists: return error_response(["Image not found"]) tweet = Tweet( text=tweet_text, poster_id=int(current_user.get_id()), image_id=image_id ) db.session.add(tweet) db.session.commit() return success_response(tweet.to_dict())
def db_import(filename): print(filename) basedir = os.path.abspath(os.path.dirname(__file__)) import_file = os.path.join(basedir, 'static/import/' + filename + '.csv') log_file = os.path.join(basedir, 'log/err_' + filename + '.log') tID_column = 1 given_text_column = 2 #print("Pre: ", Tweet.query.all()) with open(import_file) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for row in csv_reader: #Nonunique tID handler if Tweet.query.get(int(row[tID_column])): print("<Tweet " + row[tID_column] + "> was found in the database already" + time.time(), file=open(log_file, "a")) new_tweet = Tweet( tID=int(row[tID_column]), given_text=row[given_text_column], selected_text="", primary_sent=-1, secondary_sent=-1, ) print(new_tweet) db.session.add(new_tweet) db.session.commit() #print("Post: ", Tweet.query.all()) return redirect('/')
def post(self): new_value = message_parser.parse_args()['message'] result = tweet_repository.add(Tweet(new_value)) if result is None: api.abort(404) else: return result, 201
def test_tweet_update(self): first_tweet = Tweet(text="First tweet") db.session.add(first_tweet) response = self.client.patch("/tweets/1", json={'text': 'New text'}) updated_tweet = response.json self.assertEqual(response.status_code, 200) self.assertEqual(updated_tweet["id"], 1) self.assertEqual(updated_tweet["text"], "New text")
def test_tweet_show(self): first_tweet = Tweet("First tweet") tweet_repository.add(first_tweet) response = self.client.get("/tweets/1") response_tweet = response.json self.assertEqual(response_tweet["id"], 1) self.assertEqual(response_tweet["text"], "First tweet") self.assertIsNotNone(response_tweet["created_at"])
def post(self): text = api.payload["text"] if len(text) > 0: tweet = Tweet(text) tweet_repository.add(tweet) return tweet, 201 else: return abort(422, "Tweet text can't be empty")
def post_tweet(): form = TweetForm() if form.validate(): tweet = Tweet(user_id=current_user.id, text=form.text.data, date_created=datetime.now()).save() return redirect(url_for('timeline')) return 'Something went wrong'
def post(self): text = api.payload["text"] if len(text) > 0: tweet = Tweet(text=text) db.session.add(tweet) db.session.commit() return tweet, 201 else: return abort(422, "Tweet text can't be empty")
def test_tweets_show(self): first_tweet = Tweet() first_tweet.id = 1 first_tweet.text = "First tweet" db.session.add(first_tweet) second_tweet = Tweet() second_tweet.id = 2 second_tweet.text = "Second tweet" db.session.add(second_tweet) db.session.commit() response = self.client.get("/tweets") response_tweets = response.json print(response_tweets) self.assertEqual(len(response_tweets), 2)
def test_delete_one_tweet(self): tweet_to_delete = Tweet(text='A tweet') db.session.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(db.session.query(Tweet).get(1))
def test_instance_variables(self): # Create an instance of the `Tweet` class with one argument tweet = Tweet("my first tweet") # Check that `text` holds the content of the tweet self.assertEqual(tweet.text, "my first tweet") # Check that when creating a new `Tweet` instance, its `created_at` date gets set self.assertIsNotNone(tweet.created_at) # Check that the tweet's id is not yet assigned when creating a Tweet in memory self.assertIsNone(tweet.id)
def reply_status(current_user): data = request.get_json() s = Tweet.query.filter_by(id=data['id']).first() if not s: return jsonify({'data': 'Resource not found', 'error': None}), 404 reply = Tweet(text=data['text'], user=current_user, in_reply_to_status=s) reply.insert() db.session.commit() return jsonify({'data': 'success', 'error': None}), 201
def test_tweet_show(self): first_tweet = Tweet(text="First tweet") db.session.add(first_tweet) db.session.commit() response = self.client.get("/tweets/1") response_tweet = response.json self.assertEqual(response_tweet["id"], 1) self.assertEqual(response_tweet["text"], "First tweet") self.assertIsNotNone(response_tweet["created_at"])
def test_repository_populate(self): # Create an instance of the `Repository` repository = TweetRepository() for i in range(1, 4): tweet = Tweet(f"my tweet #{i}") tweet_id = repository.add(tweet) self.assertEqual(tweet_id, i) self.assertIsInstance(repository.get(1), Tweet) self.assertEqual(len(repository.tweets), 3)