You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 lines
3.1 KiB

package main
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGroupNoRecentChanges(t *testing.T) {
changes := []Change{}
f := groupRecentChanges(changes)
assert.Equal(t, 0, f, "should return 0")
}
func TestGroupOneRecentChange(t *testing.T) {
changes := []Change{
Change{
Page: "Home",
Date: time.Now(),
Body: "test",
},
}
f := groupRecentChanges(changes)
assert.Equal(t, 0, f, "should return 0")
assert.Equal(t, "Home", changes[0].Page, "change should not have changed")
assert.Equal(t, "test", changes[0].Body, "change should not have changed")
assert.Equal(t, 0, changes[0].Count, "change should not have changed")
}
func TestGroupTwoDiffRecentChanges(t *testing.T) {
d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z")
assert.NoError(t, err, "should parse first date")
d2, err := time.Parse(time.RFC3339, "2019-02-02T11:00:00Z")
assert.NoError(t, err, "should parse second date")
changes := []Change{
Change{
Page: "Home",
Date: d2,
Body: "summary 2",
},
Change{
Page: "Home",
Date: d1,
Body: "summary 1",
},
}
f := groupRecentChanges(changes)
assert.Equal(t, 0, f, "should new position of first element")
assert.Equal(t, "Home", changes[0].Page, "change should not have changed")
assert.Equal(t, "summary 2", changes[0].Body, "change should not have changed")
assert.Equal(t, 0, changes[0].Count, "change should not have changed")
assert.Equal(t, "Home", changes[1].Page, "change should not have changed")
assert.Equal(t, "summary 1", changes[1].Body, "change should not have changed")
assert.Equal(t, 0, changes[1].Count, "change should not have changed")
}
func TestGroupTwoRecentChanges(t *testing.T) {
d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z")
assert.NoError(t, err, "should parse first date")
d2, err := time.Parse(time.RFC3339, "2019-01-01T11:00:00Z")
assert.NoError(t, err, "should parse second date")
changes := []Change{
Change{
Page: "Home",
Date: d2,
Body: "summary 2",
},
Change{
Page: "Home",
Date: d1,
Body: "summary 1",
},
}
f := groupRecentChanges(changes)
assert.Equal(t, 1, f, "should new position of first element")
assert.Equal(t, "Home", changes[f].Page, "change should not have changed")
assert.Equal(t, "summary 1 summary 2", changes[f].Body, "change should not have changed")
assert.Equal(t, 1, changes[f].Count, "change should not have changed")
}
func TestChangeNotEqual(t *testing.T) {
d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z")
assert.NoError(t, err, "should parse first date")
d2, err := time.Parse(time.RFC3339, "2019-02-02T11:00:00Z")
assert.NoError(t, err, "should parse second date")
assert.Equal(t, false, changeEqual(Change{Page: "Home", Date: d1}, Change{Page: "Home", Date: d2}), "compare dates")
}
func TestChangeEqual(t *testing.T) {
d1, err := time.Parse(time.RFC3339, "2019-01-01T10:00:00Z")
assert.NoError(t, err, "should parse first date")
d2, err := time.Parse(time.RFC3339, "2019-01-01T11:00:00Z")
assert.NoError(t, err, "should parse second date")
assert.Equal(t, true, changeEqual(Change{Page: "Home", Date: d1}, Change{Page: "Home", Date: d2}), "compare dates")
}