|
| 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