func TestUnmarshalResource(t *testing.T) {
	tests := []struct {
		source        interface{}
		destination   interface{}
		errorExpected bool
	}{
		{
			// 0
			users["1"],
			&user{},
			false,
		},
		{
			// 1
			books["1"],
			&book{},
			false,
		},
	}

	for n, test := range tests {
		payload, err := MarshalResource(test.source, options[0])
		tchek.UnintendedError(err)

		_, err = UnmarshalResource(payload, options[0], test.destination)
		tchek.ErrorExpected(t, n, test.errorExpected, err)
		tchek.AreShallowEqual(t, n, test.source, test.destination)
	}
}
func TestMarshalResource(t *testing.T) {
	tests := []struct {
		resource      interface{}
		options       uint
		errorExpected bool
	}{
		{
			// 0
			users["1"],
			0,
			false,
		},
		{
			// 1
			users["2"],
			1,
			false,
		},
		{
			// 2
			books["1"],
			1,
			false,
		},
	}

	options := []Options{
		Options{
			JSONAPI: map[string]interface{}{
				"version": "1.0",
			},
		},
		Options{
			Meta: map[string]interface{}{
				"meta1": "value",
				"meta2": 38,
			},
			JSONAPI: map[string]interface{}{
				"version": "1.0",
			},
			IncludeRelationshipsData: map[string]bool{
				"books": false,
			},
		},
	}

	for n, test := range tests {
		payload, err := MarshalResource(test.resource, options[test.options])
		tchek.ErrorExpected(t, n, test.errorExpected, err)

		log.Printf("Payload:\n")
		var out bytes.Buffer
		json.Indent(&out, payload, "", "\t")
		out.WriteTo(os.Stdout)
		fmt.Printf("\n")
	}
}
示例#3
0
func TestMarshalAndUnmarshal(t *testing.T) {
	tests := []struct {
		source                 interface{}
		destination            interface{}
		params                 int
		marshalErrorExpected   bool
		unmarshalErrorExpected bool
	}{
		{
			// 0
			nil,
			nil,
			0,
			true,
			false,
		},
		{
			// 1
			users["1"],
			&user{},
			0,
			false,
			false,
		},
		{
			// 2
			[]*user{users["1"], users["2"]},
			&[]*user{},
			0,
			false,
			false,
		},
		{
			// 3
			[]user{*users["1"], *users["2"]},
			&[]*user{},
			0,
			false,
			false,
		},
		{
			// 4
			[]*user{},
			&[]user{},
			0,
			false,
			false,
		},
		{
			// 5
			[]*user{users["1"], users["2"], users["3"]},
			&user{},
			0,
			false,
			true,
		},
	}

	options := []Options{
		Options{
			Extra: Extra{
				JSONAPI: map[string]interface{}{
					"version": "1.0",
				},
			},
		},
		Options{
			Extra: Extra{
				Meta: map[string]interface{}{
					"meta1": "value",
					"meta2": 38,
				},
				JSONAPI: map[string]interface{}{
					"version": "1.0",
				},
			},
			IncludeRelationshipIdentifiers: map[string]bool{
				"books": false,
			},
		},
	}

	for n, test := range tests {
		payload, err := Marshal(test.source, options[0])
		tchek.ErrorExpected(t, n, test.marshalErrorExpected, err)

		if !test.marshalErrorExpected {
			if printTests {
				log.Printf("Payload from test %d:\n", n)
				var out bytes.Buffer
				json.Indent(&out, payload, "", "\t")
				out.WriteTo(os.Stdout)
				fmt.Printf("\n")
			}

			err = Unmarshal(payload, test.destination)
			tchek.ErrorExpected(t, n, test.unmarshalErrorExpected, err)

			srcVal := reflect.ValueOf(test.destination).Elem()
			dstType := reflect.ValueOf(test.destination).Type().Elem()
			if dstType.Kind() == reflect.Slice {
				dstVal := reflect.ValueOf(test.destination).Elem()
				tchek.AreEqual(t, n, srcVal.Len(), dstVal.Len())
				for i := 0; i < dstVal.Len(); i++ {
					tchek.AreShallowEqual(t, n, !test.unmarshalErrorExpected, srcVal.Index(i).Interface(), dstVal.Index(i).Interface())
				}
			} else {
				tchek.AreShallowEqual(t, n, !test.unmarshalErrorExpected, test.source, test.destination)
			}
		}
	}
}