def test_create_many_games(self): csv_file = "Game_Module_Template.csv" for index in range(7): with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), csv_file), 'rb') as upload_file: response = self.client.post( self.url, { "group_id": str(self.group.id), "file": upload_file }, HTTP_AUTHORIZATION=self.superuser_token) self.assertEqual(202, response.status_code) # Matching number of game scenes created_scenes = GameSceneSerializer(GameScene.objects.all(), many=True).data created_scenes = [dict(scene) for scene in created_scenes] self.assertEqual(6 * (index + 1), len(created_scenes)) # Generate pathways first_scene_id = response.data.get('data', {})['first_scene'] first_scene = GameScene.objects.get(id=first_scene_id) pathways = { str(scene['id']): tuple([choice['pathway'] for choice in scene['choices']]) for scene in created_scenes if not scene['is_end'] } # Check if 1 starting scene and 1 ending scene end_scenes = set() self.traverse_to_scene(first_scene_id, pathways, end_scenes) self.assertEqual(1, len(end_scenes))
def test_delete_a_game(self): url = "{}{}/".format(self.url, self.game['id']) response = self.client.delete(url, HTTP_AUTHORIZATION=self.superuser_token) self.assertEqual(202, response.status_code) all_games = GameSuperuserSerializer(Game.objects.all(), many=True).data self.assertEqual(0, len(all_games)) al_scenes = GameSceneSerializer(GameScene.objects.all(), many=True).data self.assertEqual(0, len(al_scenes))
def test_get_game_with_id(self): url = "{}{}/".format(self.url, self.game['id']) response = self.client.get(url, HTTP_AUTHORIZATION=self.user_token) self.assertEqual(200, response.status_code) returned_game = response.data['data'] for key, value in iter(self.game.items()): self.assertEqual(str(value), str(returned_game[key])) # Compare the scenes and answers all_scenes = GameScene.objects.filter(game_id=self.game['id']) all_answers = GameAnswer.objects.filter(game_id=self.game['id']) all_scenes = GameSceneSerializer(all_scenes, many=True).data all_answers = GameAnswerSerializer(all_answers, many=True).data self.assertEqual(self.populate_dict_from_list(all_scenes), returned_game['scenes']) self.assertEqual(self.populate_dict_from_list(all_answers), returned_game['answers'])
def populate_game_scenes(self, game): game_scenes = GameSceneSerializer( GameScene.objects.filter(game_id=game['id']), many=True).data scenes = {str(scene['id']): scene for scene in iter(game_scenes)} return scenes
def test_create_a_game_2(self): csv_file = "Game_Module_Template_2.csv" with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), csv_file), 'rb') as upload_file: response = self.client.post( self.url, { "group_id": str(self.group.id), "file": upload_file }, HTTP_AUTHORIZATION=self.superuser_token) self.assertEqual(202, response.status_code) # Matching number of game scenes created_scenes = GameSceneSerializer(GameScene.objects.all(), many=True).data created_scenes = [dict(scene) for scene in created_scenes] self.assertEqual(15, len(created_scenes)) # Generate pathways first_scene_id = response.data.get('data', {})['first_scene'] first_scene = GameScene.objects.get(id=first_scene_id) pathways = { str(scene['id']): tuple([choice['pathway'] for choice in scene['choices']]) for scene in created_scenes if not scene['is_end'] } # Check if 1 starting scene and 1 ending scene end_scenes = set() self.traverse_to_scene(first_scene_id, pathways, end_scenes) self.assertEqual(1, len(end_scenes)) # Check if the logic tree is correct expected_tree = { 1: { 2: { 3: { 4: { 5: {} }, 6: { 5: {} }, 7: { 5: {} } }, 8: { 9: { 5: {} }, 10: { 5: {} }, 11: { 5: {} } }, 12: { 13: { 5: {} }, 14: { 5: {} }, 15: { 5: {} } } } } } tree_from_game = self.build_scenes_as_a_tree_from_game( response.data['data']['id']) self.assertEqual(expected_tree, tree_from_game)