def test_can_get_all_markets(db, cleanup): market_id = 123456 market_in = MarketCreate(market_id=market_id) market = crud.create_market(db, market=market_in) market_id2 = 234567 market_in2 = MarketCreate(market_id=market_id2) market2 = crud.create_market(db, market=market_in2) response = client.get("/markets") assert response.status_code == 200 resp = response.json() assert resp == [{ 'market_id': 123456, 'id': 1 }, { 'market_id': 234567, 'id': 2 }]
def test_create_user_favorites(db, cleanup): email = "*****@*****.**" password = "******" market_id = 1 market_id2 = 2 user_in = UserCreate(email=email, password=password) market_in = MarketCreate(market_id=market_id) market_in2 = MarketCreate(market_id=market_id2) user = crud.create_user(db, user=user_in) market = crud.create_market(db, market=market_in) market2 = crud.create_market(db, market=market_in2) favorite_in = FavoriteCreate(user_id=user.id, market_id=market.id) favorite_in2 = FavoriteCreate(user_id=user.id, market_id=market2.id) favorite = crud.favorite_market(db, favorite=favorite_in) favorite2 = crud.favorite_market(db, favorite=favorite_in2) assert favorite.user_id == user.id assert favorite.market_id == market.id assert len(user.favorites) == 2 assert user.favorites[0] == market assert user.favorites[1] == market2
def test_it_can_get_all_user_favorites(db, cleanup): market_id = 123456 market_in = MarketCreate(market_id=market_id) market = crud.create_market(db, market=market_in) email = "*****@*****.**" password = "******" user_in = UserCreate(email=email, password=password) user = crud.create_user(db, user=user_in) client.post(f"/users/{user.id}/favorites", json={"fmid": market_id}) response = client.get(f"/users/{user.id}/favorites") assert response.status_code == 200 resp = response.json() assert resp['favorites'] == [{'market_id': 123456, 'id': 1}]
def test_it_cant_create_a_favorite_if_user_already_has_one(db, cleanup): market_id = 123 email = "*****@*****.**" password = "******" user_in = UserCreate(email=email, password=password) user = crud.create_user(db, user=user_in) market_in = MarketCreate(market_id=market_id) market = crud.create_market(db, market=market_in) favorite_in = FavoriteCreate(user_id=user.id, market_id=market.id) favorite = crud.favorite_market(db, favorite=favorite_in) response = client.post(f"/users/{user.id}/favorites", json={"fmid": market_id}) assert response.status_code == 400 resp = response.json() assert resp['detail'] == 'That market is already favorited!'
def favorite_a_market(user_id: int, fmid: int = Body(..., embed=True), db: Session = Depends(get_db)): db_market = crud.get_market_by_fmid(db, fmid=fmid) if db_market is None: market_in = schemas.MarketCreate(market_id=fmid) db_market = crud.create_market(db, market=market_in) elif db_market: db_fav = crud.get_favorite(db, user_id=user_id, market_id=fmid) if db_fav: raise HTTPException(status_code=400, detail="That market is already favorited!") favorite_in = schemas.FavoriteCreate(user_id=user_id, market_id=db_market.id) favorite = crud.favorite_market(db, favorite=favorite_in) user = crud.get_user(db, user_id=user_id) return user
def test_it_can_delete_user_favorites(db, cleanup): market_id = 123456 market_in = MarketCreate(market_id=market_id) market = crud.create_market(db, market=market_in) email = "*****@*****.**" password = "******" user_in = UserCreate(email=email, password=password) user = crud.create_user(db, user=user_in) client.post(f"/users/{user.id}/favorites", json={"fmid": market_id}) response = client.get(f"/users/{user.id}") user_resp = response.json() assert len(user_resp['favorites']) == 1 response2 = client.delete(f"/users/{user.id}/favorites", json={"fmid": market_id}) user_resp2 = response2.json() assert response2.status_code == 200 assert user_resp2['favorites'] == [] assert len(user_resp2['favorites']) == 0
def test_create_market(db, cleanup): market_id = 1 market_in = MarketCreate(market_id=market_id) market = crud.create_market(db, market=market_in) assert market.market_id == 1