1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/tests/test_yaml.py

70 lines
2.3 KiB
Python
Raw Permalink Normal View History

2020-02-04 13:53:45 -08:00
import os
import unittest
import json
import jc.parsers.yaml
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
try:
import ruamel.yaml
RUAMELYAML_INSTALLED = True
except:
RUAMELYAML_INSTALLED = False
2020-02-04 13:53:45 -08:00
@unittest.skipIf(not RUAMELYAML_INSTALLED, 'ruamel.yaml library not installed')
2020-02-04 13:53:45 -08:00
class MyTests(unittest.TestCase):
2022-09-23 14:02:27 -07:00
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/yaml-istio-sc.yaml'), 'r', encoding='utf-8') as f:
generic_yaml_istio_sc = f.read()
2020-02-04 13:53:45 -08:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/yaml-istio-sidecar.yaml'), 'r', encoding='utf-8') as f:
generic_yaml_istio_sidecar = f.read()
2020-02-04 13:53:45 -08:00
2022-09-23 14:02:27 -07:00
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/yaml-istio-sc.json'), 'r', encoding='utf-8') as f:
generic_yaml_istio_sc_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/yaml-istio-sidecar.json'), 'r', encoding='utf-8') as f:
generic_yaml_istio_sidecar_json = json.loads(f.read())
2020-02-04 13:53:45 -08:00
2020-06-12 12:25:07 -07:00
def test_yaml_nodata(self):
"""
Test the YAML parser with no data
"""
self.assertEqual(jc.parsers.yaml.parse('', quiet=True), [])
2020-02-04 13:53:45 -08:00
def test_yaml_istio_sc(self):
"""
Test the Istio SC yaml file
"""
self.assertEqual(jc.parsers.yaml.parse(self.generic_yaml_istio_sc, quiet=True), self.generic_yaml_istio_sc_json)
def test_yaml_istio_sidecar(self):
"""
Test the Istio Sidecar yaml file
"""
self.assertEqual(jc.parsers.yaml.parse(self.generic_yaml_istio_sidecar, quiet=True), self.generic_yaml_istio_sidecar_json)
2022-04-19 14:43:47 -04:00
def test_yaml_datetime(self):
"""
Test yaml file with datetime object (should convert to a string)
"""
data = 'deploymentTime: 2022-04-18T11:12:47'
expected = [{"deploymentTime":"2022-04-18T11:12:47"}]
self.assertEqual(jc.parsers.yaml.parse(data, quiet=True), expected)
def test_yaml_equalsign(self):
"""
Test yaml file with a value that starts with a literal equal sign "=" (should convert to a string)
"""
data = 'key: ='
expected = [{"key":"="}]
self.assertEqual(jc.parsers.yaml.parse(data, quiet=True), expected)
2020-02-04 13:53:45 -08:00
if __name__ == '__main__':
unittest.main()