This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_test.go
129 lines (122 loc) · 3.73 KB
/
profile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Contains tests for all CRUD actions for profile model
package main
import (
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zoe-gonzales/meet-up-do-stuff/db"
"github.com/zoe-gonzales/meet-up-do-stuff/user"
)
// Function should create empty profile for unverified user
func TestShouldCreateIncompleteProfile(t *testing.T) {
db, err := db.Init()
if err != nil {
t.Errorf("Could not connect to DB to query users")
}
defer db.Close()
newUser := user.User{Email: "[email protected]", Password: "helloworld", DateJoined: time.Now(), Verified: false}
u := newUser.HashPwd()
u.Create()
entry := u.CreateEmptyProfile()
rowsAffected := entry.RowsAffected
var affected int64 = 1
assert.Equal(t, affected, rowsAffected)
db.Delete(&u)
}
// Function should update any data in user profile
func TestShouldUpdateProfile(t *testing.T) {
var tests = []struct {
name string
updatedProfile user.Profile
}{
{
name: "all_fields",
updatedProfile: user.Profile{DisplayName: "Jane Lane", Location: "New York, NY", Interests: "painting, running, sarcasm", AdminOf: "Artists-Meetup, Runners-Club", MemberOf: "Runners-Club", RSVPS: "8,18,28,38"},
},
{
name: "some_fields",
updatedProfile: user.Profile{Location: "Denver, CO", Interests: "coding, web development", RSVPS: "2,4,6"},
},
{
name: "no_fields",
updatedProfile: user.Profile{},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
db, err := db.Init()
if err != nil {
t.Errorf("Could not connect to DB to update profile")
}
defer db.Close()
user.InitUserModel()
newUser := user.User{Email: "[email protected]", Password: "helloworld", DateJoined: time.Now(), Verified: false}
u := newUser.HashPwd()
u.Create()
newUser.CreateEmptyProfile()
s := strconv.Itoa(int(u.ID))
record, err := user.UpdateProfile(s, tc.updatedProfile)
rowsAffected := record.RowsAffected
switch c := tc.name; c {
case "all_fields":
assert.Equal(t, rowsAffected, int64(1))
case "some_fields":
assert.Error(t, err)
case "no_fields":
assert.Error(t, err)
}
u.Delete()
u.DeleteProfile()
})
}
}
// Function should retrieve profile data
func TestShouldRetrieveProfile(t *testing.T) {
db, err := db.Init()
if err != nil {
t.Errorf("Could not connect to DB to query users")
}
defer db.Close()
user.InitUserModel()
newUser := user.User{Email: "[email protected]", Password: "helloworld", DateJoined: time.Now(), Verified: false}
u := newUser.HashPwd()
u.Create()
newUser.CreateEmptyProfile()
profile := u.GetProfile()
var u2 user.User
assert.IsType(t, profile.User, u2)
assert.GreaterOrEqual(t, profile.UserID, 1)
assert.Equal(t, profile.DisplayName, "---")
assert.Equal(t, profile.Location, "---")
assert.Equal(t, profile.Interests, "---")
assert.Equal(t, profile.AdminOf, "---")
assert.Equal(t, profile.MemberOf, "---")
assert.Equal(t, profile.RSVPS, "---")
db.Delete(&u)
}
// Function should delete profile data (only called when User is deleted)
func TestShouldDeleteProfile(t *testing.T) {
db, err := db.Init()
if err != nil {
t.Errorf("Could not connect to DB to delete profile")
}
defer db.Close()
user.InitUserModel()
newUser := user.User{Email: "[email protected]", Password: "helloworld", DateJoined: time.Now(), Verified: false}
u := newUser.HashPwd()
u.Create()
newUser.CreateEmptyProfile()
u.Delete()
u.DeleteProfile()
profile := u.GetProfile()
assert.Empty(t, profile.User)
assert.Equal(t, profile.UserID, 0)
assert.Equal(t, profile.DisplayName, "")
assert.Equal(t, profile.Location, "")
assert.Equal(t, profile.Interests, "")
assert.Equal(t, profile.AdminOf, "")
assert.Equal(t, profile.MemberOf, "")
assert.Equal(t, profile.RSVPS, "")
}