all repos

onasty @ 215b63bea0c822935fad1478fcbbb2646a2425f0

a one-time notes service

onasty/internal/models/notes_test.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
chore(golangci-lint): upgrade config (#14)..., 1 year ago
1
package models
2
3
import (
4
	"testing"
5
	"time"
6
7
	assert "github.com/stretchr/testify/require"
8
)
9
10
func TestNote_Validate(t *testing.T) {
11
	tests := []struct {
12
		name      string
13
		note      Note
14
		willError bool
15
		error     error
16
	}{
17
		// NOTE: there no need to test if note is expired since it tested in IsExpired test
18
		{
19
			name: "ok",
20
			note: Note{ //nolint:exhaustruct
21
				Content:   "some wired ass content",
22
				ExpiresAt: time.Now().Add(time.Hour),
23
			},
24
			willError: false,
25
			error:     nil,
26
		},
27
		{
28
			name:      "content missing",
29
			note:      Note{Content: ""}, //nolint:exhaustruct
30
			willError: true,
31
			error:     ErrNoteContentIsEmpty,
32
		},
33
	}
34
35
	for _, tt := range tests {
36
		t.Run(tt.name, func(t *testing.T) {
37
			err := tt.note.Validate()
38
			if tt.willError {
39
				assert.EqualError(t, err, tt.error.Error())
40
			} else {
41
				assert.NoError(t, err)
42
			}
43
		})
44
	}
45
}
46
47
func TestNote_IsExpired(t *testing.T) {
48
	tests := []struct {
49
		name     string
50
		note     Note
51
		expected bool
52
	}{
53
		{
54
			name:     "expired",
55
			note:     Note{ExpiresAt: time.Now().Add(-time.Hour)}, //nolint:exhaustruct
56
			expected: true,
57
		},
58
		{
59
			name:     "not expired",
60
			note:     Note{ExpiresAt: time.Now().Add(time.Hour)}, //nolint:exhaustruct
61
			expected: false,
62
		},
63
		{
64
			name:     "zero expiration",
65
			note:     Note{ExpiresAt: time.Time{}}, //nolint:exhaustruct
66
			expected: false,
67
		},
68
	}
69
70
	for _, tt := range tests {
71
		t.Run(tt.name, func(t *testing.T) {
72
			assert.Equal(t, tt.expected, tt.note.IsExpired())
73
		})
74
	}
75
}
76
77
func TestNote_ShouldBeBurnt(t *testing.T) {
78
	tests := []struct {
79
		name     string
80
		note     Note
81
		expected bool
82
	}{
83
		{
84
			name: "should be burnt",
85
			note: Note{ //nolint:exhaustruct
86
				BurnBeforeExpiration: true,
87
				ExpiresAt:            time.Now().Add(time.Hour),
88
			},
89
			expected: true,
90
		},
91
		{
92
			name: "could not be burnt, no expiration time",
93
			note: Note{ //nolint:exhaustruct
94
				BurnBeforeExpiration: true,
95
				ExpiresAt:            time.Time{},
96
			},
97
			expected: false,
98
		},
99
		{
100
			name: "could not be burnt, burn when expiration and burn is false",
101
			note: Note{ //nolint:exhaustruct
102
				BurnBeforeExpiration: false,
103
				ExpiresAt:            time.Time{},
104
			},
105
			expected: false,
106
		},
107
	}
108
109
	for _, tt := range tests {
110
		t.Run(tt.name, func(t *testing.T) {
111
			assert.Equal(t, tt.expected, tt.note.ShouldBeBurnt())
112
		})
113
	}
114
}