Skip to content

Commit

Permalink
add feature for returning all movies
Browse files Browse the repository at this point in the history
  • Loading branch information
jburget committed Jun 10, 2023
1 parent a4c043e commit 0305ce4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ def get_movie(id):
return {key: result[key] for key in result.keys()}


@app.get('/movies')
def get_movies():
app.logger.debug("Retrieving all movies")
db_curr = db.get_db().cursor()
result = None
try:
db_curr.execute("SELECT * FROM movies")
result = db_curr.fetchall()
except Exception as e:
app.logger.error(e, exc_info=True)
flask.abort(HTTPStatus.INTERNAL_SERVER_ERROR)
finally:
db_curr.close()

if result is None:
return []
return [{key: row[key] for key in row.keys()} for row in result]


@app.put('/movies/<int:id>')
def update_movie(id):
app.logger.debug("Updating movie with id: %s", id)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ def test_invalid_movie_update(client, movie):
movie[Fields.release_year] = "1999"
response = client.put(f"/movies/{movie[Fields.id]}", json=movie)
assert response.status_code == HTTPStatus.BAD_REQUEST


def test_get_all_movies(client, movie):
expected_list = []
for _ in range(10):
response = client.post(f"/movies", json=movie)
assert response.status_code == HTTPStatus.OK
movie[Fields.id] = response.get_json().get(Fields.id)
expected_list.append(movie.copy())

response = client.get("/movies")
assert response.status_code == HTTPStatus.OK
assert response.get_json() == expected_list

0 comments on commit 0305ce4

Please sign in to comment.