Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestUnmarshalJSON doesn't test the function it describes #289

Closed
rootulp opened this issue Jan 30, 2024 · 1 comment
Closed

TestUnmarshalJSON doesn't test the function it describes #289

rootulp opened this issue Jan 30, 2024 · 1 comment
Labels
bug Something isn't working testing

Comments

@rootulp
Copy link
Collaborator

rootulp commented Jan 30, 2024

Context

// TestUnmarshalJSON test the UnmarshalJSON function.
func TestUnmarshalJSON(t *testing.T) {
treeName := "testing_unmarshalJSON_tree"
treeConstructorFn := sudoConstructorFn
err := RegisterTree(treeName, treeConstructorFn)
require.NoError(t, err)
codec := NewLeoRSCodec()
result, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, treeConstructorFn)
if err != nil {
panic(err)
}
tests := []struct {
name string
malleate func()
expectedTreeName string
cleanUp func()
}{
{
"Tree field exists",
func() {},
treeName,
func() {
cleanUp(treeName)
},
},
{
"Tree field missing",
func() {
// clear the tree name value in the eds before marshal
result.treeName = ""
},
DefaultTreeName,
func() {},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.malleate()
edsBytes, err := json.Marshal(result)
if err != nil {
t.Errorf("failed to marshal EDS: %v", err)
}
var eds ExtendedDataSquare
err = json.Unmarshal(edsBytes, &eds)
if err != nil {
t.Errorf("failed to unmarshal EDS: %v", err)
}
if !reflect.DeepEqual(result.squareRow, eds.squareRow) {
t.Errorf("eds not equal after json marshal/unmarshal")
}
require.Equal(t, test.expectedTreeName, eds.treeName)
test.cleanUp()
})
}
}

Problem

This test should be testing the behavior of UnmarshalJSON but it never invokes UnmarshalJSON.

Proposal

Update the test to invoke the function UnmarshalJSON

func (eds *ExtendedDataSquare) UnmarshalJSON(b []byte) error {
var aux struct {
DataSquare [][]byte `json:"data_square"`
Codec string `json:"codec"`
Tree string `json:"tree"`
}
err := json.Unmarshal(b, &aux)
if err != nil {
return err
}
var treeConstructor TreeConstructorFn
if aux.Tree == "" {
aux.Tree = DefaultTreeName
}
treeConstructor, err = TreeFn(aux.Tree)
if err != nil {
return err
}
importedEds, err := ImportExtendedDataSquare(aux.DataSquare, codecs[aux.Codec], treeConstructor)
if err != nil {
return err
}
*eds = *importedEds
return nil
}

@rootulp rootulp added bug Something isn't working testing labels Jan 30, 2024
rootulp added a commit that referenced this issue Feb 2, 2024
Closes #286 by deleting
`getTreeNameFromConstructorFn`.
Closes #288,
#289 by fixing those tests.
Inspired by #278
Includes two breaking changes:
1. `ComputeExtendedDataSquare` now accepts a parameter `treeName:
string`
2. `ImportExtendedDataSquare` now accepts a parameter `treeName: string`

---------

Co-authored-by: sontrinh16 <trinhleson2000@gmail.com>
@rootulp
Copy link
Collaborator Author

rootulp commented Feb 3, 2024

Closed by #287

@rootulp rootulp closed this as completed Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working testing
Projects
None yet
Development

No branches or pull requests

1 participant