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
Branches
Tags
4 merge requests!510Release,!227Have all Params objects adhere to json.Marshaler/Unmarshaler,!226WIP: Api2.0,!207WIP: Client Restructure
......@@ -8,30 +8,42 @@
package api
import (
"bytes"
"encoding/json"
"testing"
)
// Tests that no data is lost when marshaling and
// unmarshaling the Params object.
func TestParams_MarshalUnmarshal(t *testing.T) {
// Construct a set of params
p := GetDefaultParams()
data, err := json.Marshal(p)
// Marshal the params
data, err := json.Marshal(&p)
if err != nil {
t.Fatalf("Marshal error: %+v", err)
t.Fatalf("Marshal error: %v", err)
}
t.Logf("%s", string(data))
received := GetDefaultParams()
// Unmarshal the params object
received := Params{}
err = json.Unmarshal(data, &received)
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)
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 @@
package cmix
import (
"bytes"
"encoding/json"
"gitlab.com/xx_network/primitives/id"
"reflect"
......@@ -15,6 +16,41 @@ import (
"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) {
p := CMIXParams{
RoundTries: 5,
......
......@@ -8,11 +8,49 @@
package fileTransfer2
import (
"bytes"
"encoding/json"
"gitlab.com/elixxir/client/cmix"
"reflect"
"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.
func TestDefaultParams(t *testing.T) {
expected := Params{
......
......@@ -8,11 +8,42 @@
package single
import (
"bytes"
"encoding/json"
"gitlab.com/elixxir/client/cmix"
"reflect"
"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
// default values.
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