Пример #1
0
    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()
Пример #2
0
class Movies(Api):
    def __init__(self):
        super().__init__()

    def create_model(self):
        self.model = Movie()

    def sanitize_data(self):
        data = [
            request.json['title'].lower().capitalize(),
            float(request.json['rating']), request.json['image'].lower(),
            request.json['category'].lower().capitalize()
        ]

        return data

    def delete(self, id=None):
        valid_file = lambda f: f != 'default.png'
        if id is None:
            self.model.delete()
            for file in listdir('img/'):
                if valid_file(file):
                    remove(f'img/{file}')
        else:
            filename = self.model.delete_by_id(id)
            if filename != False:
                if exists(f'img/{filename}'):
                    if valid_file(filename):
                        remove(f'img/{filename}')

        return {}, 204
    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()
Пример #4
0
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')
Пример #5
0
    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))
Пример #6
0
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!")
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
def test_get_movie_by_id(setup_db):
    # ARRANGE
    # Insert movies in db
    movie_model_1 = Movie.load(movie_dict_example_1)
    db.session.add(movie_model_1)
    db.session.commit()  # So we can re-use the Genre model inserted above
    movie_model_2 = Movie.load(
        movie_dict_example_2
    )  # Instead of inserting new Genre we retrieve the one above from db
    db.session.add(movie_model_2)
    db.session.commit()

    # ACT
    movie1 = get_movie_by_id(1)
    movie2 = get_movie_by_id(2)

    # ASSERT
    assert movie1.dump() == movie_dict_example_1
    assert movie2.dump() == movie_dict_example_2
Пример #10
0
def test_get_movie_with_existing_id(test_client):
    # ARRANGE
    # Insert movies in db
    movie_model_1 = Movie.load(movie_dict_example_1)
    db.session.add(movie_model_1)
    db.session.commit()  # So we can re-use the Genre model inserted above
    movie_model_2 = Movie.load(
        movie_dict_example_2
    )  # Instead of inserting new Genre we retrieve the one above from db
    db.session.add(movie_model_2)
    db.session.commit()
    movie_id = 1

    # ACT
    response = test_client.get(f'/movies/{movie_id}')
    movie_dict = response.json

    # ASSERT
    assert response.status_code == 200
    assert movie_dict == movie_dict_example_1
Пример #11
0
def test_get_all_movies(setup_db):
    # ARRANGE
    # Insert a movie in database
    movie_model = Movie.load(movie_dict_example_1)
    db.session.add(movie_model)
    db.session.commit()

    # ACT
    all_movies = get_all_movies()

    # ASSERT
    assert len(all_movies) == 1
    assert all_movies[0].dump() == movie_dict_example_1
Пример #12
0
    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)
Пример #13
0
    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()
Пример #14
0
def test_delete_movie_by_id(setup_db):
    # ARRANGE
    # Insert a movie in database
    movie_model = Movie.load(movie_dict_example_1)
    db.session.add(movie_model)
    db.session.commit()

    # ACT
    delete_movie_by_id(1)
    # Retrieve all movies
    all_movies = Movie.query.all()

    # ASSERT
    assert len(all_movies) == 0
Пример #15
0
    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)
Пример #16
0
def test_delete_movie(test_client):
    # ARRANGE
    # Insert a movie in database
    movie_model = Movie.load(movie_dict_example_1)
    db.session.add(movie_model)
    db.session.commit()
    movie_id = 1

    # ACT
    response = test_client.delete(f'/movies/{movie_id}')
    all_movies = Movie.query.all()

    # ASSERT
    assert response.status_code == 204
    assert len(all_movies) == 0
Пример #17
0
def test_read(test_client):
    # ARRANGE
    # Insert a movie in database
    movie_model = Movie.load(movie_dict_example_1)
    db.session.add(movie_model)
    db.session.commit()

    # ACT
    response = test_client.get('/movies')
    all_movies = response.json

    # ASSERT
    assert response.status_code == 200
    assert len(all_movies) == 1
    assert all_movies[0] == movie_dict_example_1
Пример #18
0
def test_movie_load(setup_db):
    # ARRANGE
    # ACT
    movie_model = Movie.load(movie_dict_example_1)

    # ASSERT
    assert movie_model.name == movie_dict_example_1.get('name')
    assert movie_model.description == movie_dict_example_1.get('description')
    assert movie_model.duration == movie_dict_example_1.get('duration')
    assert movie_model.poster == movie_dict_example_1.get('poster')
    assert movie_model.rating == movie_dict_example_1.get('rating')
    assert movie_model.year == movie_dict_example_1.get('year')
    assert movie_model.director == movie_dict_example_1.get('director')
    assert movie_model.genre[0].id == None
    assert movie_model.genre[0].name == movie_dict_example_1.get(
        'genre')[0].get('name')
