import os, sys; sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from pprint import pformat

import pytest

import lasio
import lasio.las_items


test_dir = os.path.dirname(__file__)

egfn = lambda fn: os.path.join(os.path.dirname(__file__), "examples", fn)

stegfn = lambda vers, fn: os.path.join(
    os.path.dirname(__file__), "examples", vers, fn
)


def test_repr():
    h = lasio.las_items.HeaderItem(
        'MN', unit='m', value=20, descr='test testing')

    assert h.__repr__() == pformat(h)


def test_useful_mnemonic_setter_not_allowed():
    h = lasio.las_items.HeaderItem(
        'MN', unit='m', value=20, descr='test testing')

    with pytest.raises(ValueError):
        h.useful_mnemonic = "NEW_NAME"


def test_mmenomic_names_behavior():
    h = lasio.las_items.HeaderItem(
        'MN', unit='m', value=20, descr='test testing')

    h['mnemonic'] = "ZZZ"
    assert h.useful_mnemonic == "MN"

    h.mnemonic = "ZZZ"
    assert h.useful_mnemonic == "ZZZ"


def test_getitem():
    h = lasio.las_items.HeaderItem(
        'MN', unit='m', value=20, descr='test testing')

    assert h["mnemonic"] == "MN"
    assert h["original_mnemonic"] == "MN"
    assert h["useful_mnemonic"] == "MN"
    assert h["unit"] == "m"
    with pytest.raises(KeyError):
        h['notakey']


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) + "}"

    assert h.json == expect
    with pytest.raises(Exception):
        h.json = '{ "_type: "HeaderItem" }'

@@ -101,16 +101,28 @@
 format%0A%0A
+import json%0A
 import p
@@ -661,24 +661,84 @@
  testing')%0A%0A
+    # Writing to useful_mnemonic is prevented by exception.%0A
     with pyt
@@ -939,28 +939,121 @@
     
-h%5B'mnemonic'%5D = %22ZZZ
+# mnemonic is not changed%0A    h%5B'mnemonic'%5D = %22ZZZ%22%0A    assert h%5B'mnemonic'%5D == %22MN%22%0A    assert h.mnemonic == %22MN
 %22%0A  
@@ -1088,16 +1088,42 @@
 = %22MN%22%0A%0A
+    # mnemonic is changed%0A
     h.mn
@@ -1615,228 +1615,538 @@
     
-x = %5B%0A        '%22_type%22: %22HeaderItem%22',%0A        '%22mnemonic%22: %22MN%22',%0A        '%22unit%22: %22m%22',%0A        '%22value%22: 20',%0A        '%22descr%22: %22test testing%22',
+# HeaderItem transformed to json string that includes%0A    # object type and property key/values.%0A    myjson = h.json%0A%0A    # Transform json string into a python dictionary%0A    result = json.loads(myjson)%0A%0A    for key in result.keys():%0A        if key == '_type':%0A            # type of the object this json came from%0A            assert result%5Bkey%5D == 'HeaderItem'
 %0A    
-%5D%0A
     e
-xpect = %22%7B%22 + %22, %22.join(x) + %22%7D%22%0A%0A    assert h.json == expect
+lse:%0A            # data key/values: mnemonic, name, value, descr%0A            assert result%5Bkey%5D == h%5Bkey%5D%0A%0A    # Verify write-to-HeaderItem.json is discouraged.
 %0A   

Fix HeaderItem json test