"""Tests for locale file parsing module.""" import json import tempfile from pathlib import Path import pytest import yaml class TestLocaleParser: """Tests for LocaleParser class.""" def test_parse_json_file(self, locale_parser, sample_locale_json): """Test parsing a JSON locale file.""" with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: json.dump(sample_locale_json, f) f.flush() f_path = f.name data = locale_parser.parse_file(f_path) assert data["hello"] == "Hello" assert data["goodbye"] == "Goodbye" Path(f_path).unlink() def test_parse_yaml_file(self, locale_parser, sample_locale_yaml): """Test parsing a YAML locale file.""" with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: yaml.dump(sample_locale_yaml, f) f.flush() f_path = f.name data = locale_parser.parse_file(f_path) assert data["hello"] == "Hello" assert data["nested"]["key"] == "Nested value" Path(f_path).unlink() def test_parse_invalid_format(self, locale_parser): """Test parsing an invalid file format raises error.""" with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f: f.write("some content") f.flush() f_path = f.name with pytest.raises(ValueError, match="Unsupported locale file format"): locale_parser.parse_file(f_path) Path(f_path).unlink() def test_get_all_keys_flat(self, locale_parser): """Test getting all keys from flat dictionary.""" data = { "hello": "Hello", "goodbye": "Goodbye", "welcome": "Welcome", } keys = locale_parser.get_all_keys(data) assert keys == {"hello", "goodbye", "welcome"} def test_get_all_keys_nested(self, locale_parser): """Test getting all keys from nested dictionary.""" data = { "user": { "login": "Login", "register": "Register", "profile": { "name": "Name", "email": "Email", }, }, "common": "Common", } keys = locale_parser.get_all_keys(data) assert "user.login" in keys assert "user.register" in keys assert "user.profile.name" in keys assert "user.profile.email" in keys assert "common" in keys assert len(keys) == 5 def test_flatten_keys(self, locale_parser): """Test flattening nested keys to dot notation.""" data = { "user": { "login": "Login", "profile": { "name": "Name", }, }, } flattened = locale_parser.flatten_keys(data) assert flattened == { "user.login": "Login", "user.profile.name": "Name", } def test_unflatten_keys(self, locale_parser): """Test unflattening dot notation keys to nested dictionary.""" data = { "user.login": "Login", "user.profile.name": "Name", } nested = locale_parser.unflatten_keys(data) assert nested == { "user": { "login": "Login", "profile": { "name": "Name", }, }, } def test_add_key_flat(self, locale_parser): """Test adding a flat key.""" data = {"existing": "value"} result = locale_parser.add_key(data, "new_key", "new_value") assert result["existing"] == "value" assert result["new_key"] == "new_value" def test_add_key_nested(self, locale_parser): """Test adding a nested key.""" data = {"user": {}} result = locale_parser.add_key(data, "user.profile.name", "Name") assert result["user"]["profile"]["name"] == "Name" def test_get_value_existing(self, locale_parser): """Test getting an existing value.""" data = { "user": { "name": "John", }, } value = locale_parser.get_value(data, "user.name") assert value == "John" def test_get_value_nonexistent(self, locale_parser): """Test getting a nonexistent value returns None.""" data = {"hello": "Hello"} value = locale_parser.get_value(data, "goodbye") assert value is None def test_parse_directory(self, locale_parser, temp_locale_dir): """Test parsing all locale files in a directory.""" locale_keys = locale_parser.parse_directory(str(temp_locale_dir)) assert "en" in locale_keys assert "es" in locale_keys assert "hello" in locale_keys["en"] assert "unused_key" in locale_keys["en"] def test_parse_directory_with_filter(self, locale_parser, temp_locale_dir): """Test parsing directory with locale filter.""" locale_keys = locale_parser.parse_directory(str(temp_locale_dir), ["en"]) assert "en" in locale_keys assert "es" not in locale_keys def test_parse_directory_nonexistent(self, locale_parser): """Test parsing nonexistent directory returns empty dict.""" locale_keys = locale_parser.parse_directory("/nonexistent") assert locale_keys == {} def test_write_json_file(self, locale_parser, tmp_path): """Test writing JSON locale file.""" data = {"hello": "Hello", "goodbye": "Goodbye"} output_file = tmp_path / "output.json" locale_parser.write_file(output_file, data, "json") assert output_file.exists() written_data = json.loads(output_file.read_text()) assert written_data == data def test_write_yaml_file(self, locale_parser, tmp_path): """Test writing YAML locale file.""" data = {"hello": "Hello", "goodbye": "Goodbye"} output_file = tmp_path / "output.yaml" locale_parser.write_file(output_file, data, "yaml") assert output_file.exists() written_data = yaml.safe_load(output_file.read_text()) assert written_data == data def test_caching(self, locale_parser, sample_locale_json): """Test that parsed files are cached.""" with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: json.dump(sample_locale_json, f) f.flush() f_path = f.name data1 = locale_parser.parse_file(f_path) data2 = locale_parser.parse_file(f_path) assert data1 is data2 Path(f_path).unlink()