def setUpClass(cls): """Define test variables and initialize app.""" cls.header = { "Authorization": "Bearer " + "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik5FRkVPRE16UVVSRE16aENPVE" + "ZEUVRkR1FUVXpOVFpGTmtKRlJUbEZNemsyT1RWQ09FRTVRUSJ9.eyJpc3MiOiJodHRwczovL2" + "d3aWxsaWcuZXUuYXV0aDAuY29tLyIsInN1YiI6Imdvb2dsZS1vYXV0aDJ8MTE1NTY1NDgyODE" + "4Mjc4OTAxNTMwIiwiYXVkIjpbImNhc3RpbmdfYWdlbmN5IiwiaHR0cHM6Ly9nd2lsbGlnLmV1" + "LmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE1ODUwNzgzMTEsImV4cCI6MTU4NTE2NDY5O" + "CwiYXpwIjoiUVltdW9ha2hiUERqQW1SRFB5ZnBnTGlsemNwV0ZBQUsiLCJzY29wZSI6Im9wZW" + "5pZCBwcm9maWxlIGVtYWlsIiwicGVybWlzc2lvbnMiOlsiZGVsZXRlOmFjdG9yIiwiZGVsZXR" + "lOm1vdmllIiwiZ2V0OmFjdG9yIiwiZ2V0OmFjdG9ycyIsImdldDptb3ZpZSIsImdldDptb3Zp" + "ZXMiLCJwYXRjaDphY3RvciIsInBhdGNoOm1vdmllIiwicG9zdDphY3RvciIsInBvc3Q6bW92a" + "WUiXX0.aufLPeJXyJ5GTHj8c4uV6THJ_k8jXOmyN3XKVf-xEdJAQWoRyt_BbYo0ZZxdIzAaVY" + "JoN4GLAbruYN6lanUzA_Ms4enP_GsUYJUDLNxhb5IwU6BSecTysM736YfGW5s3xSi75ps7UqP" + "UNgrckYopUIQSfj2PMYyq-WByPbfhz4wfXFwK-PfkX_XMVAtQLzFjHrU7m35BKCmZ9JgwmsEk" + "B-YoDv904v1qr91JY2_X3GZQJKcmK-PyOW58Z-62zpp9X550Bx4lGnwGATZDlHiUOty66DABk" + "WH9XNZMMy2SzcOYSJwmXm46mc3BBpK9c9vO0osMpn1pAbvHI6_T7BDt_Q" } cls.app = create_app(dbms="sql", test_config=True) project_dir = os.path.dirname(os.path.abspath(__file__)) database_path = "sqlite:///{}".format( os.path.join(project_dir, "database_test.db")) cls.db = setup_db(cls.app, database_path) cls.client = cls.app.test_client dummy_actor1 = Actor(first_name="Max", family_name="Mustermann") dummy_movie1 = Movie(title="Movie XY") dummy_movie1.actors.append(dummy_actor1) cls.db.session.bulk_save_objects([ Actor(first_name="Gerald", family_name="Mustermann"), Actor(first_name="Albert", family_name="Mustermann"), Actor(first_name="Max", family_name="Mustermann"), Movie(title="Doe goes New York"), Movie(title="Tim goes New York"), dummy_movie1 ]) cls.db.session.commit()
def test_delete_movie(self): mid = Movie(**self.sample_movie).insert().id res = self.delete(f'/movies/{mid}') data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertIsNone(Movie.query.get(mid))
def __init__(self, *args, **kwargs): # noqa super().__init__(*args, **kwargs) if MyTestCase.app is None: MyTestCase.app = create_app() MyTestCase.client = self.app.test_client() if MyTestCase.db is None: setup_db(self.app, self.database_path) MyTestCase.db = db self.db.drop_all() self.db.create_all() self.sample_actor = dict( name='My actor', age=4, gender=0, ) self.bad_actor = dict( name='', # oops, empty name age=42, gender=0, ) self.sample_movie = dict( title='My movie', release_date=date(2021, 3, 30).isoformat(), ) self.bad_movie = dict( title='', release_date=date(2021, 3, 30).isoformat(), ) m = Movie(**self.sample_movie) m.id = 499 m.insert()
def forge(): ''' Generate fake data ''' db.create_all() name = "Joe Bu" movies = [ {'title': '我不是药神', 'year': '2019'}, {'title': '流感', 'year': '2018'}, {'title': '铁线虫入侵', 'year': '2018'}, {'title': '哪吒', 'year': '2019'}, {'title': '神探狄仁杰', 'year': '2016'}, {'title': '为爱迫降', 'year': '2020'}, {'title': '战狼2', 'year': '2017'}, {'title': '亲爱的,新年好', 'year': '2020'}, ] user = User(name=name) db.session.add(user) for movie in movies: mv = Movie(title=movie['title'], year=movie['year']) db.session.add(mv) db.session.commit() click.echo("Insert Done!")
def test_get_an_movie(self): m = Movie(**self.sample_movie).insert() mid = m.id res = self.get(f'/movies/{mid}') data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['movie']['title'], m.title)
def test_patch_movie(self): m = Movie(**self.sample_movie).insert() res = self.patch(f'/movies/{m.id}', json=dict( title='My new title' )) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['movie']['title'], m.title)
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name='Test', username='******') user.set_password('123') movie = Movie(title='Test Title', year='2020') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() self.runner = app.test_cli_runner()
def _from_resp_to_movie(self, movie_resp: Dict[str, Any]) -> Movie: """ Converts the JSON response received from OMDbAPI into a Movie object Args: movie_resp (Dict): Dict from OMDbAPI JSON movie response Returns: Movie: Object with all the movies properties completely populated """ movie_resp = {key.lower(): value for key, value in movie_resp.items()} movie_resp['item_type'] = movie_resp['type'] del movie_resp['type'] return Movie(**movie_resp)
def post_movie(): post_data = request.get_json() try: movie = Movie(post_data['title'], date.fromisoformat(post_data['release_date'])) movie.actors = Actor.query.filter( Actor.id.in_(post_data.get('actors', []))).all() movie.insert() return jsonify({'success': True, 'movie_id': movie.id}) except Exception: db.session.rollback() print(sys.exc_info()) abort(422, 'Unprocessable request to add new Movie')
def post_movie(payload): try: request_dict = process_request(request) m1 = Movie(title=request_dict["title"]) db.session.add(m1) db.session.commit() except: db.session.rollback() db.session.close() abort(422) finally: db.session.close() return jsonify({'success': True}, 204)
def index(): if request.method == 'POST': # check login if not current_user.is_authenticated: return redirect(url_for('index')) title = request.form.get('title') year = request.form.get('year') if not title or not year or len(year) > 4 or len(title) > 60: flash("Invalid input.") # 显示错误 return redirect(url_for('index')) # 重定向 # save submit data movie = Movie(title=title, year=year) db.session.add(movie) db.session.commit() flash('New Item Added!') return redirect(url_for('index')) user = User.query.first() movies = Movie.query.all() return render_template('index.html', movies=movies)
def seed_db(): Actor('Joe Gainwell', 23, Gender.MALE).insert() Actor('Michelle Ortega', 19, Gender.FEMALE).insert() Movie('Back to the future 4', date.fromisoformat('2021-04-01')).insert() Movie('A new bright sunshine', date.fromisoformat('2022-09-01')).insert()
"duration": 120, "genre": [{ "id": 1, "name": "Action" }], "id": 2, "name": "Movie Example 2", "poster": "Poster URL 2", "rating": 8.0, "year": 2020 } genre_dict_example = {'id': 1, 'name': 'Action'} # Movie model examples movie_model_example_1 = Movie() movie_model_example_1.id = 1 movie_model_example_1.name = 'Movie Example 1' movie_model_example_1.description = 'Description1' movie_model_example_1.duration = 100 movie_model_example_1.poster = 'Poster URL 1' movie_model_example_1.rating = 7.0 movie_model_example_1.year = 2019 # Instanciate a genre model example genre_model = Genre() genre_model.id = 1 genre_model.name = 'Action' # Append genre example to genre list in movie model movie_model_example_1.genre.append(genre_model) movie_model_example_1.director = 'Director1'
def create_model(self): self.model = Movie()