Browse Source

Move user_follow to separate file (#1210)

Also add unit tests
Ethan Koenig 2 years ago
parent
commit
3803f257fb
5 changed files with 123 additions and 72 deletions
  1. 4 0
      models/fixtures/follow.yml
  2. 3 0
      models/fixtures/user.yml
  3. 0 72
      models/user.go
  4. 71 0
      models/user_follow.go
  5. 45 0
      models/user_follow_test.go

+ 4 - 0
models/fixtures/follow.yml

@@ -0,0 +1,4 @@
1
+-
2
+  id: 1
3
+  user_id: 4
4
+  follow_id: 2

+ 3 - 0
models/fixtures/user.yml

@@ -26,6 +26,7 @@
26 26
   avatar_email: user2@example.com
27 27
   num_repos: 2
28 28
   num_stars: 2
29
+  num_followers: 1
29 30
 
30 31
 -
31 32
   id: 3
@@ -56,6 +57,7 @@
56 57
   avatar: avatar4
57 58
   avatar_email: user4@example.com
58 59
   num_repos: 0
60
+  num_following: 1
59 61
 
60 62
 -
61 63
   id: 5
@@ -72,6 +74,7 @@
72 74
   num_repos: 1
73 75
   allow_create_organization: false
74 76
   is_active: true
77
+  num_following: 0
75 78
 
76 79
 -
77 80
   id: 6

+ 0 - 72
models/user.go

@@ -1292,78 +1292,6 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
1292 1292
 	return users, count, sess.Find(&users)
1293 1293
 }
1294 1294
 
1295
-// ___________    .__  .__
1296
-// \_   _____/___ |  | |  |   ______  _  __
1297
-//  |    __)/  _ \|  | |  |  /  _ \ \/ \/ /
1298
-//  |     \(  <_> )  |_|  |_(  <_> )     /
1299
-//  \___  / \____/|____/____/\____/ \/\_/
1300
-//      \/
1301
-
1302
-// Follow represents relations of user and his/her followers.
1303
-type Follow struct {
1304
-	ID       int64 `xorm:"pk autoincr"`
1305
-	UserID   int64 `xorm:"UNIQUE(follow)"`
1306
-	FollowID int64 `xorm:"UNIQUE(follow)"`
1307
-}
1308
-
1309
-// IsFollowing returns true if user is following followID.
1310
-func IsFollowing(userID, followID int64) bool {
1311
-	has, _ := x.Get(&Follow{UserID: userID, FollowID: followID})
1312
-	return has
1313
-}
1314
-
1315
-// FollowUser marks someone be another's follower.
1316
-func FollowUser(userID, followID int64) (err error) {
1317
-	if userID == followID || IsFollowing(userID, followID) {
1318
-		return nil
1319
-	}
1320
-
1321
-	sess := x.NewSession()
1322
-	defer sessionRelease(sess)
1323
-	if err = sess.Begin(); err != nil {
1324
-		return err
1325
-	}
1326
-
1327
-	if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil {
1328
-		return err
1329
-	}
1330
-
1331
-	if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
1332
-		return err
1333
-	}
1334
-
1335
-	if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
1336
-		return err
1337
-	}
1338
-	return sess.Commit()
1339
-}
1340
-
1341
-// UnfollowUser unmarks someone as another's follower.
1342
-func UnfollowUser(userID, followID int64) (err error) {
1343
-	if userID == followID || !IsFollowing(userID, followID) {
1344
-		return nil
1345
-	}
1346
-
1347
-	sess := x.NewSession()
1348
-	defer sessionRelease(sess)
1349
-	if err = sess.Begin(); err != nil {
1350
-		return err
1351
-	}
1352
-
1353
-	if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil {
1354
-		return err
1355
-	}
1356
-
1357
-	if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
1358
-		return err
1359
-	}
1360
-
1361
-	if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
1362
-		return err
1363
-	}
1364
-	return sess.Commit()
1365
-}
1366
-
1367 1295
 // GetStarredRepos returns the repos starred by a particular user
