Skip to content
Snippets Groups Projects
Commit 6cf058a4 authored by Josh Brooks's avatar Josh Brooks
Browse files

Addd high level tests for marshalling across the board

parent 1e65ef4d
No related branches found
No related tags found
4 merge requests!510Release,!227Have all Params objects adhere to json.Marshaler/Unmarshaler,!226WIP: Api2.0,!207WIP: Client Restructure
...@@ -8,30 +8,42 @@ ...@@ -8,30 +8,42 @@
package api package api
import ( import (
"bytes"
"encoding/json" "encoding/json"
"testing" "testing"
) )
// Tests that no data is lost when marshaling and
// unmarshaling the Params object.
func TestParams_MarshalUnmarshal(t *testing.T) { func TestParams_MarshalUnmarshal(t *testing.T) {
// Construct a set of params
p := GetDefaultParams() p := GetDefaultParams()
data, err := json.Marshal(p)
// Marshal the params
data, err := json.Marshal(&p)
if err != nil { if err != nil {
t.Fatalf("Marshal error: %+v", err) t.Fatalf("Marshal error: %v", err)
} }
t.Logf("%s", string(data)) t.Logf("%s", string(data))
received := GetDefaultParams() // Unmarshal the params object
received := Params{}
err = json.Unmarshal(data, &received) err = json.Unmarshal(data, &received)
if err != nil { if err != nil {
t.Fatalf("Unmarshal error: %+v", err) t.Fatalf("Unmarshal error: %v", err)
} }
// Re-marshal this params object
data2, err := json.Marshal(received) data2, err := json.Marshal(received)
if err != nil { if err != nil {
t.Fatalf("Marshal error: %+v", err) t.Fatalf("Marshal error: %v", err)
} }
t.Logf("%s", string(data2)) // Check that they match (it is done this way to avoid
// false failures with the reflect.DeepEqual function and
// pointers)
if !bytes.Equal(data, data2) {
t.Fatalf("Data was lost in marshal/unmarshal.")
}
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
package cmix package cmix
import ( import (
"bytes"
"encoding/json" "encoding/json"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"reflect" "reflect"
...@@ -15,6 +16,41 @@ import ( ...@@ -15,6 +16,41 @@ import (
"time" "time"
) )
// Tests that no data is lost when marshaling and
// unmarshaling the Params object.
func TestParams_MarshalJSON(t *testing.T) {
// Construct a set of params
p := GetDefaultParams()
// Marshal the params
data, err := json.Marshal(&p)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
t.Logf("%s", string(data))
// Unmarshal the params object
received := Params{}
err = json.Unmarshal(data, &received)
if err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
// Re-marshal this params object
data2, err := json.Marshal(received)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
// Check that they match (it is done this way to avoid
// false failures with the reflect.DeepEqual function and
// pointers)
if !bytes.Equal(data, data2) {
t.Fatalf("Data was lost in marshal/unmarshal.")
}
}
func TestCMIXParams_JSON_Marshal_Unmarshal(t *testing.T) { func TestCMIXParams_JSON_Marshal_Unmarshal(t *testing.T) {
p := CMIXParams{ p := CMIXParams{
RoundTries: 5, RoundTries: 5,
......
...@@ -8,11 +8,49 @@ ...@@ -8,11 +8,49 @@
package fileTransfer2 package fileTransfer2
import ( import (
"bytes"
"encoding/json"
"gitlab.com/elixxir/client/cmix" "gitlab.com/elixxir/client/cmix"
"reflect" "reflect"
"testing" "testing"
) )
// Tests that no data is lost when marshaling and
// unmarshaling the Params object.
func TestParams_MarshalJSON(t *testing.T) {
// Construct a set of params
p := DefaultParams()
// Marshal the params
data, err := json.Marshal(&p)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
t.Logf("%s", string(data))
// Unmarshal the params object
received := Params{}
err = json.Unmarshal(data, &received)
if err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
// Re-marshal this params object
data2, err := json.Marshal(received)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
// Check that they match (it is done this way to avoid
// false failures with the reflect.DeepEqual function and
// pointers)
if !bytes.Equal(data, data2) {
t.Fatalf("Data was lost in marshal/unmarshal.")
}
}
// Tests that DefaultParams returns a Params object with the expected defaults. // Tests that DefaultParams returns a Params object with the expected defaults.
func TestDefaultParams(t *testing.T) { func TestDefaultParams(t *testing.T) {
expected := Params{ expected := Params{
......
...@@ -8,11 +8,42 @@ ...@@ -8,11 +8,42 @@
package single package single
import ( import (
"bytes"
"encoding/json"
"gitlab.com/elixxir/client/cmix" "gitlab.com/elixxir/client/cmix"
"reflect" "reflect"
"testing" "testing"
) )
// Tests that no data is lost when marshaling and
// unmarshaling the RequestParams object.
func TestParams_MarshalUnmarshal(t *testing.T) {
p := GetDefaultRequestParams()
data, err := json.Marshal(p)
if err != nil {
t.Fatalf("Marshal error: %+v", err)
}
t.Logf("%s", string(data))
received := RequestParams{}
err = json.Unmarshal(data, &received)
if err != nil {
t.Fatalf("Unmarshal error: %+v", err)
}
data2, err := json.Marshal(received)
if err != nil {
t.Fatalf("Marshal error: %+v", err)
}
t.Logf("%s", string(data2))
if !bytes.Equal(data, data2) {
t.Fatalf("Data was lost in marshal/unmarshal.")
}
}
// Tests that GetDefaultRequestParams returns a RequestParams with the expected // Tests that GetDefaultRequestParams returns a RequestParams with the expected
// default values. // default values.
func TestGetDefaultRequestParams(t *testing.T) { func TestGetDefaultRequestParams(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment