From edcf474a2597bb5ad644e4485e549f342a46eafb Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 28 Sep 2023 10:05:03 +0900 Subject: [PATCH] initial commits --- ent/admn/dimensiondata.go | 2 +- ent/admn/dimensiondata_marshaljson_test.go | 118 +++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 ent/admn/dimensiondata_marshaljson_test.go diff --git a/ent/admn/dimensiondata.go b/ent/admn/dimensiondata.go index a931712..e3c31f6 100644 --- a/ent/admn/dimensiondata.go +++ b/ent/admn/dimensiondata.go @@ -77,7 +77,7 @@ func (receiver DimensionData) MarshalJSON() ([]byte, error) { { if opt.Nothing[string]() != receiver.HumanValue { - buffer = append(buffer, `,"unit":`...) + buffer = append(buffer, `,"human_value":`...) marshaled, err := json.Marshal(receiver.HumanValue) if nil != err { diff --git a/ent/admn/dimensiondata_marshaljson_test.go b/ent/admn/dimensiondata_marshaljson_test.go new file mode 100644 index 0000000..0a67245 --- /dev/null +++ b/ent/admn/dimensiondata_marshaljson_test.go @@ -0,0 +1,118 @@ +package admn_test + +import ( + "testing" + + "encoding/json" + + "sourcecode.social/reiver/go-opt" + + "sourcecode.social/reiver/go-mstdn/ent/admn" +) + +func TestDimensionData_MarshalJSON(t *testing.T) { + + tests := []struct{ + DimensionData admn.DimensionData + Expected string + }{ + { + DimensionData: admn.DimensionData{ + Key: opt.Something("fa"), + HumanKey: opt.Something("Persian"), + Value: opt.Something("7"), + }, + Expected: `{`+ + `"key":"fa"`+ + `,`+ + `"human_key":"Persian"`+ + `,`+ + `"value":"7"`+ + `}`, + }, + + + + { + DimensionData: admn.DimensionData{ + Key: opt.Something("drive"), + HumanKey: opt.Something("Drive Space"), + Value: opt.Something("2097152"), + Unit: opt.Something("bytes"), + }, + Expected: `{`+ + `"key":"drive"`+ + `,`+ + `"human_key":"Drive Space"`+ + `,`+ + `"value":"2097152"`+ + `,`+ + `"unit":"bytes"`+ + `}`, + }, + { + DimensionData: admn.DimensionData{ + Key: opt.Something("drive"), + HumanKey: opt.Something("Drive Space"), + Value: opt.Something("2097152"), + Unit: opt.Something("bytes"), + HumanValue: opt.Something("2,097,152 bytes"), + }, + Expected: `{`+ + `"key":"drive"`+ + `,`+ + `"human_key":"Drive Space"`+ + `,`+ + `"value":"2097152"`+ + `,`+ + `"unit":"bytes"`+ + `,`+ + `"human_value":"2,097,152 bytes"`+ + `}`, + }, + + + + { + DimensionData: admn.DimensionData{ + Key: opt.Something("primes-founds"), + HumanKey: opt.Something("Number of Prime-Numbers Found"), + Value: opt.Something("8723389322"), + HumanValue: opt.Something("8,723,389,322 Prime-Numbers"), + }, + Expected: `{`+ + `"key":"primes-founds"`+ + `,`+ + `"human_key":"Number of Prime-Numbers Found"`+ + `,`+ + `"value":"8723389322"`+ + `,`+ + `"human_value":"8,723,389,322 Prime-Numbers"`+ + `}`, + }, + } + + for testNumber, test := range tests { + + actualBytes, err := json.Marshal(test.DimensionData) + if nil != err { + t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber) + t.Logf("ERROR: (%T) %s", err, err) + t.Logf("DIMENSION-DATA: %#v", test.DimensionData) + continue + } + + { + actual := string(actualBytes) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED:\n%s", expected) + t.Logf("ACTUAL:\n%s", actual) + t.Logf("DIMENSION-DATA: %#v", test.DimensionData) + continue + } + } + } +}