1368 1296
 func GetStarredRepos(userID int64, private bool) ([]*Repository, error) {
1369 1297
 	sess := x.Where("star.uid=?", userID).

+ 71 - 0
models/user_follow.go

@@ -0,0 +1,71 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package models
6
+
7
+// Follow represents relations of user and his/her followers.
8
+type Follow struct {
9
+	ID       int64 `xorm:"pk autoincr"`
10
+	UserID   int64 `xorm:"UNIQUE(follow)"`
11
+	FollowID int64 `xorm:"UNIQUE(follow)"`
12
+}
13
+
14
+// IsFollowing returns true if user is following followID.
15
+func IsFollowing(userID, followID int64) bool {
16
+	has, _ := x.Get(&Follow{UserID: userID, FollowID: followID})
17
+	return has
18
+}
19
+
20
+// FollowUser marks someone be another's follower.
21
+func FollowUser(userID, followID int64) (err error) {
22
+	if userID == followID || IsFollowing(userID, followID) {
23
+		return nil
24
+	}
25
+
26
+	sess := x.NewSession()
27
+	defer sessionRelease(sess)
28
+	if err = sess.Begin(); err != nil {
29
+		return err
30
+	}
31
+
32
+	if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil {
33
+		return err
34
+	}
35
+
36
+	if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
37
+		return err
38
+	}
39
+
40
+	if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
41
+		return err
42
+	}
43
+	return sess.Commit()
44
+}
45
+
46
+// UnfollowUser unmarks someone as another's follower.
47
+func UnfollowUser(userID, followID int64) (err error) {
48
+	if userID == followID || !IsFollowing(userID, followID) {
49
+		return nil
50
+	}
51
+
52
+	sess := x.NewSession()
53
+	defer sessionRelease(sess)
54
+	if err = sess.Begin(); err != nil {
55
+		return err
56
+	}
57
+
58
+	if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil {
59
+		return err
60
+	}
61
+
62
+	if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
63
+		return err
64
+	}
65
+
66
+	if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
67
+		return err
68
+	}
69
+	return sess.Commit()
70
+}
71
+

+ 45 - 0
models/user_follow_test.go

@@ -0,0 +1,45 @@
1
+package models
2
+
3
+import (
4
+	"testing"
5
+
6
+	"github.com/stretchr/testify/assert"
7
+)
8
+
9
+func TestIsFollowing(t *testing.T) {
10
+	assert.NoError(t, PrepareTestDatabase())
11
+	assert.True(t, IsFollowing(4, 2))
12
+	assert.False(t, IsFollowing(2, 4))
13
+	assert.False(t, IsFollowing(5, NonexistentID))
14
+	assert.False(t, IsFollowing(NonexistentID, 5))
15
+	assert.False(t, IsFollowing(NonexistentID, NonexistentID))
16
+}
17
+
18
+func TestFollowUser(t *testing.T) {
19
+	assert.NoError(t, PrepareTestDatabase())
20
+
21
+	testSuccess := func(followerID, followedID int64) {
22
+		assert.NoError(t, FollowUser(followerID, followedID))
23
+		AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID})
24
+	}
25
+	testSuccess(4, 2)
26
+	testSuccess(5, 2)
27
+
28
+	assert.NoError(t, FollowUser(2, 2))
29
+
30
+	CheckConsistencyFor(t, &User{})
31
+}
32
+
33
+func TestUnfollowUser(t *testing.T) {
34
+	assert.NoError(t, PrepareTestDatabase())
35
+
36
+	testSuccess := func(followerID, followedID int64) {
37
+		assert.NoError(t, UnfollowUser(followerID, followedID))
38
+		AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID})
39
+	}
40
+	testSuccess(4, 2)
41
+	testSuccess(5, 2)
42
+	testSuccess(2, 2)
43
+
44
+	CheckConsistencyFor(t, &User{})
45
+}