package tests import ( "crypto/sha256" "encoding/hex" "kerma/models" "testing" "github.com/docker/go/canonical/json" . "github.com/smartystreets/goconvey/convey" ) func TestObject(t *testing.T) { Convey("Given a new object wrapper", t, func() { var wrapper models.ObjectWrapper toHash := "thisstringwillbehashed" hashSum := sha256.Sum256([]byte(toHash)) oid := hex.EncodeToString(hashSum[:]) Convey("When the BuildObjectRequest method is called", func() { wrapper.BuildObjectRequest(oid) Convey("The object wrapper should be a valid 'getobject' object", func() { So(wrapper.Type, ShouldEqual, "getobject") So(wrapper.ObjectID, ShouldEqual, oid) }) }) Convey("When the BuildGossipObject method is called", func() { wrapper.BuildGossipObject(oid) Convey("The object wrapper should be a valid 'ihaveobject' object", func() { So(wrapper.Type, ShouldEqual, "ihaveobject") So(wrapper.ObjectID, ShouldEqual, oid) }) }) }) Convey("Given a new object", t, func() { var object models.Object Convey("When the object is a valid transaction", func() { raw := json.RawMessage(` { "height":1, "outputs":[ { "pubkey":"62b7c521cd9211579cf70fd4099315643767b96711febaa5c76dc3daf27c281c", "value":50000000000000 } ], "type":"transaction" }`) height := uint64(1) object = models.Object{ Type: "object", Object: raw, } transaction := models.Transaction{ Type: "transaction", Height: &height, Outputs: []models.Output{ { Pubkey: "62b7c521cd9211579cf70fd4099315643767b96711febaa5c76dc3daf27c281c", Value: 50000000000000, }, }, } Convey("Calling GetObjectValue should return the transaction and no error", func() { res, err := object.GetObjectValue() So(res, ShouldResemble, &transaction) So(err, ShouldEqual, nil) Convey("Calling String should return the string representation of the transaction", func() { So(res.String(), ShouldEqual, transaction.String()) }) }) }) Convey("When the object is a valid block", func() { previd := "00000000a420b7cefa2b7730243316921ed59ffe836e111ca3801f82a4f5360e" raw := json.RawMessage(` { "type": "block", "txids": [ "1bb37b637d07100cd26fc063dfd4c39a7931cc88dae3417871219715a5e374af" ], "nonce": "c5ee71be4ca85b160d352923a84f86f44b7fc4fe60002214bc1236ceedc5c615", "previd": "` + previd + `", "created": 1649827795114, "T": "00000002af000000000000000000000000000000000000000000000000000000", "miner": "svatsan", "note": "First block. Yayy, I have 50 bu now!!" }`) object = models.Object{ Type: "object", Object: raw, } block := models.Block{ Type: "block", Txids: []string{ "1bb37b637d07100cd26fc063dfd4c39a7931cc88dae3417871219715a5e374af", }, Nonce: "c5ee71be4ca85b160d352923a84f86f44b7fc4fe60002214bc1236ceedc5c615", Previd: &previd, Created: 1649827795114, Target: "00000002af000000000000000000000000000000000000000000000000000000", Miner: "svatsan", Note: "First block. Yayy, I have 50 bu now!!", } Convey("Calling GetObjectValue should return the block and no error", func() { res, err := object.GetObjectValue() So(err, ShouldEqual, nil) So(res, ShouldResemble, &block) Convey("Calling String should return the string representation of the block", func() { So(res.String(), ShouldEqual, block.String()) }) }) }) Convey("When the object is a random object", func() { raw := json.RawMessage(` { "key": "value" }`) object = models.Object{ Type: "object", Object: raw, } Convey("Calling GetObjectValue should return a nil object and an error", func() { res, err := object.GetObjectValue() So(res, ShouldEqual, nil) So(err, ShouldNotEqual, nil) }) }) }) }