Fix HeaderItem json test
@@ -2,6 +2,7 @@ import os, sys; sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from pprint import pformat
+import json
import pytest
import lasio
@@ -28,6 +29,7 @@ def test_useful_mnemonic_setter_not_allowed():
h = lasio.las_items.HeaderItem(
'MN', unit='m', value=20, descr='test testing')
+ # Writing to useful_mnemonic is prevented by exception.
with pytest.raises(ValueError):
h.useful_mnemonic = "NEW_NAME"
@@ -36,9 +38,13 @@ def test_mmenomic_names_behavior():
h = lasio.las_items.HeaderItem(
'MN', unit='m', value=20, descr='test testing')
+ # mnemonic is not changed
h['mnemonic'] = "ZZZ"
+ assert h['mnemonic'] == "MN"
+ assert h.mnemonic == "MN"
assert h.useful_mnemonic == "MN"
+ # mnemonic is changed
h.mnemonic = "ZZZ"
assert h.useful_mnemonic == "ZZZ"
@@ -59,15 +65,21 @@ def test_header_json():
h = lasio.las_items.HeaderItem(
'MN', unit='m', value=20, descr='test testing')
- x = [
- '"_type": "HeaderItem"',
- '"mnemonic": "MN"',
- '"unit": "m"',
- '"value": 20',
- '"descr": "test testing"',
- ]
- expect = "{" + ", ".join(x) + "}"
+ # HeaderItem transformed to json string that includes
+ # object type and property key/values.
+ myjson = h.json
- assert h.json == expect
+ # Transform json string into a python dictionary
+ result = json.loads(myjson)
+
+ for key in result.keys():
+ if key == '_type':
+ # type of the object this json came from
+ assert result[key] == 'HeaderItem'
+ else:
+ # data key/values: mnemonic, name, value, descr
+ assert result[key] == h[key]
+
+ # Verify write-to-HeaderItem.json is discouraged.
with pytest.raises(Exception):
h.json = '{ "_type: "HeaderItem" }'
