golang-client/util/rfc8601DateTime_test.go
Davanum Srinivas ce3bd97613 Sanitize the package names
* All files in the same directory should belong to
  the same package
* Avoid xyz/xyz_v123 format (example volume/volume_v2)
* Added a doc.go so we can support a different import
  path later:
    https://golang.org/doc/go1.4#canonicalimports
* Drop the trailing ".git" in the imports

Change-Id: If4a880c62a2142f7015ed693e07af239bbbcf8e5
2017-03-07 13:39:59 -05:00

59 lines
1.9 KiB
Go

// Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package util
import (
"encoding/json"
"testing"
"time"
"git.openstack.org/openstack/golang-client/testUtil"
)
var testValue = `{"created_at":"2014-09-29T14:44:31"}`
var testTime, _ = time.Parse(`"2006-01-02T15:04:05"`, `"2014-09-29T14:44:31"`)
var timeTestValue = timeTest{CreatedAt: RFC8601DateTime{testTime}}
func TestMarshalTimeTest(t *testing.T) {
bytes, _ := json.Marshal(timeTestValue)
testUtil.Equals(t, testValue, string(bytes))
}
func TestUnmarshalValidTimeTest(t *testing.T) {
val := timeTest{}
err := json.Unmarshal([]byte(testValue), &val)
testUtil.IsNil(t, err)
testUtil.Equals(t, timeTestValue.CreatedAt.Time, val.CreatedAt.Time)
}
func TestUnmarshalInvalidDataFormatTimeTest(t *testing.T) {
val := timeTest{}
err := json.Unmarshal([]byte("something other than date time"), &val)
testUtil.Assert(t, err != nil, "expected an error")
}
// Added this test to ensure that its understood that
// only one specific format is supported at this time.
func TestUnmarshalInvalidDateTimeFormatTimeTest(t *testing.T) {
val := timeTest{}
err := json.Unmarshal([]byte("2014-09-29T14:44"), &val)
testUtil.Assert(t, err != nil, "expected an error")
}
type timeTest struct {
CreatedAt RFC8601DateTime `json:"created_at"`
}