def _read_row(self, row: _ROW, id_: int) -> Movie: """ Helper method to construct a Movie from a row. Raises: ValueError: unable to parse row: {row} """ error = False try: title = row['Title'] genres = [ self._get_genre(Genre(name)) for name in row['Genre'].split(',') ] description = row['Description'] director = self._get_director(Director(row['Director'])) actors = [ self._get_actor(Actor(name)) for name in row['Actors'].split(',') ] release_year = int(row['Year']) runtime_minutes = int(row['Runtime (Minutes)']) rating = float(row['Rating']) votes = int(row['Votes']) revenue_millions = float( row['Revenue (Millions)'] ) if row['Revenue (Millions)'] != 'N/A' else None metascore = int( row['Metascore']) if row['Metascore'] != 'N/A' else None except KeyError: error = True except ValueError: error = True if error: raise ValueError(f'unable to parse row: {row}') movie = Movie(title, release_year, id_) movie.genres = genres movie.description = description movie.director = director movie.actors = actors movie.runtime_minutes = runtime_minutes movie.rating = rating movie.votes = votes if revenue_millions: movie.revenue_millions = revenue_millions if metascore: movie.metascore = metascore return movie
def test_loading_of_actors_with_colleagues(empty_session): actors = ["Andrew", "Cindy"] ids = insert_actors(empty_session, actors) # Create a bidirectional relationship actor_colleagues = [ (ids[0], ids[1]), (ids[1], ids[0]), ] insert_actor_colleagues(empty_session, actor_colleagues) expected = [Actor("Andrew"), Actor("Cindy")] expected[0].add_actor_colleague(expected[1]) expected[1].add_actor_colleague(expected[0]) results = empty_session.query(Actor).all() assert results == expected for result, actor in zip(results, expected): assert result.colleagues == actor.colleagues
def actors(): return [Actor(f'Actor{i}') for i in range(10)]
def test_check_if_this_actor_worked_with_when_false(actor): colleague_a = Actor('A') colleague_b = Actor('b') actor.add_actor_colleague(colleague_a) assert not actor.check_if_this_actor_worked_with(colleague_b)
def actor(): return Actor("Firstname Lastname")
def test_constructor_with_empty_string_actor_full_name(): actor = Actor("") assert actor.actor_full_name is None
def test_check_if_this_actor_worked_with_when_true(actor): colleague = Actor('b') actor.add_actor_colleague(colleague) assert actor.check_if_this_actor_worked_with(colleague)
def test_constructor_strips_whitespace(): actor = Actor(" Firstname Lastname ") assert actor.actor_full_name == "Firstname Lastname"
def test_add_duplicate_colleague(actor): colleague = Actor('b') actor.add_actor_colleague(colleague) actor.add_actor_colleague(colleague) assert len(actor.colleagues) == 1
def test_less_than_when_false(actor): a = Actor("a") b = Actor("b") assert not (b < a)
def test_actors_invalid_type(movie): with pytest.raises(TypeError): movie.actors = Actor("123") with pytest.raises(TypeError): movie.actors = [Actor("123"), 123]
def test_constructor(): actor = Actor("123") assert actor.actor_full_name == "123"
def test_less_than_when_true(actor): a = Actor("a") b = Actor("b") assert a < b
def test_equality_when_not_equal(actor): other = Actor("ActorName2") assert actor != other
def test_equality_when_equal(actor): other = Actor("Firstname Lastname") assert actor == other
def test_repr_with_no_name(): actor = Actor("") assert repr(actor) == "<Actor None>"
def test_loading_of_actors(empty_session): actors = ["Andrew", "Cindy"] insert_actors(empty_session, actors) expected = [Actor("Andrew"), Actor("Cindy")] assert empty_session.query(Actor).all() == expected
def test_add_colleague(actor): colleague = Actor('b') actor.add_actor_colleague(colleague) assert colleague in actor.colleagues
def test_hash_changes(actor): other = Actor("123") assert hash(actor) != hash(other)
def test_constructor_with_non_string_actor_full_name(): actor = Actor(42) assert actor.actor_full_name is None