def other_user(user_data, like_data, dislike_data, db_session): user_id = helper.add_user({**user_data, "email": "*****@*****.**"})["id"] helper.add_like({**like_data, "user_id": user_id}) helper.add_dislike({**dislike_data, "user_id": user_id}) user = db_session.query(User).get(user_id) yield user pass
def current_user(user_data, like_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] helper.add_like({**like_data, "user_id": user_id}) helper.add_dislike({**dislike_data, "user_id": user_id}) user = db_session.query(User).get(user_id) old_current_user = me.current_user me.current_user = user yield user me.current_user = old_current_user
def test_add_dislike_missing_data(user_data, dislike_data, db_session): assert db_session.query(DisLike).all() == [] user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id # Missing user_id with pytest.raises(_SchemaLoadError): helper.add_dislike({"place_id": dislike_data["place_id"]}) assert db_session.query(DisLike).all() == [] # Missing place_id with pytest.raises(_SchemaLoadError): helper.add_dislike({"user_id": dislike_data["user_id"]}) assert db_session.query(DisLike).all() == []
def test_add_dislike(user_data, dislike_data, db_session): assert db_session.query(DisLike).all() == [] user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id dislike_id = helper.add_dislike(dislike_data)["id"] assert db_session.query(DisLike).get(dislike_id)
def test_delete_dislike(user_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id dislike_id = helper.add_dislike(dislike_data)["id"] assert db_session.query(DisLike).get(dislike_id) helper.delete_dislike(dislike_id) assert db_session.query(DisLike).get(dislike_id) is None
def fill_db(likes, dislikes): user_data = dict( first_name="United", last_name="Remote", email="*****@*****.**", password="******" ) user_id = helper.add_user(user_data)["id"] likes = [dict(place_id=generate_place_id(), user_id=user_id) for _ in range(likes)] dislikes = [dict(place_id=generate_place_id(), user_id=user_id) for _ in range(dislikes)] [helper.add_like(like) for like in likes] [helper.add_dislike(dislike) for dislike in dislikes]
def add_dislike(): dislike_data = request.json if dislike_data: if "user_id" in dislike_data and dislike_data[ "user_id"] != current_user.id: raise _SecurityError( message="Attempt to temper with dislike user_id", data={ "user_id": current_user.id, "dislike_data": dislike_data }) dislike_data["user_id"] = current_user.id validation_errors = DisLikeSchema().validate(dislike_data) if validation_errors: return jsonify(validation_errors), 400 created_dislike = helper.add_dislike(dislike_data) return jsonify(created_dislike), 201 return jsonify({"message": "Empty json payload"}), 400
def test_fresh_dislikes(user_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id dislike_id = helper.add_dislike(dislike_data)["id"] user = db_session.query(User).get(user_id) dislike = db_session.query(DisLike).get(dislike_id) assert len(user.fresh_dislikes) == 1 assert user.fresh_dislikes[0].id == dislike_id # Update created_at dislike.created_at = datetime.now(tz=timezone.utc) - config.DISLIKE_EXPIRY db_session.add(dislike) db_session.commit() db_session.expunge(user) db_session.expunge(dislike) user = db_session.query(User).get(user_id) assert len(user.fresh_dislikes) == 0
def test_purge_dislikes_helpers(user_data, dislike_data, db_session): user_id = helper.add_user(user_data)["id"] dislike_data["user_id"] = user_id nbr_dislikes = 5 [helper.add_dislike(dislike_data)["id"] for _ in range(nbr_dislikes)] user = db_session.query(User).get(user_id) assert db_session.query(DisLike).count() == nbr_dislikes user._dislikes[0].created_at = datetime.now( tz=timezone.utc) - config.DISLIKE_EXPIRY user._dislikes[1].created_at = datetime.now( tz=timezone.utc) - config.DISLIKE_EXPIRY db_session.add(user._dislikes[0]) db_session.add(user._dislikes[1]) db_session.commit() helper.purge_dislikes() assert db_session.query(DisLike).count() == nbr_dislikes - 2