From 70b6f0e1fd66782c8ca563de82b71e14db67e648 Mon Sep 17 00:00:00 2001 From: Ghraven Date: Wed, 5 Aug 2026 05:24:29 +0800 Subject: [PATCH] fix(testing): read test case json as utf-8 --- burr/cli/__main__.py | 4 ++-- burr/testing/__init__.py | 2 +- tests/test_testing.py | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/test_testing.py diff --git a/burr/cli/__main__.py b/burr/cli/__main__.py index 5bbc99dd5..8967602d9 100644 --- a/burr/cli/__main__.py +++ b/burr/cli/__main__.py @@ -477,14 +477,14 @@ def create_test_case( if target_file_name: # if it already exists, load it up and append to it if os.path.exists(target_file_name): - with open(target_file_name, "r") as f: + with open(target_file_name, "r", encoding="utf-8") as f: # assumes it's a list of test cases current_testcases = json.load(f) current_testcases.append(tc_json) else: current_testcases = [tc_json] print(f"\nWriting data to file {target_file_name}") - with open(target_file_name, "w") as f: + with open(target_file_name, "w", encoding="utf-8") as f: json.dump(current_testcases, f, indent=2) else: logger.info(json.dumps(tc_json, indent=2)) diff --git a/burr/testing/__init__.py b/burr/testing/__init__.py index f1caa19bf..bd89af77f 100644 --- a/burr/testing/__init__.py +++ b/burr/testing/__init__.py @@ -26,7 +26,7 @@ # and then load it for the test. def load_test_cases(file_name: str) -> tuple: """Load test cases from a json file.""" - with open(file_name, "r") as f: + with open(file_name, "r", encoding="utf-8") as f: json_test_cases = json.load(f) test_cases = [(tc.get("input_state"), tc.get("expected_state")) for tc in json_test_cases] test_ids = [ diff --git a/tests/test_testing.py b/tests/test_testing.py new file mode 100644 index 000000000..3c8224d83 --- /dev/null +++ b/tests/test_testing.py @@ -0,0 +1,43 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import json + +from burr.testing import load_test_cases + + +def test_load_test_cases_reads_utf8_json(tmp_path): + test_case_file = tmp_path / "test_cases.json" + test_case_file.write_text( + json.dumps( + [ + { + "action": "summarize", + "name": "handles_utf8", + "input_state": {"message": "café"}, + "expected_state": {"response": "résumé"}, + } + ], + ensure_ascii=False, + ), + encoding="utf-8", + ) + + test_cases, test_ids = load_test_cases(str(test_case_file)) + + assert test_cases == [({"message": "café"}, {"response": "résumé"})] + assert test_ids == ["summarize-handles_utf8"]