Fix tests: Need to write proper date unmarshalling also
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
@@ -8,7 +9,11 @@ import (
|
||||
type Date time.Time
|
||||
|
||||
func (d Date) Equal(other Date) bool {
|
||||
return time.Time(d).Equal(time.Time(other))
|
||||
// usec and nsec are lost in ISO8601 conversion
|
||||
dr := time.Time(d).Round(time.Minute)
|
||||
or := time.Time(other).Round(time.Minute)
|
||||
|
||||
return dr.Equal(or)
|
||||
}
|
||||
|
||||
func (d Date) After(other Date) bool {
|
||||
@@ -23,8 +28,26 @@ func (d Date) Format(layout string) string {
|
||||
return time.Time(d).Format(layout)
|
||||
}
|
||||
|
||||
func (t Date) MarshalJSON() ([]byte, error) {
|
||||
func (d Date) MarshalJSON() ([]byte, error) {
|
||||
// Must use ISO8601
|
||||
formatted := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02T15:04:05+00:00"))
|
||||
formatted := fmt.Sprintf("\"%s\"", time.Time(d).Format("2006-01-02T15:04:05+00:00"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (d *Date) UnmarshalJSON(data []byte) error {
|
||||
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
|
||||
return errors.New("Time.UnmarshalJSON: input is not a JSON string")
|
||||
}
|
||||
|
||||
data = data[len(`"`) : len(data)-len(`"`)]
|
||||
|
||||
var err error
|
||||
var t time.Time
|
||||
t, err = time.ParseInLocation("2006-01-02T15:04:05+00:00", string(data), time.Now().Location())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*d = Date(t)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func TestConversations(t *testing.T) {
|
||||
httpServer := httptest.NewServer(server)
|
||||
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{"Alice", "Bob"},
|
||||
UnreadCount: 1,
|
||||
LastMessagePreview: "Hello world",
|
||||
@@ -102,7 +102,7 @@ func TestMessages(t *testing.T) {
|
||||
const text = "This is a test."
|
||||
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{sender},
|
||||
UnreadCount: 1,
|
||||
Guid: "1234567890",
|
||||
@@ -113,7 +113,7 @@ func TestMessages(t *testing.T) {
|
||||
message := model.Message{
|
||||
Text: text,
|
||||
Sender: &conversation.Participants[0],
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
}
|
||||
|
||||
server.Server.AppendMessageToConversation(&conversation, message)
|
||||
@@ -245,7 +245,7 @@ func TestUpdates(t *testing.T) {
|
||||
// Mock conversation
|
||||
guid := "1234567890"
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{"Alice"},
|
||||
UnreadCount: 0,
|
||||
Guid: guid,
|
||||
@@ -256,7 +256,7 @@ func TestUpdates(t *testing.T) {
|
||||
message := model.Message{
|
||||
Text: "This is a test.",
|
||||
Sender: &conversation.Participants[0],
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
}
|
||||
|
||||
// This should enqueue an update item
|
||||
@@ -318,7 +318,7 @@ func TestUpdatesWebsocket(t *testing.T) {
|
||||
// Mock conversation
|
||||
guid := "1234567890"
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{"Alice"},
|
||||
UnreadCount: 0,
|
||||
Guid: guid,
|
||||
@@ -329,7 +329,7 @@ func TestUpdatesWebsocket(t *testing.T) {
|
||||
message := model.Message{
|
||||
Text: "This is a test.",
|
||||
Sender: &conversation.Participants[0],
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
}
|
||||
|
||||
// Open websocket connection
|
||||
@@ -402,7 +402,7 @@ func TestMarkConversation(t *testing.T) {
|
||||
// Mock conversation
|
||||
guid := "1234567890"
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{"Alice"},
|
||||
UnreadCount: 0,
|
||||
Guid: guid,
|
||||
@@ -414,7 +414,7 @@ func TestMarkConversation(t *testing.T) {
|
||||
message := model.Message{
|
||||
Text: "This is a test.",
|
||||
Sender: &conversation.Participants[0],
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
}
|
||||
|
||||
s.Server.ReceiveMessage(&conversation, message)
|
||||
@@ -445,7 +445,7 @@ func TestMessageQueries(t *testing.T) {
|
||||
// Mock conversation
|
||||
guid := "1234567890"
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{"Alice"},
|
||||
UnreadCount: 0,
|
||||
Guid: guid,
|
||||
@@ -497,7 +497,7 @@ func TestMessageQueries(t *testing.T) {
|
||||
// Make sure messages are actually before the pivot
|
||||
for _, message := range messages {
|
||||
if message.Date.After(pivotMessage.Date) {
|
||||
t.Fatalf("Unexpected message date: %s (expected before %s)", message.Date, pivotMessage.Date)
|
||||
t.Fatalf("Unexpected message date.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,7 +529,7 @@ func TestMessageQueries(t *testing.T) {
|
||||
// Make sure messages are actually after the pivot
|
||||
for _, message := range messages {
|
||||
if message.Date.Before(pivotMessage.Date) {
|
||||
t.Fatalf("Unexpected message date: %s (expected after %s)", message.Date, pivotMessage.Date)
|
||||
t.Fatalf("Unexpected message date")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,7 +542,7 @@ func TestSendMessage(t *testing.T) {
|
||||
// Mock conversation
|
||||
guid := "1234567890"
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Date: model.Date(time.Now()),
|
||||
Participants: []string{"Alice"},
|
||||
UnreadCount: 0,
|
||||
Guid: guid,
|
||||
|
||||
Reference in New Issue
Block a user