fix unit test CID

This commit is contained in:
0xmuralik 2023-03-09 00:47:04 +05:30
parent 1d73452ff6
commit 8932d69f36
2 changed files with 12 additions and 22 deletions

View File

@ -10,7 +10,6 @@ import (
canonicalJson "github.com/gibson042/canonicaljson-go"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/linking"
@ -18,7 +17,6 @@ import (
"github.com/ipld/go-ipld-prime/multicodec"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/storage/memstore"
mh "github.com/multiformats/go-multihash"
)
var store = memstore.Store{}
@ -43,16 +41,6 @@ func GenerateHash(json map[string]interface{}) (string, []byte, error) {
return cidString, content, nil
}
// CIDFromJSONBytes returns CID (cbor) for json (as bytes).
func CIDFromJSONBytes(content []byte) (string, error) {
cid, err := cbor.FromJSON(bytes.NewReader(content), mh.SHA2_256, -1)
if err != nil {
return "", err
}
return cid.String(), nil
}
// GetAttributeAsString returns a map attribute as string, if possible.
func GetAttributeAsString(obj map[string]interface{}, attr string) (string, error) {
if value, ok := obj[attr]; ok {
@ -70,6 +58,10 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro
// This is combination of samples for unmarshalling and linking
// see: https://pkg.go.dev/github.com/ipld/go-ipld-prime
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
if len(content) == 0 {
return "", nil
}
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
nb := np.NewBuilder() // Create a builder.
err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!

View File

@ -1,8 +1,9 @@
package utils
import (
"github.com/stretchr/testify/require"
"testing"
"github.com/stretchr/testify/require"
)
func TestAndValidateCIDGeneration(t *testing.T) {
@ -11,13 +12,12 @@ func TestAndValidateCIDGeneration(t *testing.T) {
content string
expected string
}{
// empty string and empty json blows up
// {
// "empty string", "", "bafyreiengp2sbi6ez34a2jctv34bwyjl7yoliteleaswgcwtqzrhmpyt2m",
// },
// {
// "empty json", "{}", "bafyreihpfkdvib5muloxlj5b3tgdwibjdcu3zdsuhyft33z7gtgnlzlkpm",
// },
{
"empty string", "", "",
},
{
"empty json", "{}", "bafyreigbtj4x7ip5legnfznufuopl4sg4knzc2cof6duas4b3q2fy6swua",
},
{
"test record", "{\"build_artifact_cid\":\"QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9\",\"repo_registration_record_cid\":\"QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D\",\"tls_cert_cid\":\"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR\",\"type\":\"WebsiteRegistrationRecord\",\"url\":\"https://cerc.io\",\"version\":\"0.0.1\"}",
@ -26,10 +26,8 @@ func TestAndValidateCIDGeneration(t *testing.T) {
}
for _, tc := range testCases {
deprecatedAndCorrect, _ := CIDFromJSONBytes([]byte(tc.content))
newImpl, err := CIDFromJSONBytesUsingIpldPrime([]byte(tc.content))
require.NoError(t, err)
require.Equal(t, deprecatedAndCorrect, newImpl, tc.name)
require.Equal(t, tc.expected, newImpl)
}
}