Пример #19
0
def replace_movie_in_db(movie_id, movie):
    movie_to_replace = Movie.query.filter_by(id=movie_id).one_or_none()
    movie['id'] = movie_id

    if movie_to_replace is not None:
        movie_to_replace.update(**movie)
        logger.info(f'Updated movie with id: {movie_id} in database.')
    else:
        # Insert new movie
        logger.info(
            f'Movie with id: {movie_id} to update is not found in database. inserting movie: {pformat(movie)}'
        )

        movie_model = Movie.load(movie)
        db.session.add(movie_model)
    db.session.commit(
    )  # movie_to_replace ORM object is tracked => commit saves changes done to the object
Пример #20
0
def test_replace_movie_in_db_with_existing_id(setup_db):
    # ARRANGE
    # Insert a movie in database
    movie_model = Movie.load(movie_dict_example_1)
    db.session.add(movie_model)
    db.session.commit()

    # ACT
    replace_movie_in_db(1, movie_dict_example_2)
    # Retrieve the replaced movie
    all_movies = Movie.query.all()
    # Change movie_dict_example_2's id to 1 (id of movie to replace)
    movie_dict_example_2_copy = movie_dict_example_2.copy()
    movie_dict_example_2_copy['id'] = 1

    # ASSERT
    assert len(all_movies) == 1
    assert all_movies[0].dump() == movie_dict_example_2_copy
Пример #21
0
def test_replace_movie(test_client):
    # ARRANGE
    # Insert a movie in database
    movie_model = Movie.load(movie_dict_example_1)
    db.session.add(movie_model)
    db.session.commit()
    movie_id = 1

    # ACT
    response = test_client.put(f'/movies/{movie_id}',
                               json=movie_dict_example_2)
    all_movies = Movie.query.all()
    # Change movie_dict_example_2's id to 1 (movie with id 1 in db has movie_dict_example_2 infos)
    movie_dict_example_2_copy = movie_dict_example_2.copy()
    movie_dict_example_2_copy['id'] = 1

    assert response.status_code == 204
    assert len(all_movies) == 1
    assert all_movies[0].dump() == movie_dict_example_2_copy
Пример #22
0
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)
Пример #23
0
def main(api_key: str = typer.Argument(..., help="tmdb api_key for the user"),
         data_path: str = typer.Option(
             "data",
             help="directory path where you want to save the data to.",
             callback=directory_resolve_callback),
         number_threads: int = typer.Option(cpu_count(),
                                            help="number of threads"),
         init_id: int = typer.Option(
             0, help="initial id to begin iteration")) -> None:
    # create a queue to commnucate with the worker thread
    queue = Queue()
    # crate worker threads
    for num_worker in range(number_threads):
        worker = DownloadWorker(queue)
        # Setting daemon to True will let the main thread exit even though the workers are blocking
        worker.daemon = True
        worker.start()

    # Put the tasks into the queue as a tuple
    # main bar
    last_id = Movie.request_last_movie(host=BASE_URL, api_key=api_key).id
    all_ids = range(init_id, last_id + 1)
    main_bar = tqdm(desc="Queuing movie id:", total=len(all_ids), position=0)
    # thread bar
    thread_bar = tqdm(desc="Downloading movie",
                      total=len(all_ids),
                      position=1,
                      leave=True)

    for _id in all_ids:
        queue.put((BASE_URL, _id, api_key, data_path, thread_bar))
        main_bar.set_postfix(movie_id=_id)
        main_bar.update()
    # causes the main thread to wait for the queue to finish processing all the tasks
    queue.join()
    # save all
    main_logger.info("saving all the data to {}".format(data_path))
    save_all(data_path)
    main_logger.info("process finished!")
Пример #24
0
 def run(self):
     global bad_request_movie
     while True:
         host, _id, api_key, data_path, thread_bar = self.queue.get()
         thread_bar.set_postfix(movie_id=_id,
                                thread_name=self.getName(),
                                bad_movies=bad_request_movie)
         thread_bar.update()
         try:
             movie = Movie.request_movie_by_id(host=host,
                                               _id=_id,
                                               api_key=api_key)
             if getattr(movie, "id", None):
                 movie_list.append(movie.json())
                 movie.save(data_path=data_path)
             else:
                 bad_request_movie += 1
         except Exception as e:
             thread_logger.info(f"There was an error {e}")
             save_all(data_path, self.name)
         finally:
             self.queue.task_done()
Пример #25
0
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'
Пример #27
0
def add_movie_to_db(movie):
    movie_model = Movie.load(movie)
    db.session.add(movie_model)
    db.session.commit()
    logger.info(f'Added movie to database, movie info: {pformat(movie)}')
Пример #28
0
 def create_model(self):
     self.model = Movie()