Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Make tests using temporary files more consistent
- Code style (variable names, keyword argument order)
- File encoding (specify as UTF-8 in all, not just some, cases)
- Child process standard stream encoding (system default)

(The encoding of the temporary files, and of stdin/stdout for the
child processes, are separate. Even when text from the temporary
files is output by a child process, it is encoded with whatever the
stream's encoding is, which for standard streams is the default
system encoding.)
  • Loading branch information
EliahKagan committed Jul 13, 2023
commit 81f418b9c05639375b2238b25133b097375ffa78
4 changes: 2 additions & 2 deletions openai/tests/test_file_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_file_cli(tmp_path: Path) -> None:
contents = json.dumps({"prompt": "1 + 3 =", "completion": "4"}) + "\n"
train_file = tmp_path / "example.jsonl"
train_file = tmp_path / "data.jsonl"
train_file.write_bytes(contents.encode("utf-8"))
create_output = subprocess.check_output(
["openai", "api", "files.create", "-f", train_file, "-p", "fine-tune"]
Expand All @@ -26,7 +26,7 @@ def test_file_cli(tmp_path: Path) -> None:
["openai", "api", "files.delete", "-i", file_id],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
text=True,
)
if delete_result.returncode == 0:
break
Expand Down
12 changes: 6 additions & 6 deletions openai/tests/test_long_examples_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ def test_long_examples_validator(tmp_path: Path) -> None:
{"prompt": long_prompt, "completion": long_completion}, # 2 of 2 duplicates
]

training_data = tmp_path / "data.jsonl"
print(training_data) # show the full path to the temporary file
with open(training_data, mode="w") as file:
train_file = tmp_path / "data.jsonl"
print(train_file) # show the full path to the temporary file
with open(train_file, mode="w", encoding="utf-8") as file:
for prompt_completion_row in unprepared_training_data:
print(json.dumps(prompt_completion_row), file=file)

prepared_data_cmd_output = subprocess.run(
["openai", "tools", "fine_tunes.prepare_data", "-f", training_data],
["openai", "tools", "fine_tunes.prepare_data", "-f", train_file],
stdout=subprocess.PIPE,
text=True,
input="y\ny\ny\ny\ny", # apply all recommendations, one at a time
stderr=subprocess.PIPE,
input="y\ny\ny\ny\ny", # apply all recommendations, one at a time
text=True,
)

# validate data was prepared successfully
Expand Down
4 changes: 2 additions & 2 deletions openai/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def api_key_file(tmp_path: Path, monkeypatch: MonkeyPatch) -> Path:


def test_openai_api_key_path(api_key_file: Path) -> None:
api_key_file.write_text("sk-foo\n")
api_key_file.write_text("sk-foo\n", encoding="utf-8")
assert util.default_api_key() == "sk-foo"


def test_openai_api_key_path_with_malformed_key(api_key_file: Path) -> None:
api_key_file.write_text("malformed-api-key\n")
api_key_file.write_text("malformed-api-key\n", encoding="utf-8")
with pytest.raises(ValueError, match="Malformed API key"):
util.default_api_key()

Expand Down