Skip to content
Snippets Groups Projects
Commit 169f8f67 authored by Benedikt's avatar Benedikt
Browse files

added fisr version of serilizer test

parent 24ba052a
No related branches found
No related tags found
No related merge requests found
Pipeline #52113 failed
......@@ -4,7 +4,7 @@ from numpy.ma.core import shape
import numpy as np
from AbstractQuantityTypeData import AbstractQuantityTypeData
from typing import Union
from helpers import format_slice,ensureList
from helpers import format_slice, ensureList
from DccName import DccName
class DccQuantityType:
......@@ -180,5 +180,5 @@ class DccQuantityType:
else:
result[key] = value
# TODO: This should not happen, maybe add a warning?
return result
return {'dcc:quantity':result}
......@@ -529,4 +529,5 @@ def test_div_SiRealList():
"test1/test2c",
"test1/test2d",
]
assert result.hasSameValue([ufloat(0.02), ufloat(0.22), ufloat(1.1), ufloat(5.5)])
\ No newline at end of file
assert result.hasSameValue([ufloat(0.02), ufloat(0.22), ufloat(1.1), ufloat(5.5)])
from __future__ import annotations # for type annotation recursion
from typing import Union
# from pathlib import Path
import warnings
import json
import pytest
import os
from dccQuantityParser import parse
print("TESTING test_parser.py")
print(os.getcwd()) # Print current working directory
from testHelpers import get_test_file
from helpers import dccTypeCollector
from dccXMLJSONConv.dccConv import XMLToDict, get_from_dict
from json import dumps,loads
def convertToListOfStr(data: Union(object, List[object])) -> List[str]:
if isinstance(data, list):
return [str(item) for item in data]
else:
return [str(data)]
quantityDataKeysWUnit={'si:real','si:hybrid','si:complex','si:constant','si:realListXMLList','si:complexListXMLList','si:hybridListXMLList','si:constantListXMLList'}
quantityDataKeysWOUnit=['dcc:noQuantity','dcc:charsXMLList']
def test_basic():
# Use the correct relative paths
try:
test_file = get_test_file("tests", "data", "simpleSineCalibration.xml")
with open(test_file, "r") as xml_file:
xml_data = xml_file.read()
# parse XML to Json
jsonDict, errors = XMLToDict(xml_data)
# get dccQuantities
quantityDict = dccTypeCollector(jsonDict)
dccQunats=parse(xml_data)
except FileNotFoundError as e:
warnings.warn(str(e))
serilizedData=[]
for q in dccQunats:
serilizedData.append(q.toJsonDict())
# check if the first key is 'dcc:qunatity'
for i,serilized in enumerate(serilizedData):
inputJsonData=quantityDict[i][1]
# Test name
assert serilized['dcc:name']==inputJsonData['dcc:name']
# Test unit
#TODO reconsider this test strategy may be it would convert to json str and than with normal json loads back to have the str casting done
matching_QuantityTypesWUnit = list(set(quantityDataKeysWUnit) & set(inputJsonData.keys()))
if len(matching_QuantityTypesWUnit)==1:
quantType=matching_QuantityTypesWUnit[0]
isList = True if quantType.endswith('XMLList') else False
suffix = 'XMLList' if isList else ''
outData=serilized[quantType]
strOutData=json.dumps(outData)
inData = inputJsonData[quantType]
# test for same unit since we know we must have a unit
# we don't serilize to str only data but everything musst castable in str and then be equal
if 'si:unit'+suffix in inData.keys() and 'si:unit'+suffix in outData.keys():
if not isList:
print(outData['si:unit'+suffix])
assert str(outData['si:unit'+suffix])==inData['si:unit'+suffix]
else:
print(convertToListOfStr(outData['si:unit'+suffix]))
assert convertToListOfStr(outData['si:unit'+suffix]) == inData['si:unit'+suffix]
else:
raise KeyError("Unit not found in or out test data But we know we must have a unit in both.")
# test for same data
# okay this is rather ahrd to implent so we will go over the json dumps way....
"""
if 'si:value'+suffix in inData.keys() and 'si:value'+suffix in outData.keys():
if not isList:
assert outData['si:value']==inData['si:value']
else:
assert outData['si:value'+suffix].value==inData['si:value'+suffix]
else:
raise KeyError("Data not found in or out test data But we know we must have a data in both.")
"""
# there was no unit so i must have a quantity without unit
elif len(matching_QuantityTypesWUnit)==0:
matching_QuantityTypesWOUnit = list(set(quantityDataKeysWOUnit) & set(inputJsonData.keys()))
assert len(matching_QuantityTypesWOUnit)==1
else:
raise ValueError("There should be only one quantity type with unit")
assert False # we should never get here
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment