Skip to content

Commit 038c8da

Browse files
committed
Refactor tests
1 parent 466de86 commit 038c8da

17 files changed

+1195
-1113
lines changed

internal/model/comment.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package model
22

33
import (
4-
"html/template"
54
"strings"
65

76
"github.com/goyalmunish/reminder/pkg/utils"
@@ -21,19 +20,19 @@ type Comment struct {
2120
}
2221

2322
// String provides basic string representation of a commment.
24-
func (comment *Comment) String() (string, error) {
25-
var escapeString bool = false
23+
func (comment *Comment) String() string {
24+
// var escapeString bool = false
2625

2726
// way 1
28-
if escapeString {
29-
reportTemplate := `[{{.CreatedAt | mediumTimeStr}}] {{.Text}}`
30-
funcMap := template.FuncMap{
31-
"mediumTimeStr": utils.UnixTimestampToMediumTimeStr,
32-
}
33-
return utils.TemplateResult(reportTemplate, funcMap, comment)
34-
}
27+
// if escapeString {
28+
// reportTemplate := `[{{.CreatedAt | mediumTimeStr}}] {{.Text}}`
29+
// funcMap := template.FuncMap{
30+
// "mediumTimeStr": utils.UnixTimestampToMediumTimeStr,
31+
// }
32+
// return utils.TemplateResult(reportTemplate, funcMap, comment)
33+
// }
3534

3635
// way 2
3736
parts := []string{utils.UnixTimestampToMediumTimeStr(comment.CreatedAt), comment.Text}
38-
return strings.Join(parts, " | "), nil
37+
return strings.Join(parts, " | ")
3938
}

internal/model/comment_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package model_test
2+
3+
import (
4+
"testing"
5+
6+
model "github.com/goyalmunish/reminder/internal/model"
7+
"github.com/goyalmunish/reminder/pkg/utils"
8+
)
9+
10+
func TestCommentString(t *testing.T) {
11+
utils.Location = utils.UTCLocation()
12+
c := model.Comment{Text: "c1:\n- line 1\n\n- line 2\n- line 3 with \" and < characters", BaseStruct: model.BaseStruct{CreatedAt: 1600000004, UpdatedAt: 1600001004}}
13+
want := "13-Sep-20 12:26:44 | c1:\n- line 1\n\n- line 2\n- line 3 with \" and < characters"
14+
utils.AssertEqual(t, c.String(), want)
15+
}

internal/model/comments.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ func (c Comments) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
1212
func (c Comments) Less(i, j int) bool { return c[i].CreatedAt > c[j].CreatedAt }
1313

1414
// Strings provides representation of Commments in terms of slice of strings.
15-
func (comments Comments) Strings() ([]string, error) {
15+
func (comments Comments) Strings() []string {
1616
// assuming each note will have 10 comments on average
1717
strs := make([]string, 0, 10)
1818
for _, comment := range comments {
19-
s, err := comment.String()
20-
if err != nil {
21-
return nil, err
22-
}
19+
s := comment.String()
2320
strs = append(strs, s)
2421
}
25-
return strs, nil
22+
return strs
2623
}

internal/model/comments_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package model_test
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
model "github.com/goyalmunish/reminder/internal/model"
8+
"github.com/goyalmunish/reminder/pkg/utils"
9+
)
10+
11+
func TestCommentsStrings(t *testing.T) {
12+
utils.Location = utils.UTCLocation()
13+
comments := model.Comments{&model.Comment{Text: "c1:\n- line 1\n\n- line 2\n- line 3 with \" and < characters"}, &model.Comment{Text: "c2"}, &model.Comment{Text: "c3"}}
14+
want := []string{"nil | c1:\n- line 1\n\n- line 2\n- line 3 with \" and < characters", "nil | c2", "nil | c3"}
15+
utils.AssertEqual(t, comments.Strings(), want)
16+
}
17+
18+
func TestCommentsSort(t *testing.T) {
19+
utils.Location = utils.UTCLocation()
20+
c1 := &model.Comment{Text: "c1", BaseStruct: model.BaseStruct{CreatedAt: 1600000004}}
21+
c2 := &model.Comment{Text: "c2", BaseStruct: model.BaseStruct{CreatedAt: 1600000002}}
22+
c3 := &model.Comment{Text: "c3", BaseStruct: model.BaseStruct{CreatedAt: 1600000003}}
23+
comments := model.Comments{c1, c2, c3}
24+
sort.Sort(comments)
25+
utils.AssertEqual(t, comments, model.Comments{c1, c3, c2})
26+
}

internal/model/functions_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package model_test
2+
3+
import (
4+
"errors"
5+
"io/fs"
6+
"os"
7+
"path"
8+
"testing"
9+
"time"
10+
11+
model "github.com/goyalmunish/reminder/internal/model"
12+
"github.com/goyalmunish/reminder/pkg/utils"
13+
)
14+
15+
func TestNewNote(t *testing.T) {
16+
tagIDs := []int{1, 3, 5}
17+
dummyText := "a random note text"
18+
note, _ := model.NewNote(tagIDs, dummyText)
19+
want := &model.Note{
20+
Text: dummyText,
21+
TagIds: tagIDs,
22+
Status: note.Status,
23+
BaseStruct: model.BaseStruct{UpdatedAt: note.UpdatedAt, CreatedAt: note.CreatedAt},
24+
}
25+
utils.AssertEqual(t, note, want)
26+
}
27+
28+
func TestBasicTags(t *testing.T) {
29+
basicTags := model.BasicTags()
30+
slugs := basicTags.Slugs()
31+
want := "[current priority-urgent priority-medium priority-low repeat-annually repeat-monthly tips]"
32+
utils.AssertEqual(t, slugs, want)
33+
}
34+
35+
func TestNewTag(t *testing.T) {
36+
dummySlug := "test_tag_slug"
37+
dummyGroup := "test_tag_group"
38+
tag, _ := model.NewTag(10, dummySlug, dummyGroup)
39+
want := &model.Tag{
40+
Id: 10,
41+
Slug: dummySlug,
42+
Group: dummyGroup,
43+
}
44+
utils.AssertEqual(t, tag, want)
45+
}
46+
47+
func TestMakeSureFileExists(t *testing.T) {
48+
var dataFilePath = "temp_test_dir/mydata.json"
49+
// make sure temporary files and dirs are removed at the end of the test
50+
defer os.RemoveAll(path.Dir(dataFilePath))
51+
52+
// make sure file doesn't exists already
53+
_, err := os.Stat(dataFilePath)
54+
utils.AssertEqual(t, err != nil, true)
55+
utils.AssertEqual(t, errors.Is(err, fs.ErrNotExist), true)
56+
// attempt to create the file and required dirs, when the file doesn't exist already
57+
_ = model.MakeSureFileExists(dataFilePath, false)
58+
// prove that the file was created
59+
stats, err := os.Stat(dataFilePath)
60+
utils.AssertEqual(t, err != nil, false)
61+
utils.AssertEqual(t, errors.Is(err, fs.ErrNotExist), false)
62+
63+
// make sure that the existing file is not replaced
64+
modificationTime := stats.ModTime()
65+
// attempt to create the file and required dirs, when the file does exist already
66+
time.Sleep(10 * time.Millisecond)
67+
_ = model.MakeSureFileExists(dataFilePath, false)
68+
utils.AssertEqual(t, err != nil, false)
69+
utils.AssertEqual(t, errors.Is(err, fs.ErrNotExist), false)
70+
stats, _ = os.Stat(dataFilePath)
71+
newModificationTime := stats.ModTime()
72+
utils.AssertEqual(t, newModificationTime == modificationTime, true)
73+
}
74+
75+
func TestReadDataFile(t *testing.T) {
76+
var dataFilePath = "temp_test_dir/mydata.json"
77+
// make sure temporary files and dirs are removed at the end of the test
78+
defer os.RemoveAll(path.Dir(dataFilePath))
79+
// create the file and required dirs
80+
_ = model.MakeSureFileExists(dataFilePath, false)
81+
// attempt to read file and parse it
82+
reminderData, _ := model.ReadDataFile(dataFilePath, false)
83+
utils.AssertEqual(t, reminderData.UpdatedAt > 0, true)
84+
}

0 commit comments

Comments
 (0)