-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
466de86
commit 038c8da
Showing
17 changed files
with
1,195 additions
and
1,113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.