def test_add_objects_to_scenario(self): s = Scenario.load(StringIO(""" actors: - name: test age: 20 - id: 2 name: test2 age: 22 movies: - title: test movie 1 genre: drama actor: $actor_1 year: 2018 - title: test movie 2 genre: action actor: $actor_2 year: 2018 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) s.add_actor(dict(name="New actor", age=33)) s.add_movie( dict(title="New movie", genre="thriller", actor=s.actors[0], year=2018)) assert 3 == len(s.actors) assert 3 == len(s.movies)
def test_get_object_by_id(self): s = Scenario.load(StringIO(""" actors: - name: test age: 20 - id: 2 name: test2 age: 22 movies: - title: test movie 1 genre: drama actor: $actor_1 year: 2018 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert 20 == s.by_id('actors', 1).age assert "test" == s.by_id('actors', 1).name assert 22 == s.by_id('actors', 2).age assert "test2" == s.by_id('actors', 2).name assert "test movie 1" == s.by_id('movies', 1).title assert not s.by_id('movies', 99)
def test_reference_object_by_alias(self): s = Scenario.load(StringIO(""" actors: - _alias: test1 name: test age: 20 - _alias: test2 id: 2 name: test2 age: 22 movies: - title: test movie 1 genre: drama actor: $actor_test1 year: 2018 - title: test movie 2 genre: action actor: $actor_test2 year: 2018 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert s.actors assert s.movies assert 'test' == s.by_id('movies', 1).actor.name assert 'test2' == s.by_id('movies', 2).actor.name
def test_custom_ids(self): s = Scenario.load(StringIO(""" actors: - id: 1 name: test1 age: 20 - id: 3 name: test2 age: 22 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert s.actors assert 2 == len(s.actors) actor_1 = s.by_id('actors', 1) actor_3 = s.by_id('actors', 3) assert 1 == actor_1.id assert 3 == actor_3.id assert 'test1' == actor_1.name assert 'test2' == actor_3.name # Testing direct attribute access to objects with custom ids actor_1 = s.actors.actor_1 actor_3 = s.actors.actor_3 assert 1 == actor_1.id assert 3 == actor_3.id assert 'test1' == actor_1.name assert 'test2' == actor_3.name
def test_load_type(self): s = Scenario.load(StringIO(""" actors: - name: test age: 20 movies: - title: test movie genre: drama year: 2018 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert s.actors assert 1 == len(s.actors) assert 'test' == s.actors[0].name assert 20 == s.actors[0].age # Testing attribute access to objects assert 'test' == s.actors.actor_1.name assert 20 == s.actors.actor_1.age assert s.movies assert 1 == len(s.movies) assert 'test movie' == s.movies[0].title assert 'drama' == s.movies[0].genre # Testing attribute access to objects assert 'test movie' == s.movies.movie_1.title assert 'drama' == s.movies.movie_1.genre
def test_mix_auto_generated_and_custom_ids(self): s = Scenario.load(StringIO(""" actors: - name: test1 age: 20 - name: test2 age: 22 - id: 1 name: test3 age: 33 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert s.actors assert 3 == len(s.actors) assert 'test3' == s.by_id('actors', 1).name assert 'test2' == s.by_id('actors', 2).name # should've been forced to relocate because of test33 having _id:1 assert 'test1' == s.by_id('actors', 3).name # Testing direct attribute access to objects with custom ids assert 'test3' == s.actors.actor_1.name assert 'test2' == s.actors.actor_2.name assert 'test1' == s.actors.actor_3.name
def test_load_type_missing_required_field(self): config = StringIO(""" actors: 1 """) scenario = None try: scenario = Scenario.load(config, [ActorTypeHandler]) except Exception as e: assert isinstance(e, TypeHandlerException) assert not scenario
def test_load_defaults(self): config = StringIO(""" genres: 2 """) scenario = Scenario.load(config, [GenreTypeHandler]) assert 2 == len(scenario.genres) assert scenario.genres[0] assert scenario.genres[1] assert scenario.genres[0].id != scenario.genres[1].id assert scenario.genres[0].name in ['drama', 'comedy', 'action']
def test_alias_objects(self): s = Scenario.load(StringIO(""" actors: - id: 1 _alias: test1 name: test1 age: 20 - id: 3 name: test2 age: 22 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert s.actors assert 2 == len(s.actors) assert 'test1' == s.by_id('actors', 'test1').name assert not s.by_id('actors', 'test2') # id:3 has no alias
def test_alias_objects(self): s = Scenario.load(StringIO(""" actors: - id: 1 _alias: test1 name: test1 age: 20 - id: 3 name: test2 age: 22 """), type_handlers=[ActorTypeHandler, MovieTypeHandler]) assert s.actors assert 2 == len(s.actors) assert 'test1' == s.by_id('actors', 'test1').name assert not s.by_id('actors', 'test2') # id:3 has no alias # Testing direct attribute access to objects with custom ids assert 'test1' == s.actors.actor_test1.name with self.assertRaises(EntityStoreException, msg="Object not found: test2"): not s.actors.actor_test2