From 5c11c83e2b75a2fe7a2ceeb207fc34b72e00fabb Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Mon, 12 Aug 2024 19:12:22 -0600 Subject: [PATCH] Add a test that demonstrates unmarshalling behavior for `getrawtransaction` results. --- frontend/service_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 frontend/service_test.go diff --git a/frontend/service_test.go b/frontend/service_test.go new file mode 100644 index 00000000..52661ebd --- /dev/null +++ b/frontend/service_test.go @@ -0,0 +1,38 @@ +package frontend + +import ( + "encoding/json" + "testing" + + "github.com/zcash/lightwalletd/common" +) + +func TestZcashdRpcReplyUnmarshalling(t *testing.T) { + var txinfo0 common.ZcashdRpcReplyGetrawtransaction + err0 := json.Unmarshal([]byte("{\"hex\": \"deadbeef\", \"height\": 123456}"), &txinfo0) + if err0 != nil { + t.Fatal("Failed to unmarshal tx with known height.") + } + if txinfo0.Height != 123456 { + t.Errorf("Unmarshalled incorrect height: got: %d, want: 123456.", txinfo0.Height) + } + + var txinfo1 common.ZcashdRpcReplyGetrawtransaction + err1 := json.Unmarshal([]byte("{\"hex\": \"deadbeef\", \"height\": -1}"), &txinfo1) + if err1 != nil { + t.Fatal("failed to unmarshal tx not in main chain") + } + if txinfo1.Height != -1 { + t.Errorf("Unmarshalled incorrect height: got: %d, want: -1.", txinfo1.Height) + } + + var txinfo2 common.ZcashdRpcReplyGetrawtransaction + err2 := json.Unmarshal([]byte("{\"hex\": \"deadbeef\"}"), &txinfo2) + if err2 != nil { + t.Fatal("failed to unmarshal reply lacking height data") + } + if txinfo2.Height != 0 { + t.Errorf("Unmarshalled incorrect height: got: %d, want: 0.", txinfo2.Height) + } +} +