Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goyalmunish committed Jan 14, 2023
1 parent 466de86 commit 038c8da
Show file tree
Hide file tree
Showing 17 changed files with 1,195 additions and 1,113 deletions.
21 changes: 10 additions & 11 deletions internal/model/comment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"html/template"
"strings"

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

// String provides basic string representation of a commment.
func (comment *Comment) String() (string, error) {
var escapeString bool = false
func (comment *Comment) String() string {
// var escapeString bool = false

// way 1
if escapeString {
reportTemplate := `[{{.CreatedAt | mediumTimeStr}}] {{.Text}}`
funcMap := template.FuncMap{
"mediumTimeStr": utils.UnixTimestampToMediumTimeStr,
}
return utils.TemplateResult(reportTemplate, funcMap, comment)
}
// if escapeString {
// reportTemplate := `[{{.CreatedAt | mediumTimeStr}}] {{.Text}}`
// funcMap := template.FuncMap{
// "mediumTimeStr": utils.UnixTimestampToMediumTimeStr,
// }
// return utils.TemplateResult(reportTemplate, funcMap, comment)
// }

// way 2
parts := []string{utils.UnixTimestampToMediumTimeStr(comment.CreatedAt), comment.Text}
return strings.Join(parts, " | "), nil
return strings.Join(parts, " | ")
}
15 changes: 15 additions & 0 deletions internal/model/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model_test

import (
"testing"

model "github.com/goyalmunish/reminder/internal/model"
"github.com/goyalmunish/reminder/pkg/utils"
)

func TestCommentString(t *testing.T) {
utils.Location = utils.UTCLocation()
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}}
want := "13-Sep-20 12:26:44 | c1:\n- line 1\n\n- line 2\n- line 3 with \" and < characters"
utils.AssertEqual(t, c.String(), want)
}
9 changes: 3 additions & 6 deletions internal/model/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ func (c Comments) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c Comments) Less(i, j int) bool { return c[i].CreatedAt > c[j].CreatedAt }

// Strings provides representation of Commments in terms of slice of strings.
func (comments Comments) Strings() ([]string, error) {
func (comments Comments) Strings() []string {
// assuming each note will have 10 comments on average
strs := make([]string, 0, 10)
for _, comment := range comments {
s, err := comment.String()
if err != nil {
return nil, err
}
s := comment.String()
strs = append(strs, s)
}
return strs, nil
return strs
}
26 changes: 26 additions & 0 deletions internal/model/comments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package model_test

import (
"sort"
"testing"

model "github.com/goyalmunish/reminder/internal/model"
"github.com/goyalmunish/reminder/pkg/utils"
)

func TestCommentsStrings(t *testing.T) {
utils.Location = utils.UTCLocation()
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"}}
want := []string{"nil | c1:\n- line 1\n\n- line 2\n- line 3 with \" and < characters", "nil | c2", "nil | c3"}
utils.AssertEqual(t, comments.Strings(), want)
}

func TestCommentsSort(t *testing.T) {
utils.Location = utils.UTCLocation()
c1 := &model.Comment{Text: "c1", BaseStruct: model.BaseStruct{CreatedAt: 1600000004}}
c2 := &model.Comment{Text: "c2", BaseStruct: model.BaseStruct{CreatedAt: 1600000002}}
c3 := &model.Comment{Text: "c3", BaseStruct: model.BaseStruct{CreatedAt: 1600000003}}
comments := model.Comments{c1, c2, c3}
sort.Sort(comments)
utils.AssertEqual(t, comments, model.Comments{c1, c3, c2})
}
84 changes: 84 additions & 0 deletions internal/model/functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package model_test

import (
"errors"
"io/fs"
"os"
"path"
"testing"
"time"

model "github.com/goyalmunish/reminder/internal/model"
"github.com/goyalmunish/reminder/pkg/utils"
)

func TestNewNote(t *testing.T) {
tagIDs := []int{1, 3, 5}
dummyText := "a random note text"
note, _ := model.NewNote(tagIDs, dummyText)
want := &model.Note{
Text: dummyText,
TagIds: tagIDs,
Status: note.Status,
BaseStruct: model.BaseStruct{UpdatedAt: note.UpdatedAt, CreatedAt: note.CreatedAt},
}
utils.AssertEqual(t, note, want)
}

func TestBasicTags(t *testing.T) {
basicTags := model.BasicTags()
slugs := basicTags.Slugs()
want := "[current priority-urgent priority-medium priority-low repeat-annually repeat-monthly tips]"
utils.AssertEqual(t, slugs, want)
}

func TestNewTag(t *testing.T) {
dummySlug := "test_tag_slug"
dummyGroup := "test_tag_group"
tag, _ := model.NewTag(10, dummySlug, dummyGroup)
want := &model.Tag{
Id: 10,
Slug: dummySlug,
Group: dummyGroup,
}
utils.AssertEqual(t, tag, want)
}

func TestMakeSureFileExists(t *testing.T) {
var dataFilePath = "temp_test_dir/mydata.json"
// make sure temporary files and dirs are removed at the end of the test
defer os.RemoveAll(path.Dir(dataFilePath))

// make sure file doesn't exists already
_, err := os.Stat(dataFilePath)
utils.AssertEqual(t, err != nil, true)
utils.AssertEqual(t, errors.Is(err, fs.ErrNotExist), true)
// attempt to create the file and required dirs, when the file doesn't exist already
_ = model.MakeSureFileExists(dataFilePath, false)
// prove that the file was created
stats, err := os.Stat(dataFilePath)
utils.AssertEqual(t, err != nil, false)
utils.AssertEqual(t, errors.Is(err, fs.ErrNotExist), false)

// make sure that the existing file is not replaced
modificationTime := stats.ModTime()
// attempt to create the file and required dirs, when the file does exist already
time.Sleep(10 * time.Millisecond)
_ = model.MakeSureFileExists(dataFilePath, false)
utils.AssertEqual(t, err != nil, false)
utils.AssertEqual(t, errors.Is(err, fs.ErrNotExist), false)
stats, _ = os.Stat(dataFilePath)
newModificationTime := stats.ModTime()
utils.AssertEqual(t, newModificationTime == modificationTime, true)
}

func TestReadDataFile(t *testing.T) {
var dataFilePath = "temp_test_dir/mydata.json"
// make sure temporary files and dirs are removed at the end of the test
defer os.RemoveAll(path.Dir(dataFilePath))
// create the file and required dirs
_ = model.MakeSureFileExists(dataFilePath, false)
// attempt to read file and parse it
reminderData, _ := model.ReadDataFile(dataFilePath, false)
utils.AssertEqual(t, reminderData.UpdatedAt > 0, true)
}
Loading

0 comments on commit 038c8da

Please sign in to comment.