def test_get_all_movies_multiple_entry(self): movieA = dummy_movie() movieB = dummy_movie(title="test2") db.session.add(movieA) db.session.add(movieB) db.session.commit() movies = byte_to_json(self.get_all_movies().data)['movies'] self.assertEqual(movies[0], MovieSchema().dump(movieA)) self.assertEqual(movies[1], MovieSchema().dump(movieB))
def insertMovie(): try: result = request_to_schema(request) result = MovieSchema().load(result) if not isinstance(result, Movie): return result, 400 result = MovieOperations.add_movie(result) return MovieSchema().dump(result), 200 except Exception as e: return {"msg": "Movie could not be added: {}".format(e)}, 400
def deleteMovie(): title = request.json.get("title", None) if not isinstance(title, str): return {"msg": "Missing title parameter"}, 400 result = MovieOperations.del_movie(title) if not isinstance(result, Movie): return result return MovieSchema().dump(result), 200
def expiredMovies(): fields = {i: request.json.get(i, None) for i in ["from", "to"]} for (field, value) in fields.items(): if value is None: return {"msg": "Field {} is Missing".format(field)}, 400 if fields["from"] > fields["to"]: return {"msg": "Query invalid, 'from' date is after 'to' date."}, 400 query = MovieOperations.query_expired_movies(fields["from"], fields["to"]) return {"movies": [MovieSchema().dump(movie) for movie in query]}, 200
def insert_movie(self, movie=dummy_movie()): jwt = byte_to_json(self.get_auth_point())['access_token'] result = self.client.post("/api/", headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(jwt) }, data=MovieSchema().dumps(movie)) return result
def test_search_title(self): movie = dummy_movie() movie_to_not_find = dummy_movie(title="somethingElse") db.session.add(movie) db.session.add(movie_to_not_find) db.session.commit() result = self.search_movie({'title': movie.title}) self.assert200(result) movies = byte_to_json(result.data)['movies'] self.assertEquals(json.dumps(movies[0], sort_keys=True), json.dumps(MovieSchema().dump(movie), sort_keys=True))
def request_to_schema(request, toSearch=False): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 fields = ['title', 'genre', 'expiry_date', 'release_year'] args = {} schema = MovieSchema() if not toSearch else MovieSearch() for field in fields: arg = request.json.get(field, None) if not arg and not toSearch: return jsonify({"msg": "Missing {} parameter".format(field)}), 400 if field is 'expiry_date' and arg != None: #This is a nightmare someone needs to make this a library arg = dt.datetime( *[int(num) for num in re.compile("[/\:-]").split(arg)]) args[field] = arg try: result = schema.dump(args) except ValidationError as e: return jsonify({"msg": "Errors with request body: {}".format(e)}), 400 return result
def test_search_multi_fields(self): title_to_search = "find me" genre_to_search = "find me too" movie_to_search = dummy_movie(title=title_to_search, genre=genre_to_search) movie_to_not_find = dummy_movie(title="somethingElse") db.session.add(movie_to_search) db.session.add(movie_to_not_find) db.session.commit() result = self.search_movie({'title': title_to_search, 'genre': genre_to_search}) self.assert200(result) movies = byte_to_json(result.data)['movies'] self.assertEquals(json.dumps(movies[0], sort_keys=True), json.dumps(MovieSchema().dump(movie_to_search), sort_keys=True))
def test_insert_multiple(self): movies = [dummy_movie(), dummy_movie(title="test2")] for movie in movies: result = self.insert_movie(movie) self.assert200(result) movies_found = byte_to_json(self.get_all_movies().data)['movies'] for i, movie in enumerate(movies): self.assertEquals(json.dumps(movies_found[i], sort_keys=True), json.dumps(MovieSchema().dump(movie), sort_keys=True))
def test_get_all_movies_with_entry(self): movie = dummy_movie() db.session.add(movie) db.session.commit() movies = byte_to_json(self.get_all_movies().data)['movies'] self.assertEquals(movies[0], MovieSchema().dump(movie))
def searchMovies(): search = request_to_schema(request, toSearch=True) query = MovieOperations.search_movie(search) return {"movies": [MovieSchema().dump(movie) for movie in query]}, 200
def greeting(): query = MovieOperations.get_all() return {"movies": [MovieSchema().dump(movie) for movie in query]}, 200
def movie_dump(movie: Movie): return MovieSchema().dump(movie)