def test_json_to_checkpoint(self): """ Try creating a checkpoint from a JSON file Notes: Unmaintained and expected to fail. """ self.assertTrue(os.path.exists(self.model_file)) test_work_dir = self.work_dir("test_json_to_checkpoint") # dump stored model to json json_file = os.path.join(test_work_dir, "output.json") # open and remove file in case it remains from previous test open(json_file, "w").close() os.remove(json_file) cmd = [self.script, self.model_file, "--output", json_file] util.run_cmd(self, cmd).expect_exit_code(0) self.assertTrue(os.path.exists(json_file)) # convert json back to checkpoint re_model_file = os.path.join(test_work_dir, "re_model.checkpoint") cmd = [self.json_to_cp, json_file, "--output", re_model_file] open(re_model_file, "w").close() error_message = ("RuntimeError: File/path for 'output' exists, {}" ).format(re_model_file) util.run_cmd(self, cmd).expect_exit_code(1).expect_stderr( util.any_line_starts_with(error_message)) os.remove(re_model_file) util.run_cmd(self, cmd).expect_exit_code(0) self.assertTrue(os.path.exists(re_model_file)) # and finally convert checkpoint back to json again for comparison json_file2 = os.path.join(test_work_dir, "output2.json") # open and remove file in case it remains from previous test open(json_file2, "w").close() os.remove(json_file2) cmd = [self.script, re_model_file, "--output", json_file2] util.run_cmd(self, cmd).expect_exit_code(0) self.assertTrue(os.path.exists(json_file2)) # note that checkpoints are not equal with a binary dump, but the # json files are due to the unused GRU bias parameters. json_dump = open(json_file, 'r').read() json_dump2 = open(json_file2, 'r').read() self.assertEqual(json_dump, json_dump2)
def test_dump_to_a_file(self, options, subdir): self.assertTrue(os.path.exists(self.model_file)) test_work_dir = self.work_dir(os.path.join("test_dump_to_a_file", subdir)) output_file = os.path.join(test_work_dir, "output.json") open(output_file, "w").close() cmd = [self.script, self.model_file, "--out_file", output_file] + options error_message = "RuntimeError: File/path for 'out_file' exists, {}".format(output_file) util.run_cmd(self, cmd).expect_exit_code(1).expect_stderr(util.any_line_starts_with(error_message)) os.remove(output_file) info_message = "Writing to file: {}".format(output_file) util.run_cmd(self, cmd).expect_exit_code(0).expect_stdout(lambda o: o == [info_message]) self.assertTrue(os.path.exists(output_file)) dump = open(output_file, 'r').read() self.assertTrue(is_valid_json(dump))
def test_usage(self): cmd = [self.script] util.run_cmd(self, cmd).expect_exit_code(2).expect_stderr( util.any_line_starts_with(u"usage"))
def test_usage(self): """ Run without arguments, expect usage info """ cmd = [self.script] util.run_cmd(self, cmd).expect_exit_code(2).expect_stderr( util.any_line_starts_with(u"usage"))
def test_usage(self): """Check usage returns the correct exit code and stderr output.""" cmd = [self.merge_script] # cmd = ['python3',self.merge_script] util.run_cmd(self, cmd).expect_exit_code(2).expect_stderr( util.any_line_starts_with(u"usage"))