From 0bc590ba0de62eddbd03a5673848d625fba1443a Mon Sep 17 00:00:00 2001 From: turulomio Date: Mon, 4 Dec 2023 13:49:36 +0100 Subject: [PATCH] myjsonencoder integrated (#24) * updated project file * Added basic test * myjsonencoder integrated --- pydicts.epj | 3 +- pydicts/myjsonencoder.py | 35 ------------------ pydicts/tests/test_myjsonencoder.py | 57 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 pydicts/tests/test_myjsonencoder.py diff --git a/pydicts.epj b/pydicts.epj index 8844562..46b9d18 100644 --- a/pydicts.epj +++ b/pydicts.epj @@ -2,7 +2,7 @@ "header": { "comment": "eric project file for project pydicts", "copyright": "Copyright (C) 2023 , ", - "saved": "2023-12-04, 13:12:01" + "saved": "2023-12-04, 13:20:21" }, "project": { "AUTHOR": "", @@ -89,6 +89,7 @@ "pydicts/tests/test_lod.py", "pydicts/tests/test_lod_ymv.py", "pydicts/tests/test_lol.py", + "pydicts/tests/test_myjsonencoder.py", "pydicts/tests/test_pylatex.py" ], "SOURCESDIR": "", diff --git a/pydicts/myjsonencoder.py b/pydicts/myjsonencoder.py index 780dab3..9608e36 100644 --- a/pydicts/myjsonencoder.py +++ b/pydicts/myjsonencoder.py @@ -218,38 +218,3 @@ def hooks_MyJSONEncoderAsFloat(iter_value): def hooks_MyJSONEncoderAsString(iter_value): return hooks(iter_value, DecimalsWay.String) - -if __name__ == '__main__': - from datetime import timezone - d={} - d["None"]=None - d["Integer"]=12112 - d["Float"]=12121.1212 - d["Date"]=date.today() - d["Datetime"]=datetime.now() - d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12) - d["Time"]=time(12, 12, 12, 123456) - d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc) - d["Bytes"]=b"Byte array" - d["Decimal"]=Decimal("12.12123414") - print ("Dictionary") - print(d) - print() - print ("MyJSONEncoder_dumps") - print (MyJSONEncoder_dumps(d)) - print() - print ("MyJSONEncoder_loads") - print(MyJSONEncoder_loads(MyJSONEncoder_dumps(d))) - print() - print ("MyJSONEncoderDecimalsAsFloat_dumps") - print (MyJSONEncoderDecimalsAsFloat_dumps(d)) - print() - print ("MyJSONEncoderDecimalsAsFloat_loads") - print(MyJSONEncoderDecimalsAsFloat_loads(MyJSONEncoderDecimalsAsFloat_dumps(d))) - print() - print ("MyJSONEncoderDecimalsAsString_dumps") - print (MyJSONEncoderDecimalsAsString_dumps(d)) - print() - print ("MyJSONEncoderDecimalsAsString_loads") - print(MyJSONEncoderDecimalsAsString_loads(MyJSONEncoderDecimalsAsString_dumps(d))) - print() diff --git a/pydicts/tests/test_myjsonencoder.py b/pydicts/tests/test_myjsonencoder.py new file mode 100644 index 0000000..189bc26 --- /dev/null +++ b/pydicts/tests/test_myjsonencoder.py @@ -0,0 +1,57 @@ +from decimal import Decimal +from datetime import timezone, date, datetime, time, timedelta +from pydicts import myjsonencoder +d={} +d["None"]=None +d["Integer"]=12112 +d["Float"]=12121.1212 +d["Date"]=date.today() +d["Datetime"]=datetime.now() +d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12) +d["Time"]=time(12, 12, 12, 123456) +d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc) +d["Bytes"]=b"Byte array" +d["Decimal"]=Decimal("12.12123414") + +def test_myjsonencoder(): + json_string=myjsonencoder.MyJSONEncoder_dumps(d) + json_=myjsonencoder.MyJSONEncoder_loads(json_string) + assert json_["None"]==d["None"] + assert json_["Integer"]==d["Integer"] + assert json_["Float"]==d["Float"] + assert json_["Date"]==d["Date"] + assert json_["Datetime"]==d["Datetime"] + assert json_["Datetime aware"]==d["Datetime aware"] + assert json_["Bytes"]==d["Bytes"] + assert json_["Decimal"]==d["Decimal"] +# assert json_["Time"]==d["Time"] +# assert json_["Timedelta"]==d["Timedelta"] + +def test_myjsonencoder_decimals_as_float(): + json_string=myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d) + json_=myjsonencoder.MyJSONEncoderDecimalsAsFloat_loads(json_string) + assert json_["None"]==d["None"] + assert json_["Integer"]==d["Integer"] + assert json_["Float"]==d["Float"] + assert json_["Date"]==d["Date"] + assert json_["Datetime"]==d["Datetime"] + assert json_["Datetime aware"]==d["Datetime aware"] + assert json_["Bytes"]==d["Bytes"] + assert json_["Decimal"]==float(d["Decimal"]) +# assert json_["Time"]==d["Time"] +# assert json_["Timedelta"]==d["Timedelta"] + + +def test_myjsonencoder_decimals_as_string(): + json_string=myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d) + json_=myjsonencoder.MyJSONEncoderDecimalsAsString_loads(json_string) + assert json_["None"]==d["None"] + assert json_["Integer"]==d["Integer"] + assert json_["Float"]==d["Float"] + assert json_["Date"]==d["Date"] + assert json_["Datetime"]==d["Datetime"] + assert json_["Datetime aware"]==d["Datetime aware"] + assert json_["Bytes"]==d["Bytes"] + assert json_["Decimal"]==str(d["Decimal"]) +# assert json_["Time"]==d["Time"] +# assert json_["Timedelta"]==d["Timedelta"]