def test_make_create_event(self): """Check that content is created properly""" # return empty value when content is empty empty_content = make_create_event({}) self.assertEquals(empty_content, None) game_content = { "title" : "test game"} event = make_create_event(game_content) self.assertEquals( is_valid_event(event), True) self.assertEquals( event["type"], "create") self.assertEquals( event["content"], game_content)
def create_content(info, attachments, data_dir): """ Write a JSON file description of a game according to the provided data and attachment files in a dedicated directory named after the game. Arguments: info -- a dictionnary storing all the information about the content. It exactly mimic the structure of the disired JSON file. The data["title"] must exist, no other verification is made to check it contains coherent structure or data. attachments -- list of files attached to the content (images, PDF...). All files will be stored in a '/files' folder data_dir -- directory where all the game directories are stored i.e. where a directory named after the content will be created to store the JSON file. It must exist. Returns the path to the directory created after the name of the slugified title of the content. """ # get slug and add name slugified_name = get_resource_slug(info) info["slug"] = slugified_name # if info about files, remove it info.pop('files', None) # validate game data validate_content(info) # add default state info["state"] = "needs_review" # check attachments if attachments: check_attachments(attachments) # Create a directory after the cleaned name of the content content_path = os.path.join(data_dir, slugified_name) create_resource_folder(content_path) # get current user user = None if current_user.is_authenticated : user = current_user.email # create event and add to history record event = make_create_event(info, user=user) info_with_updated_history = add_event_to_history(info, event) # create the JSON file write_info_json(info_with_updated_history, content_path) # Write the attached files if attachments: write_attachments(attachments, content_path) return content_path
def test_apply_history(self): """ Course of events should change history and should also changed the actual content. -- very deep thoughts here :) """ game_content = { "title" : "test game"} creation = make_create_event(game_content) created_game = add_event_to_history(game_content, creation) new_game_content_1 = { "title" : "My test game"} update1 = make_update_event(new_game_content_1, game_content) updated_game = add_event_to_history(created_game, update1) self.assertEquals(updated_game["title"], new_game_content_1["title"]) new_game_content_2 = { "title" : "My test game", "description" : "An awesome game" } update2 = make_update_event(new_game_content_2, game_content) updated_game = add_event_to_history(updated_game, update2) self.assertTrue("description" in updated_game.keys()) new_game_content_3 = { "title" : "My final test game", "description" : "A very awesome game", "fab_time" : 120 } update3 = make_update_event(new_game_content_3, game_content) updated_game = add_event_to_history(updated_game, update3) self.assertTrue("fab_time" in updated_game.keys()) self.assertEquals(updated_game["title"], new_game_content_3["title"]) # assert that the whole history is there self.assertEquals(len(updated_game["history"]), 4) self.assertEquals(updated_game["title"], "My final test game") # let's check if we can rewrite history ids = [ event["id"] for event in updated_game["history"]] step_zero = apply_history(updated_game["history"], ids[0]) self.assertDictEqual(game_content, step_zero) step_one = apply_history(updated_game["history"], ids[1]) self.assertDictEqual(new_game_content_1, step_one) step_two = apply_history(updated_game["history"], ids[2]) self.assertDictEqual(new_game_content_2, step_two) step_three = apply_history(updated_game["history"], ids[3]) self.assertDictEqual(new_game_content_3, step_three)
def test_event_save_user(self): """User name should be saved""" game_content = { "title" : "test game"} event = make_create_event(game_content, user=self.user_email) self.assertEquals( is_valid_event(event), True) self.assertEquals( event["user"], self.user_email) new_game_content = { "title" : "My test game"} event = make_update_event(game_content, new_game_content, user=self.user_email) self.assertEquals(event["type"], "update") self.assertEquals( is_valid_event(event), True) self.assertEquals( event["user"], self.user_email)
def test_update_content(self): """Content update should be stored using ISO json diff""" # create a basic game and update it game_content = { "title" : "test game"} new_game_content = { "title" : "My test game"} creation = make_create_event(game_content) created_game = add_event_to_history({}, creation) # update the game event = make_update_event(new_game_content, game_content) self.assertEquals(is_valid_event(event), True) self.assertEquals(event["type"], "update") self.assertEquals(type(event["content"]["changes"]), list) self.assertEquals(len(event["content"]["changes"]), 1) self.assertEquals(event["content"]["changes"][0]["value"], "My test game") self.assertEquals(event["content"]["changes"][0]["op"], "replace")
if confirm_choice(): # update the actual data for game_path in needs_update: print game_path info = read_content(game_path) print "-" * 10 print "%s..." % info["title"] # remove history info.pop('history', None) # make a 'create' event = make_create_event(info.copy()) # add it to history info["history"] = [event] # reset state info["state"] = "needs_review" # validate_content(info) write_info_json(info, game_path) print "updated." print print "OK ! done."
def test_make_create_event_reject_games_with_prior_history(self): """make_create_event should not tolerate games with prior history""" game = {"title" :"test", "history" : [1]} self.assertRaises(ValueError, lambda :make_create_event(game))