2024-01-28 01:13:25 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-28 17:39:42 -06:00
|
|
|
"bufio"
|
2024-01-28 01:13:25 -06:00
|
|
|
"encoding"
|
2024-01-28 08:56:34 -06:00
|
|
|
"encoding/json"
|
2024-01-28 17:39:42 -06:00
|
|
|
"strings"
|
2024-01-28 01:13:25 -06:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Type assertions
|
|
|
|
|
var _ encoding.TextMarshaler = Entry{}
|
2024-01-28 08:56:34 -06:00
|
|
|
var _ encoding.TextUnmarshaler = new(Entry)
|
2024-01-28 19:23:51 -06:00
|
|
|
var _ json.Marshaler = Entry{}
|
2024-01-28 22:02:57 -06:00
|
|
|
var _ json.Unmarshaler = new(Entry)
|
2024-01-28 01:13:25 -06:00
|
|
|
|
2024-01-28 08:56:34 -06:00
|
|
|
func TestEntryMarshal(t *testing.T) {
|
2024-01-28 01:13:25 -06:00
|
|
|
when := time.Now()
|
|
|
|
|
whens := when.Format(DateFormat)
|
|
|
|
|
simple := []Meta{}
|
|
|
|
|
nolines := []string{}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
title string
|
|
|
|
|
date time.Time
|
|
|
|
|
fields []Meta
|
|
|
|
|
first string
|
|
|
|
|
lines []string
|
|
|
|
|
err error
|
|
|
|
|
}{
|
2024-01-28 12:41:55 -06:00
|
|
|
{"no-title", "", when, simple, "", nolines, ErrorMissingTitle},
|
|
|
|
|
{"zero-date", "Empty title", time.Time{}, simple, "", nolines, ErrorMissingDate},
|
2024-01-28 01:13:25 -06:00
|
|
|
{"one-line", "A Title", when, simple, "@begin " + whens + " - A Title @end", nolines, nil},
|
|
|
|
|
{
|
|
|
|
|
"one-field",
|
|
|
|
|
"Title 2",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"age", 41}},
|
|
|
|
|
"@begin " + whens + " - Title 2",
|
|
|
|
|
[]string{"@age 41 @end"},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"three-fields",
|
|
|
|
|
"Title 3",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"age", 41}, {"cool", true}, {"name", "Jim"}},
|
|
|
|
|
"@begin " + whens + " - Title 3",
|
|
|
|
|
[]string{"@age 41", "@cool true", "@name Jim"},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"json-field",
|
|
|
|
|
"Title J",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"json", json.RawMessage(`{"age": 41, "cool": true, "name": "Jim"}`)}},
|
|
|
|
|
"@begin " + whens + " - Title J",
|
|
|
|
|
[]string{"@age 41", "@cool true", "@name Jim"},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2024-01-28 08:56:34 -06:00
|
|
|
t.Run(tt.name, getEntryMarshalTestRunner(tt.title, tt.date, tt.fields, tt.first, tt.lines, tt.err))
|
2024-01-28 01:13:25 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-28 08:56:34 -06:00
|
|
|
func getEntryMarshalTestRunner(title string, date time.Time, fields []Meta, first string, lines []string, err error) func(*testing.T) {
|
2024-01-28 01:13:25 -06:00
|
|
|
return func(t *testing.T) {
|
2024-03-10 15:26:00 -05:00
|
|
|
en := Entry{title, date, fields}
|
2024-01-28 01:13:25 -06:00
|
|
|
o, er := en.MarshalText()
|
|
|
|
|
assert.Equal(t, err, er)
|
|
|
|
|
if first == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-09 15:38:34 -06:00
|
|
|
os := string(o)
|
2024-01-28 01:13:25 -06:00
|
|
|
if len(lines) == 0 {
|
2024-03-09 15:38:34 -06:00
|
|
|
assert.Equal(t, first, os)
|
2024-01-28 01:13:25 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 15:38:34 -06:00
|
|
|
assert.Regexp(t, first, os)
|
2024-01-28 01:13:25 -06:00
|
|
|
for _, line := range lines {
|
|
|
|
|
assert.Regexp(t, "(?m)^"+line, os)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-28 08:56:34 -06:00
|
|
|
|
|
|
|
|
func TestEntryUnmarshal(t *testing.T) {
|
|
|
|
|
when := time.Now()
|
|
|
|
|
whens := when.Format(DateFormat)
|
|
|
|
|
simple := []Meta{}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
in string
|
|
|
|
|
title string
|
|
|
|
|
date time.Time
|
|
|
|
|
fields []Meta
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
{"one-line", "@begin " + whens + " - A Title @end", "A Title", when, simple, nil},
|
2024-01-28 17:39:42 -06:00
|
|
|
{"rfc3999-date", "@begin " + when.Format(time.RFC3339) + " - A Title @end", "A Title", when, simple, nil},
|
2024-01-28 08:56:34 -06:00
|
|
|
{"multi-title", "@begin " + whens + " - A Title\nwith break @end", "A Title\nwith break", when, simple, nil},
|
2024-01-28 17:39:42 -06:00
|
|
|
{"no-title", "@begin " + whens + " - @end", "", when, simple, ErrorMissingTitle},
|
|
|
|
|
{"parse-error", "this is no good", "", when, simple, ErrorParsing},
|
|
|
|
|
{"no-date", "@begin - A Title @end", "A Title", when, simple, ErrorMissingDate},
|
|
|
|
|
{"bad-date", "@begin not-a-real date - A Title @end", "A Title", when, simple, ErrorParsing},
|
2024-01-28 08:56:34 -06:00
|
|
|
{"one-field", "@begin " + whens + " - A Title\n@age 41 @end", "A Title", when, []Meta{{"age", 41}}, nil},
|
|
|
|
|
{
|
|
|
|
|
"two-fields",
|
|
|
|
|
"@begin " + whens + " - A Title\n@age 41\n@cool true @end",
|
|
|
|
|
"A Title",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"age", 41}, {"cool", true}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"obj-field",
|
|
|
|
|
"@begin " + whens + " - A Title\n" + `@me {"name":"Dan","coder":true} @end`,
|
|
|
|
|
"A Title",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"me", json.RawMessage(`{"name":"Dan","coder":true}`)}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"json-field",
|
|
|
|
|
"@begin " + whens + " - Some Guy\n" + `@json {"name":"Dan","coder":true} @end`,
|
2024-01-30 23:40:47 -06:00
|
|
|
"Some Guy",
|
2024-01-28 08:56:34 -06:00
|
|
|
when,
|
|
|
|
|
[]Meta{{"name", "Dan"}, {"coder", true}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, getEntryUnmarshalTestRunner(tt.in, tt.title, tt.date, tt.fields, tt.err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getEntryUnmarshalTestRunner(in string, title string, date time.Time, fields []Meta, err error) func(*testing.T) {
|
|
|
|
|
return func(t *testing.T) {
|
|
|
|
|
e := &Entry{}
|
|
|
|
|
er := e.UnmarshalText([]byte(in))
|
|
|
|
|
if err != nil {
|
2024-01-28 17:39:42 -06:00
|
|
|
assert.ErrorIs(t, er, err)
|
2024-01-28 08:56:34 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, title, e.Title)
|
|
|
|
|
assert.WithinRange(t, e.Date, date.Add(-time.Second), date.Add(time.Second))
|
|
|
|
|
for _, f := range fields {
|
|
|
|
|
got := false
|
|
|
|
|
for _, m := range e.Fields {
|
2024-01-28 17:39:42 -06:00
|
|
|
var mVal any = m.Value
|
|
|
|
|
var fVal any = f.Value
|
|
|
|
|
if mJ, ok := m.Value.(json.RawMessage); ok {
|
|
|
|
|
mVal = string(mJ)
|
|
|
|
|
}
|
|
|
|
|
if fJ, ok := f.Value.(json.RawMessage); ok {
|
|
|
|
|
fVal = string(fJ)
|
|
|
|
|
}
|
|
|
|
|
if m.Key == f.Key && mVal == fVal {
|
2024-01-28 08:56:34 -06:00
|
|
|
got = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-30 23:40:47 -06:00
|
|
|
assert.Truef(t, got, "Couldn't find field %+v. We have %+v", f, e.Fields)
|
2024-01-28 08:56:34 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-28 17:39:42 -06:00
|
|
|
|
|
|
|
|
func TestScan(t *testing.T) {
|
|
|
|
|
in := "@begin date - Title\nlong\n@foo john\njones\n@bar 42@nobody @end"
|
|
|
|
|
read := strings.NewReader(in)
|
|
|
|
|
scan := bufio.NewScanner(read)
|
|
|
|
|
scan.Split(scanEntry)
|
|
|
|
|
assert.True(t, scan.Scan())
|
|
|
|
|
assert.Equal(t, "@begin date - Title\nlong", scan.Text())
|
|
|
|
|
assert.True(t, scan.Scan())
|
|
|
|
|
assert.Equal(t, "@foo john\njones", scan.Text())
|
|
|
|
|
assert.True(t, scan.Scan())
|
|
|
|
|
assert.Equal(t, "@bar 42@nobody", scan.Text())
|
|
|
|
|
assert.False(t, scan.Scan())
|
|
|
|
|
}
|
2024-01-28 19:23:51 -06:00
|
|
|
|
|
|
|
|
func TestEntryJsonMarshal(t *testing.T) {
|
|
|
|
|
when := time.Now()
|
2024-01-28 19:41:30 -06:00
|
|
|
whens := when.Format(time.RFC3339)
|
2024-01-28 19:23:51 -06:00
|
|
|
simple := []Meta{}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
title string
|
|
|
|
|
date time.Time
|
|
|
|
|
fields []Meta
|
|
|
|
|
out string
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
{"simple", "A Title", when, simple, `{"title":"A Title","date":"` + whens + `"}`, nil},
|
|
|
|
|
{"one-field", "A Title 2", when, []Meta{{"age", 41}}, `{"title":"A Title 2","date":"` + whens + `","age":41}`, nil},
|
|
|
|
|
{"skip-title-field", "A Title", when, []Meta{{"title", "Different title"}}, `{"title":"A Title","date":"` + whens + `"}`, nil},
|
|
|
|
|
{"skip-date-field", "A Title", when, []Meta{{"date", when.Add(time.Hour)}}, `{"title":"A Title","date":"` + whens + `"}`, nil},
|
|
|
|
|
{"skip-dupe-field", "A Title", when, []Meta{{"foo", "bar"}, {"foo", "baz"}}, `{"title":"A Title","date":"` + whens + `","foo": "bar"}`, nil},
|
|
|
|
|
{"two-fields", "A Title", when, []Meta{{"foo", "bar"}, {"baz", 42}}, `{"title":"A Title","date":"` + whens + `","foo": "bar","baz":42}`, nil},
|
|
|
|
|
{"empty-title", "", when, simple, "", ErrorMissingTitle},
|
|
|
|
|
{"empty-date", "A Title", time.Time{}, simple, "", ErrorMissingDate},
|
|
|
|
|
{"obj-field", "A Title", when, []Meta{{"obj", json.RawMessage(`{"foo":"bar","title":"Sub-title"}`)}}, `{"title":"A Title","date":"` + whens + `","obj":{"foo":"bar","title":"Sub-title"}}`, nil},
|
2024-01-28 19:41:30 -06:00
|
|
|
{"date-field", "A Title", when, []Meta{{"when", when.Add(time.Hour)}}, `{"title":"A Title","date":"` + whens + `","when":"` + when.Add(time.Hour).Format(time.RFC3339) + `"}`, nil},
|
2024-01-30 23:40:47 -06:00
|
|
|
{"json-field", "A Title", when, []Meta{{"json", json.RawMessage(`{"age": 41, "cool": true, "name": "Jim"}`)}}, `{"title":"A Title","date":"` + whens + `","age":41,"cool": true, "name": "Jim"}`, nil},
|
2024-01-28 19:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, getEntryJsonMarshalTestRunner(tt.title, tt.date, tt.fields, tt.out, tt.err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getEntryJsonMarshalTestRunner(title string, date time.Time, fields []Meta, out string, err error) func(t *testing.T) {
|
|
|
|
|
return func(t *testing.T) {
|
2024-03-10 15:26:00 -05:00
|
|
|
e := Entry{title, date, fields}
|
2024-01-28 19:23:51 -06:00
|
|
|
o, er := json.Marshal(e)
|
|
|
|
|
if err == nil {
|
|
|
|
|
assert.JSONEq(t, out, string(o))
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
assert.ErrorIs(t, er, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-28 22:02:57 -06:00
|
|
|
|
|
|
|
|
func TestEntryJsonUnmarshal(t *testing.T) {
|
2024-01-29 22:07:18 -06:00
|
|
|
when := time.Now().Truncate(time.Second)
|
2024-01-28 22:02:57 -06:00
|
|
|
whens := when.Format(time.RFC3339)
|
|
|
|
|
simple := []Meta{}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
in string
|
|
|
|
|
title string
|
|
|
|
|
date time.Time
|
|
|
|
|
fields []Meta
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
{"simple", `{"title":"A Title","date":"` + whens + `"}`, "A Title", when, simple, nil},
|
|
|
|
|
{"missing-title", `{"date":"` + whens + `"}`, "", when, simple, ErrorMissingTitle},
|
|
|
|
|
{"missing-date", `{"title":"A Title"}`, "", when, simple, ErrorMissingDate},
|
|
|
|
|
{"empty-title", `{"title":"","date":"` + whens + `"}`, "", when, simple, ErrorMissingTitle},
|
|
|
|
|
{"empty-date", `{"title":"A Title","date":""}`, "", when, simple, ErrorMissingDate},
|
|
|
|
|
{"bad-date", `{"title":"A Title","date":"bad"}`, "", when, simple, ErrorParsing},
|
2024-01-29 22:07:18 -06:00
|
|
|
{"bad-json", `{"title":"A Title","date":"`, "", when, simple, ErrorParsing},
|
2024-01-28 22:02:57 -06:00
|
|
|
{
|
|
|
|
|
"single-field",
|
|
|
|
|
`{"title":"A Title","date":"` + whens + `","hello":"Hi"}`,
|
|
|
|
|
"A Title",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"hello", "Hi"}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"many-fields",
|
2024-01-29 22:07:18 -06:00
|
|
|
`{"title":"A Title","date":"` + whens + `","hello":"Hi","bye":42,"b":true,"fl":42.13}`,
|
2024-01-28 22:02:57 -06:00
|
|
|
"A Title",
|
|
|
|
|
when,
|
2024-01-29 22:07:18 -06:00
|
|
|
[]Meta{{"hello", "Hi"}, {"bye", int64(42)}, {"b", true}, {"fl", float64(42.13)}},
|
2024-01-28 22:02:57 -06:00
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"date-field",
|
2024-02-25 13:39:12 -06:00
|
|
|
`{"title":"A Title","date":"` + whens + `","posted":"` + when.Add(-time.Hour).In(time.UTC).Format(time.RFC3339) + `"}`,
|
2024-01-28 22:02:57 -06:00
|
|
|
"A Title",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"posted", when.Add(-time.Hour)}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
2024-01-30 23:40:47 -06:00
|
|
|
{
|
|
|
|
|
"json-field",
|
|
|
|
|
`{"title":"A Title","date":"` + whens + `","json":{"age": 41, "cool": true, "name": "Jim"}}`,
|
|
|
|
|
"A Title",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"age", int64(41)}, {"cool", true}, {"name", "Jim"}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"json-field-embed",
|
|
|
|
|
`{"title":"A Title","date":"` + whens + `","json":"{\"age\": 41, \"cool\": true, \"name\": \"Jim\"}"}`,
|
|
|
|
|
"A Title",
|
|
|
|
|
when,
|
|
|
|
|
[]Meta{{"age", int64(41)}, {"cool", true}, {"name", "Jim"}},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
2024-01-28 22:02:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, getEntryJsonUnmarshalTestRunner(tt.in, tt.title, tt.date, tt.fields, tt.err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields []Meta, err error) func(t *testing.T) {
|
|
|
|
|
return func(t *testing.T) {
|
|
|
|
|
e := new(Entry)
|
2024-01-29 22:07:18 -06:00
|
|
|
er := e.UnmarshalJSON([]byte(in))
|
2024-01-28 22:02:57 -06:00
|
|
|
if err != nil {
|
|
|
|
|
assert.ErrorIs(t, er, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 22:07:18 -06:00
|
|
|
assert.Nil(t, er)
|
2024-01-28 22:02:57 -06:00
|
|
|
assert.Equal(t, title, e.Title)
|
|
|
|
|
assert.WithinRange(t, e.Date, date.Add(-time.Second), date.Add(time.Second))
|
2024-01-29 22:07:18 -06:00
|
|
|
assert.Len(t, e.Fields, len(fields))
|
2024-01-28 22:02:57 -06:00
|
|
|
for _, f := range fields {
|
|
|
|
|
got := false
|
2024-02-25 13:39:12 -06:00
|
|
|
fTime, isTime := f.Value.(time.Time)
|
2024-01-28 22:02:57 -06:00
|
|
|
for _, m := range e.Fields {
|
|
|
|
|
var mVal any = m.Value
|
|
|
|
|
var fVal any = f.Value
|
|
|
|
|
if mJ, ok := m.Value.(json.RawMessage); ok {
|
|
|
|
|
mVal = string(mJ)
|
|
|
|
|
}
|
|
|
|
|
if fJ, ok := f.Value.(json.RawMessage); ok {
|
|
|
|
|
fVal = string(fJ)
|
|
|
|
|
}
|
|
|
|
|
if m.Key == f.Key && mVal == fVal {
|
|
|
|
|
got = true
|
|
|
|
|
break
|
|
|
|
|
}
|
2024-02-25 13:39:12 -06:00
|
|
|
if isTime && m.Key == f.Key {
|
|
|
|
|
mTime, _ := mVal.(time.Time)
|
|
|
|
|
if assert.WithinRange(t, mTime, fTime.Add(-2*time.Second), fTime.Add(2*time.Second)) {
|
|
|
|
|
got = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-28 22:02:57 -06:00
|
|
|
}
|
2024-01-29 22:07:18 -06:00
|
|
|
assert.Truef(t, got, "Couldn't find field %+v. We have %+v", f, e.Fields)
|
2024-01-28 22:02:57 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|