2021-08-07 17:13:10 +00:00
|
|
|
/*
|
|
|
|
* Wiki - A wiki with editor
|
|
|
|
* Copyright (c) 2021 Peter Stuifzand
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-03-03 07:30:04 +00:00
|
|
|
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")
|
|
|
|
}
|