Skip to content

Commit 114020b

Browse files
authored
Merge pull request #16 from ndsev/release/0.8.0
Release v0.8.0
2 parents 17a6651 + 81eab62 commit 114020b

File tree

7 files changed

+700
-49
lines changed

7 files changed

+700
-49
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ jobs:
4040
echo "Transformation failed"
4141
exit 1
4242
fi
43+
- name: Test pyobj_to_yaml functionality
44+
run: |
45+
cd examples/team
46+
python test_pyobj_to_yaml.py
47+
if [ $? -eq 0 ]; then
48+
echo "pyobj_to_yaml tests successful"
49+
else
50+
echo "pyobj_to_yaml tests failed"
51+
exit 1
52+
fi
53+
- name: Test all conversion functions
54+
run: |
55+
cd examples/team
56+
python test_all_conversions.py
57+
if [ $? -eq 0 ]; then
58+
echo "All conversion tests successful"
59+
else
60+
echo "Conversion tests failed"
61+
exit 1
62+
fi
4363
- name: Set version
4464
run: |
4565
if [[ $GITHUB_REF == refs/tags/v* ]]; then
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Comprehensive test script for all zs-yaml conversion functions.
4+
Tests yaml_to_yaml, yaml_to_json, json_to_yaml, and bin_to_yaml.
5+
"""
6+
7+
import sys
8+
import os
9+
import tempfile
10+
import yaml
11+
import json
12+
import subprocess
13+
14+
# Add parent directory to path to import zs_yaml
15+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
16+
17+
from zs_yaml.convert import yaml_to_yaml, yaml_to_json, json_to_yaml, bin_to_yaml
18+
19+
def test_yaml_to_yaml():
20+
"""Test yaml_to_yaml transformation and templating functionality"""
21+
print("Testing yaml_to_yaml transformation...")
22+
23+
# Create a test YAML with transformations
24+
test_yaml_content = """_meta:
25+
schema_module: team.api
26+
schema_type: Team
27+
28+
name: "Transformation Test Team"
29+
members:
30+
- name: "Alice"
31+
age: 25
32+
address:
33+
street: "Main St"
34+
city: "Test City"
35+
country: "Test Country"
36+
zipCode: 12345
37+
workExperience: []
38+
skills:
39+
_f: repeat_node
40+
_a:
41+
count: 3
42+
node:
43+
name: "Python"
44+
level: 8
45+
hobbies:
46+
_f: py_eval
47+
_a:
48+
expr: '["Reading", "Gaming", "Coding"]'
49+
bio: "Test bio"
50+
"""
51+
52+
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as temp_input:
53+
temp_input.write(test_yaml_content)
54+
temp_input_path = temp_input.name
55+
56+
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as temp_output:
57+
temp_output_path = temp_output.name
58+
59+
try:
60+
# Run yaml_to_yaml transformation
61+
yaml_to_yaml(temp_input_path, temp_output_path)
62+
63+
# Read and verify the output
64+
with open(temp_output_path, 'r') as f:
65+
output_data = yaml.safe_load(f)
66+
67+
# Verify transformations were applied
68+
assert len(output_data['members'][0]['skills']) == 3, "repeat_node transformation failed"
69+
assert all(s['name'] == 'Python' for s in output_data['members'][0]['skills']), "repeat_node content incorrect"
70+
assert output_data['members'][0]['hobbies'] == ["Reading", "Gaming", "Coding"], "py_eval transformation failed"
71+
72+
print(" ✓ Transformations applied successfully")
73+
74+
# Clean up
75+
os.unlink(temp_input_path)
76+
os.unlink(temp_output_path)
77+
78+
return True
79+
80+
except Exception as e:
81+
os.unlink(temp_input_path)
82+
if os.path.exists(temp_output_path):
83+
os.unlink(temp_output_path)
84+
raise
85+
86+
87+
def test_yaml_to_json():
88+
"""Test yaml_to_json conversion"""
89+
print("\nTesting yaml_to_json conversion...")
90+
91+
# Use team1.yaml as input
92+
input_yaml_path = "team1.yaml"
93+
94+
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as temp_output:
95+
output_json_path = temp_output.name
96+
97+
try:
98+
# Convert YAML to JSON
99+
yaml_to_json(input_yaml_path, output_json_path)
100+
101+
# Read and verify JSON
102+
with open(output_json_path, 'r') as f:
103+
json_data = json.load(f)
104+
105+
# Verify key fields exist
106+
assert 'name' in json_data, "name field missing in JSON"
107+
assert 'members' in json_data, "members field missing in JSON"
108+
assert isinstance(json_data['members'], list), "members should be a list"
109+
assert len(json_data['members']) > 0, "members list should not be empty"
110+
111+
# Verify no _meta in JSON output (as per design)
112+
assert '_meta' not in json_data, "JSON should not contain _meta section"
113+
114+
print(" ✓ YAML to JSON conversion successful")
115+
116+
# Save path for next test
117+
return output_json_path
118+
119+
except Exception as e:
120+
if os.path.exists(output_json_path):
121+
os.unlink(output_json_path)
122+
raise
123+
124+
125+
def test_json_to_yaml(json_path):
126+
"""Test json_to_yaml conversion"""
127+
print("\nTesting json_to_yaml conversion...")
128+
129+
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as temp_output:
130+
output_yaml_path = temp_output.name
131+
132+
try:
133+
# Convert JSON to YAML
134+
json_to_yaml(json_path, output_yaml_path)
135+
136+
# Read and verify YAML
137+
with open(output_yaml_path, 'r') as f:
138+
yaml_data = yaml.safe_load(f)
139+
140+
# Verify key fields exist
141+
assert 'name' in yaml_data, "name field missing in YAML"
142+
assert 'members' in yaml_data, "members field missing in YAML"
143+
assert isinstance(yaml_data['members'], list), "members should be a list"
144+
145+
print(" ✓ JSON to YAML conversion successful")
146+
147+
# Clean up
148+
os.unlink(output_yaml_path)
149+
os.unlink(json_path) # Clean up JSON from previous test
150+
151+
return True
152+
153+
except Exception as e:
154+
if os.path.exists(output_yaml_path):
155+
os.unlink(output_yaml_path)
156+
if os.path.exists(json_path):
157+
os.unlink(json_path)
158+
raise
159+
160+
161+
def test_bin_to_yaml():
162+
"""Test bin_to_yaml conversion (roundtrip test)"""
163+
print("\nTesting bin_to_yaml conversion...")
164+
165+
# First, create a binary file from team1.yaml
166+
print(" Creating binary file from team1.yaml...")
167+
result = subprocess.run(['zs-yaml', 'team1.yaml', 'test_bin_to_yaml.bin'],
168+
capture_output=True, text=True)
169+
if result.returncode != 0:
170+
raise Exception(f"Failed to create binary file: {result.stderr}")
171+
172+
# Create template YAML with metadata for bin_to_yaml
173+
template_yaml_content = """_meta:
174+
schema_module: team.api
175+
schema_type: Team
176+
"""
177+
178+
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as temp_template:
179+
temp_template.write(template_yaml_content)
180+
template_path = temp_template.name
181+
182+
try:
183+
# Convert binary back to YAML
184+
bin_to_yaml('test_bin_to_yaml.bin', template_path)
185+
186+
# Read and verify the output
187+
with open(template_path, 'r') as f:
188+
output_data = yaml.safe_load(f)
189+
190+
# Read original for comparison
191+
with open('team1.yaml', 'r') as f:
192+
original_data = yaml.safe_load(f)
193+
194+
# Verify _meta section exists
195+
assert '_meta' in output_data, "_meta section missing"
196+
assert output_data['_meta']['schema_module'] == 'team.api'
197+
assert output_data['_meta']['schema_type'] == 'Team'
198+
199+
# Verify data integrity
200+
assert 'name' in output_data, "name field missing"
201+
assert 'members' in output_data, "members field missing"
202+
assert output_data['name'] == "Dream Team", "Team name doesn't match"
203+
204+
print(" ✓ Binary to YAML conversion successful")
205+
206+
# Clean up
207+
os.unlink('test_bin_to_yaml.bin')
208+
os.unlink(template_path)
209+
210+
return True
211+
212+
except Exception as e:
213+
if os.path.exists('test_bin_to_yaml.bin'):
214+
os.unlink('test_bin_to_yaml.bin')
215+
if os.path.exists(template_path):
216+
os.unlink(template_path)
217+
raise
218+
219+
220+
def test_yaml_to_yaml_with_template_args():
221+
"""Test yaml_to_yaml with template argument substitution"""
222+
print("\nTesting yaml_to_yaml with template arguments...")
223+
224+
# Create a YAML with template placeholders
225+
test_yaml_content = """_meta:
226+
schema_module: team.api
227+
schema_type: Team
228+
229+
name: "${team_name}"
230+
members:
231+
- name: "${member_name}"
232+
age: ${member_age}
233+
address:
234+
street: "${street}"
235+
city: "Test City"
236+
country: "Test Country"
237+
zipCode: 12345
238+
workExperience: []
239+
skills: []
240+
hobbies: []
241+
bio: "Bio for ${member_name}"
242+
"""
243+
244+
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as temp_input:
245+
temp_input.write(test_yaml_content)
246+
temp_input_path = temp_input.name
247+
248+
# Import YamlTransformer to test with template args
249+
from zs_yaml.yaml_transformer import YamlTransformer
250+
251+
try:
252+
# Create transformer with template arguments
253+
template_args = {
254+
'team_name': 'Template Test Team',
255+
'member_name': 'Bob',
256+
'member_age': '30',
257+
'street': '123 Template St'
258+
}
259+
260+
transformer = YamlTransformer(temp_input_path, template_args)
261+
262+
# Verify template substitution
263+
assert transformer.data['name'] == 'Template Test Team'
264+
assert transformer.data['members'][0]['name'] == 'Bob'
265+
assert transformer.data['members'][0]['age'] == 30
266+
assert transformer.data['members'][0]['address']['street'] == '123 Template St'
267+
assert transformer.data['members'][0]['bio'] == 'Bio for Bob'
268+
269+
print(" ✓ Template argument substitution successful")
270+
271+
# Clean up
272+
os.unlink(temp_input_path)
273+
274+
return True
275+
276+
except Exception as e:
277+
if os.path.exists(temp_input_path):
278+
os.unlink(temp_input_path)
279+
raise
280+
281+
282+
if __name__ == "__main__":
283+
try:
284+
# Run all tests
285+
test_yaml_to_yaml()
286+
json_path = test_yaml_to_json()
287+
test_json_to_yaml(json_path)
288+
test_bin_to_yaml()
289+
test_yaml_to_yaml_with_template_args()
290+
291+
print("\n✅ All conversion tests passed!")
292+
sys.exit(0)
293+
except Exception as e:
294+
print(f"\n❌ Test failed: {e}")
295+
import traceback
296+
traceback.print_exc()
297+
sys.exit(1)

0 commit comments

Comments
 (0)