Added an alternate builder syntax and started using it in a few places.

This commit is contained in:
Azareal 2017-11-11 08:46:30 +00:00
parent fb8fa10570
commit 7ac3de8299
5 changed files with 226 additions and 109 deletions

View File

@ -75,7 +75,7 @@ func init() {
acc := qgen.Builder.Accumulator()
replyStmts = ReplyStmts{
isLiked: acc.SimpleSelect("likes", "targetItem", "sentBy = ? and targetItem = ? and targetType = 'replies'", "", ""),
createLike: acc.SimpleInsert("likes", "weight, targetItem, targetType, sentBy", "?,?,?,?"),
createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(),
delete: acc.SimpleDelete("replies", "rid = ?"),
addLikesToReply: acc.SimpleUpdate("replies", "likeCount = likeCount + ?", "rid = ?"),
removeRepliesFromTopic: acc.SimpleUpdate("topics", "postCount = postCount - ?", "tid = ?"),

View File

@ -129,18 +129,19 @@ func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
topicStmts = TopicStmts{
addRepliesToTopic: acc.SimpleUpdate("topics", "postCount = postCount + ?, lastReplyBy = ?, lastReplyAt = UTC_TIMESTAMP()", "tid = ?"),
lock: acc.SimpleUpdate("topics", "is_closed = 1", "tid = ?"),
unlock: acc.SimpleUpdate("topics", "is_closed = 0", "tid = ?"),
stick: acc.SimpleUpdate("topics", "sticky = 1", "tid = ?"),
unstick: acc.SimpleUpdate("topics", "sticky = 0", "tid = ?"),
hasLikedTopic: acc.SimpleSelect("likes", "targetItem", "sentBy = ? and targetItem = ? and targetType = 'topics'", "", ""),
createLike: acc.SimpleInsert("likes", "weight, targetItem, targetType, sentBy", "?,?,?,?"),
addLikesToTopic: acc.SimpleUpdate("topics", "likeCount = likeCount + ?", "tid = ?"),
delete: acc.SimpleDelete("topics", "tid = ?"),
edit: acc.SimpleUpdate("topics", "title = ?, content = ?, parsed_content = ?", "tid = ?"),
createActionReply: acc.SimpleInsert("replies", "tid, actionType, ipaddress, createdBy, createdAt, lastUpdated, content, parsed_content", "?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),'',''"),
getTopicUser: acc.SimpleLeftJoin("topics", "users", "topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.url_prefix, users.url_name, users.level", "topics.createdBy = users.uid", "tid = ?", "", ""),
addRepliesToTopic: acc.Update("topics").Set("postCount = postCount + ?, lastReplyBy = ?, lastReplyAt = UTC_TIMESTAMP()").Where("tid = ?").Prepare(),
lock: acc.Update("topics").Set("is_closed = 1").Where("tid = ?").Prepare(),
unlock: acc.Update("topics").Set("is_closed = 0").Where("tid = ?").Prepare(),
stick: acc.Update("topics").Set("sticky = 1").Where("tid = ?").Prepare(),
unstick: acc.Update("topics").Set("sticky = 0").Where("tid = ?").Prepare(),
hasLikedTopic: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'topics'").Prepare(),
createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(),
addLikesToTopic: acc.Update("topics").Set("likeCount = likeCount + ?").Where("tid = ?").Prepare(),
delete: acc.Delete("topics").Where("tid = ?").Prepare(),
edit: acc.Update("topics").Set("title = ?, content = ?, parsed_content = ?").Where("tid = ?").Prepare(),
createActionReply: acc.Insert("replies").Columns("tid, actionType, ipaddress, createdBy, createdAt, lastUpdated, content, parsed_content").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),'',''").Prepare(),
getTopicUser: acc.SimpleLeftJoin("topics", "users", "topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.url_prefix, users.url_name, users.level", "topics.createdBy = users.uid", "tid = ?", "", ""),
}
return acc.FirstError()
})

View File

@ -1,24 +1,21 @@
package main
import (
//"os"
"bytes"
"database/sql"
"errors"
"log"
"strconv"
"strings"
//"math/rand"
"database/sql"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
"./common"
"./install/install"
"./query_gen/lib"
//"runtime/pprof"
//_ "github.com/go-sql-driver/mysql"
//"github.com/erikstmartin/go-testdb"
//"github.com/husobee/vestigo"
)
@ -75,16 +72,16 @@ func gloinit() (err error) {
return err
}
err = initDatabase()
err = InitDatabase()
if err != nil {
return err
}
rstore, err = NewSQLReplyStore()
common.Rstore, err = common.NewSQLReplyStore()
if err != nil {
return err
}
prstore, err = NewSQLProfileReplyStore()
common.Prstore, err = common.NewSQLProfileReplyStore()
if err != nil {
return err
}
@ -95,25 +92,29 @@ func gloinit() (err error) {
// return err
//}
err = initTemplates()
err = common.InitTemplates()
if err != nil {
return err
}
dbProd.SetMaxOpenConns(64)
err = initPhrases()
err = common.InitPhrases()
if err != nil {
log.Fatal(err)
}
log.Print("Loading the static files.")
err = initStaticFiles()
err = common.StaticFiles.Init()
if err != nil {
return err
}
auth = NewDefaultAuth()
err = LoadWordFilters()
common.Auth, err = common.NewDefaultAuth()
if err != nil {
return err
}
err = common.LoadWordFilters()
if err != nil {
return err
}
@ -142,15 +143,15 @@ func BenchmarkTopicAdminRouteParallel(b *testing.B) {
}
b.RunParallel(func(pb *testing.PB) {
admin, err := users.Get(1)
admin, err := common.Users.Get(1)
if err != nil {
b.Fatal(err)
}
if !admin.IsAdmin {
b.Fatal("UID1 is not an admin")
}
adminUIDCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: year}
adminSessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: year}
adminUIDCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: common.Year}
adminSessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: common.Year}
topicW := httptest.NewRecorder()
topicReq := httptest.NewRequest("get", "/topic/1", bytes.NewReader(nil))
@ -159,7 +160,7 @@ func BenchmarkTopicAdminRouteParallel(b *testing.B) {
topicReqAdmin.AddCookie(&adminSessionCookie)
// Deal with the session stuff, etc.
user, ok := PreRoute(topicW, topicReqAdmin)
user, ok := common.PreRoute(topicW, topicReqAdmin)
if !ok {
b.Fatal("Mysterious error!")
}
@ -185,7 +186,7 @@ func BenchmarkTopicGuestRouteParallel(b *testing.B) {
topicReq := httptest.NewRequest("get", "/topic/1", bytes.NewReader(nil))
for pb.Next() {
topicW.Body.Reset()
routeTopicID(topicW, topicReq, guestUser)
routeTopicID(topicW, topicReq, common.GuestUser)
}
})
}
@ -522,7 +523,7 @@ func BenchmarkQueryTopicParallel(b *testing.B) {
}
b.RunParallel(func(pb *testing.PB) {
var tu TopicUser
var tu common.TopicUser
for pb.Next() {
err := db.QueryRow("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.url_prefix, users.url_name, users.level from topics left join users ON topics.createdBy = users.uid where tid = ?", 1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IPAddress, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.URLPrefix, &tu.URLName, &tu.Level)
if err == ErrNoRows {
@ -546,9 +547,15 @@ func BenchmarkQueryPreparedTopicParallel(b *testing.B) {
}
b.RunParallel(func(pb *testing.PB) {
var tu TopicUser
var tu common.TopicUser
getTopicUser, err := qgen.Builder.SimpleLeftJoin("topics", "users", "topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.url_prefix, users.url_name, users.level", "topics.createdBy = users.uid", "tid = ?", "", "")
if err != nil {
b.Fatal(err)
}
for pb.Next() {
err := stmts.getTopicUser.QueryRow(1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IPAddress, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.URLPrefix, &tu.URLName, &tu.Level)
err := getTopicUser.QueryRow(1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IPAddress, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.URLPrefix, &tu.URLName, &tu.Level)
if err == ErrNoRows {
b.Fatal("No rows found!")
return
@ -572,7 +579,7 @@ func BenchmarkUserGet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var err error
for pb.Next() {
_, err = users.Get(1)
_, err = common.Users.Get(1)
if err != nil {
b.Fatal(err)
return
@ -594,7 +601,7 @@ func BenchmarkUserBypassGet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var err error
for pb.Next() {
_, err = users.BypassGet(1)
_, err = common.Users.BypassGet(1)
if err != nil {
b.Fatal(err)
return
@ -605,7 +612,7 @@ func BenchmarkUserBypassGet(b *testing.B) {
func BenchmarkQueriesSerial(b *testing.B) {
b.ReportAllocs()
var tu TopicUser
var tu common.TopicUser
b.Run("topic", func(b *testing.B) {
for i := 0; i < b.N; i++ {
err := db.QueryRow("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.url_prefix, users.url_name, users.level from topics left join users ON topics.createdBy = users.uid where tid = ?", 1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IPAddress, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.URLPrefix, &tu.URLName, &tu.Level)
@ -637,7 +644,7 @@ func BenchmarkQueriesSerial(b *testing.B) {
}
})
var replyItem ReplyUser
var replyItem common.ReplyUser
var isSuperAdmin bool
var group int
b.Run("topic_replies_scan", func(b *testing.B) {
@ -997,32 +1004,32 @@ func BenchmarkParserSerial(b *testing.B) {
b.ReportAllocs()
b.Run("empty_post", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = parseMessage("", 0, "")
_ = common.ParseMessage("", 0, "")
}
})
b.Run("short_post", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = parseMessage("Hey everyone, how's it going?", 0, "")
_ = common.ParseMessage("Hey everyone, how's it going?", 0, "")
}
})
b.Run("one_smily", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = parseMessage("Hey everyone, how's it going? :)", 0, "")
_ = common.ParseMessage("Hey everyone, how's it going? :)", 0, "")
}
})
b.Run("five_smilies", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = parseMessage("Hey everyone, how's it going? :):):):):)", 0, "")
_ = common.ParseMessage("Hey everyone, how's it going? :):):):):)", 0, "")
}
})
b.Run("ten_smilies", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = parseMessage("Hey everyone, how's it going? :):):):):):):):):):)", 0, "")
_ = common.ParseMessage("Hey everyone, how's it going? :):):):):):):):):):)", 0, "")
}
})
b.Run("twenty_smilies", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = parseMessage("Hey everyone, how's it going? :):):):):):):):):):):):):):):):):):):):)", 0, "")
_ = common.ParseMessage("Hey everyone, how's it going? :):):):):):):):):):):):):):):):):):):):)", 0, "")
}
})
}
@ -1175,7 +1182,7 @@ func BenchmarkBBCodePluginWithFullParserSerial(b *testing.B) {
}
func TestLevels(t *testing.T) {
levels := getLevels(40)
levels := common.GetLevels(40)
for level, score := range levels {
sscore := strconv.FormatFloat(score, 'f', -1, 64)
t.Log("Level: " + strconv.Itoa(level) + " Score: " + sscore)

View File

@ -35,24 +35,24 @@ func TestUserStore(t *testing.T) {
t.Fatal(err)
}
}
if !pluginsInited {
initPlugins()
if !common.PluginsInited {
common.InitPlugins()
}
var err error
users, err = NewMemoryUserStore(config.UserCacheCapacity)
common.Users, err = common.NewMemoryUserStore(common.Config.UserCacheCapacity)
expectNilErr(t, err)
users.(UserCache).Flush()
common.Users.(common.UserCache).Flush()
userStoreTest(t, 2)
users, err = NewSQLUserStore()
common.Users, err = common.NewSQLUserStore()
expectNilErr(t, err)
userStoreTest(t, 3)
}
func userStoreTest(t *testing.T, newUserID int) {
ucache, hasCache := common.Users.(UserCache)
ucache, hasCache := common.Users.(common.UserCache)
// Go doesn't have short-circuiting, so this'll allow us to do one liner tests
if !hasCache {
ucache = &NullUserStore{}
ucache = &common.NullUserStore{}
}
expect(t, (!hasCache || ucache.Length() == 0), fmt.Sprintf("The initial ucache length should be zero, not %d", ucache.Length()))
@ -104,7 +104,7 @@ func userStoreTest(t *testing.T, newUserID int) {
}
// TODO: Lock onto the specific error type. Is this even possible without sacrificing the detailed information in the error message?
var userList map[int]*User
var userList map[int]*common.User
userList, _ = common.Users.BulkGetMap([]int{-1})
if len(userList) > 0 {
t.Error("There shouldn't be any results for UID #-1")
@ -135,7 +135,6 @@ func userStoreTest(t *testing.T, newUserID int) {
t.Error("We couldn't find UID #1 in the returned map")
t.Error("userList", userList)
}
if user.ID != 1 {
t.Error("user.ID does not match the requested UID. Got '" + strconv.Itoa(user.ID) + "' instead.")
}
@ -151,10 +150,10 @@ func userStoreTest(t *testing.T, newUserID int) {
ucache.Flush()
}
expect(t, !users.Exists(-1), "UID #-1 shouldn't exist")
expect(t, !users.Exists(0), "UID #0 shouldn't exist")
expect(t, !common.Users.Exists(-1), "UID #-1 shouldn't exist")
expect(t, !common.Users.Exists(0), "UID #0 shouldn't exist")
expect(t, common.Users.Exists(1), "UID #1 should exist")
expect(t, !users.Exists(newUserID), fmt.Sprintf("UID #%d shouldn't exist", newUserID))
expect(t, !common.Users.Exists(newUserID), fmt.Sprintf("UID #%d shouldn't exist", newUserID))
expect(t, !hasCache || ucache.Length() == 0, fmt.Sprintf("User cache length should be 0, not %d", ucache.Length()))
expectIntToBeX(t, common.Users.GlobalCount(), 1, "The number of users should be one, not %d")
@ -207,7 +206,7 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
expect(t, user.Group == config.DefaultGroup, fmt.Sprintf("Sam should be in group %d, not %d", config.DefaultGroup, user.Group))
expect(t, user.Group == common.Config.DefaultGroup, fmt.Sprintf("Sam should be in group %d, not %d", common.Config.DefaultGroup, user.Group))
// Permanent ban
duration, _ := time.ParseDuration("0")
@ -215,7 +214,7 @@ func userStoreTest(t *testing.T, newUserID int) {
// TODO: Attempt a double ban, double activation, and double unban
err = user.Ban(duration, 1)
expectNilErr(t, err)
expect(t, user.Group == config.DefaultGroup, fmt.Sprintf("Sam should be in group %d, not %d", config.DefaultGroup, user.Group))
expect(t, user.Group == common.Config.DefaultGroup, fmt.Sprintf("Sam should be in group %d, not %d", common.Config.DefaultGroup, user.Group))
if hasCache {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
@ -235,13 +234,13 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, user.IsBanned, "Sam should be banned")
expectIntToBeX(t, user.Group, banGroup, "Sam should be in group %d")
expectIntToBeX(t, user.Group, common.BanGroup, "Sam should be in group %d")
// TODO: Do tests against the scheduled updates table and the task system to make sure the ban exists there and gets revoked when it should
err = user.Unban()
expectNilErr(t, err)
expectIntToBeX(t, user.Group, banGroup, "Sam should still be in the ban group in this copy")
expectIntToBeX(t, user.Group, common.BanGroup, "Sam should still be in the ban group in this copy")
if hasCache {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
@ -259,7 +258,7 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
expectIntToBeX(t, user.Group, config.DefaultGroup, "Sam should be back in group %d")
expectIntToBeX(t, user.Group, common.Config.DefaultGroup, "Sam should be back in group %d")
var reportsForumID = 1
var generalForumID = 2
@ -270,12 +269,12 @@ func userStoreTest(t *testing.T, newUserID int) {
err = user.ChangeGroup(1)
expectNilErr(t, err)
expect(t, user.Group == config.DefaultGroup, "Someone's mutated this pointer elsewhere")
expect(t, user.Group == common.Config.DefaultGroup, "Someone's mutated this pointer elsewhere")
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectIntToBeX(t, user.ID, newUserID, "The UID of the user record should be %d")
var user2 *User = getDummyUser()
var user2 *common.User = common.BlankUser()
*user2 = *user
expect(t, !user.IsSuperAdmin, "Sam should not be a super admin")
@ -284,10 +283,10 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, user.IsMod, "Sam should be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
_, ferr := forumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
_, ferr := common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user.Perms.ViewTopic, "Admins should be able to access the reports forum")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
@ -298,7 +297,7 @@ func userStoreTest(t *testing.T, newUserID int) {
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectIntToBeX(t, user.ID, newUserID, "The UID of the user record should be %d")
user2 = getDummyUser()
user2 = common.BlankUser()
*user2 = *user
expect(t, !user.IsSuperAdmin, "Sam should not be a super admin")
@ -307,10 +306,10 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, user.IsMod, "Sam should be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user.Perms.ViewTopic, "Mods should be able to access the reports forum")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
@ -321,7 +320,7 @@ func userStoreTest(t *testing.T, newUserID int) {
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectIntToBeX(t, user.ID, newUserID, "The UID of the user record should be %d")
user2 = getDummyUser()
user2 = common.BlankUser()
*user2 = *user
expect(t, !user.IsSuperAdmin, "Sam should not be a super admin")
@ -330,10 +329,10 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
expect(t, user.Perms.ViewTopic != user2.Perms.ViewTopic, "user.Perms.ViewTopic and user2.Perms.ViewTopic should never match")
@ -345,7 +344,7 @@ func userStoreTest(t *testing.T, newUserID int) {
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectIntToBeX(t, user.ID, newUserID, "The UID of the user record should be %d")
user2 = getDummyUser()
user2 = common.BlankUser()
*user2 = *user
expect(t, !user.IsSuperAdmin, "Sam should not be a super admin")
@ -354,10 +353,10 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, user.IsBanned, "Sam should be banned")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
@ -368,7 +367,7 @@ func userStoreTest(t *testing.T, newUserID int) {
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectIntToBeX(t, user.ID, newUserID, "The UID of the user record should be %d")
user2 = getDummyUser()
user2 = common.BlankUser()
*user2 = *user
expect(t, !user.IsSuperAdmin, "Sam should not be a super admin")
@ -377,10 +376,10 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
@ -391,7 +390,7 @@ func userStoreTest(t *testing.T, newUserID int) {
user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectIntToBeX(t, user.ID, newUserID, "The UID of the user record should be %d")
user2 = getDummyUser()
user2 = common.BlankUser()
*user2 = *user
expect(t, !user.IsSuperAdmin, "Sam should not be a super admin")
@ -400,20 +399,20 @@ func userStoreTest(t *testing.T, newUserID int) {
expect(t, !user.IsMod, "Sam should not be a mod")
expect(t, !user.IsBanned, "Sam should not be banned")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest1, user, reportsForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, !user.Perms.ViewTopic, "Members shouldn't be able to access the reports forum")
_, ferr = forumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
_, ferr = common.ForumUserCheck(dummyResponseRecorder, dummyRequest2, user2, generalForumID)
expect(t, ferr == nil, "There shouldn't be any errors in forumUserCheck")
expect(t, user2.Perms.ViewTopic, "Sam should be able to access the general forum")
err = user.ChangeGroup(config.DefaultGroup)
err = user.ChangeGroup(common.Config.DefaultGroup)
expectNilErr(t, err)
expect(t, user.Group == 6, "Someone's mutated this pointer elsewhere")
err = user.Delete()
expectNilErr(t, err)
expect(t, !users.Exists(newUserID), fmt.Sprintf("UID #%d should no longer exist", newUserID))
expect(t, !common.Users.Exists(newUserID), fmt.Sprintf("UID #%d should no longer exist", newUserID))
if hasCache {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
@ -456,39 +455,39 @@ func TestPermsMiddleware(t *testing.T) {
t.Fatal(err)
}
}
if !pluginsInited {
initPlugins()
if !common.PluginsInited {
common.InitPlugins()
}
dummyResponseRecorder := httptest.NewRecorder()
bytesBuffer := bytes.NewBuffer([]byte(""))
dummyRequest := httptest.NewRequest("", "/forum/1", bytesBuffer)
user := getDummyUser()
user := common.BlankUser()
ferr := SuperModOnly(dummyResponseRecorder, dummyRequest, *user)
ferr := common.SuperModOnly(dummyResponseRecorder, dummyRequest, *user)
expect(t, ferr != nil, "Blank users shouldn't be supermods")
user.IsSuperMod = false
ferr = SuperModOnly(dummyResponseRecorder, dummyRequest, *user)
ferr = common.SuperModOnly(dummyResponseRecorder, dummyRequest, *user)
expect(t, ferr != nil, "Non-supermods shouldn't be allowed through supermod gates")
user.IsSuperMod = true
ferr = SuperModOnly(dummyResponseRecorder, dummyRequest, *user)
ferr = common.SuperModOnly(dummyResponseRecorder, dummyRequest, *user)
expect(t, ferr == nil, "Supermods should be allowed through supermod gates")
// TODO: Loop over the Control Panel routes and make sure only supermods can get in
user = getDummyUser()
user = common.BlankUser()
ferr = MemberOnly(dummyResponseRecorder, dummyRequest, *user)
ferr = common.MemberOnly(dummyResponseRecorder, dummyRequest, *user)
expect(t, ferr != nil, "Blank users shouldn't be considered loggedin")
user.Loggedin = false
ferr = MemberOnly(dummyResponseRecorder, dummyRequest, *user)
ferr = common.MemberOnly(dummyResponseRecorder, dummyRequest, *user)
expect(t, ferr != nil, "Guests shouldn't be able to access member areas")
user.Loggedin = true
ferr = MemberOnly(dummyResponseRecorder, dummyRequest, *user)
ferr = common.MemberOnly(dummyResponseRecorder, dummyRequest, *user)
expect(t, ferr == nil, "Logged in users should be able to access member areas")
// TODO: Loop over the /user/ routes and make sure only members can access the ones other than /user/username
@ -501,20 +500,20 @@ func TestTopicStore(t *testing.T) {
t.Fatal(err)
}
}
if !pluginsInited {
initPlugins()
if !common.PluginsInited {
common.InitPlugins()
}
var err error
topics, err = NewMemoryTopicStore(config.TopicCacheCapacity)
common.Topics, err = common.NewMemoryTopicStore(common.Config.TopicCacheCapacity)
expectNilErr(t, err)
topicStoreTest(t)
topics, err = NewSQLTopicStore()
common.Topics, err = common.NewSQLTopicStore()
expectNilErr(t, err)
topicStoreTest(t)
}
func topicStoreTest(t *testing.T) {
var topic *Topic
var topic *common.Topic
var err error
_, err = common.Topics.Get(-1)
@ -560,17 +559,17 @@ func TestForumStore(t *testing.T) {
if !gloinited {
gloinit()
}
if !pluginsInited {
initPlugins()
if !common.PluginsInited {
common.InitPlugins()
}
_, err := fstore.Get(-1)
_, err := common.Fstore.Get(-1)
recordMustNotExist(t, err, "FID #-1 shouldn't exist")
_, err = fstore.Get(0)
_, err = common.Fstore.Get(0)
recordMustNotExist(t, err, "FID #0 shouldn't exist")
forum, err := fstore.Get(1)
forum, err := common.Fstore.Get(1)
recordMustExist(t, err, "Couldn't find FID #1")
if forum.ID != 1 {
@ -582,7 +581,7 @@ func TestForumStore(t *testing.T) {
var expectDesc = "All the reports go here"
expect(t, forum.Desc == expectDesc, fmt.Sprintf("The forum description should be '%s' not '%s'", expectDesc, forum.Desc))
forum, err = fstore.Get(2)
forum, err = common.Fstore.Get(2)
recordMustExist(t, err, "Couldn't find FID #1")
expect(t, forum.ID == 2, fmt.Sprintf("The FID should be 2 not %d", forum.ID))
@ -591,11 +590,11 @@ func TestForumStore(t *testing.T) {
expectDesc = "A place for general discussions which don't fit elsewhere"
expect(t, forum.Desc == expectDesc, fmt.Sprintf("The forum description should be '%s' not '%s'", expectDesc, forum.Desc))
ok := fstore.Exists(-1)
ok := common.Fstore.Exists(-1)
expect(t, !ok, "FID #-1 shouldn't exist")
ok = fstore.Exists(0)
ok = common.Fstore.Exists(0)
expect(t, !ok, "FID #0 shouldn't exist")
ok = fstore.Exists(1)
ok = common.Fstore.Exists(1)
expect(t, ok, "FID #1 should exist")
// TODO: Test forum creation

View File

@ -186,3 +186,113 @@ func (build *accBuilder) PurgeTx(tx *sql.Tx, table string) (stmt *sql.Stmt) {
res, err := build.adapter.Purge("_builder", table)
return build.prepareTx(tx, res, err)
}
func (build *accBuilder) Delete(table string) *deleteBuilder {
return &deleteBuilder{table, "", build}
}
type deleteBuilder struct {
table string
where string
build *accBuilder
}
func (delete *deleteBuilder) Where(where string) *deleteBuilder {
delete.where = where
return delete
}
func (delete *deleteBuilder) Prepare() *sql.Stmt {
return delete.build.SimpleDelete(delete.table, delete.where)
}
func (build *accBuilder) Update(table string) *updateBuilder {
return &updateBuilder{table, "", "", build}
}
type updateBuilder struct {
table string
set string
where string
build *accBuilder
}
func (update *updateBuilder) Set(set string) *updateBuilder {
update.set = set
return update
}
func (update *updateBuilder) Where(where string) *updateBuilder {
update.where = where
return update
}
func (update *updateBuilder) Prepare() *sql.Stmt {
return update.build.SimpleUpdate(update.table, update.set, update.where)
}
func (build *accBuilder) Select(table string) *selectBuilder {
return &selectBuilder{table, "", "", "", "", build}
}
type selectBuilder struct {
table string
columns string
where string
orderby string
limit string
build *accBuilder
}
func (selectItem *selectBuilder) Columns(columns string) *selectBuilder {
selectItem.columns = columns
return selectItem
}
func (selectItem *selectBuilder) Where(where string) *selectBuilder {
selectItem.where = where
return selectItem
}
func (selectItem *selectBuilder) Orderby(orderby string) *selectBuilder {
selectItem.orderby = orderby
return selectItem
}
func (selectItem *selectBuilder) Limit(limit string) *selectBuilder {
selectItem.limit = limit
return selectItem
}
func (selectItem *selectBuilder) Prepare() *sql.Stmt {
return selectItem.build.SimpleSelect(selectItem.table, selectItem.columns, selectItem.where, selectItem.orderby, selectItem.limit)
}
func (build *accBuilder) Insert(table string) *insertBuilder {
return &insertBuilder{table, "", "", build}
}
type insertBuilder struct {
table string
columns string
fields string
build *accBuilder
}
func (insert *insertBuilder) Columns(columns string) *insertBuilder {
insert.columns = columns
return insert
}
func (insert *insertBuilder) Fields(fields string) *insertBuilder {
insert.fields = fields
return insert
}
func (insert *insertBuilder) Prepare() *sql.Stmt {
return insert.build.SimpleInsert(insert.table, insert.columns, insert.fields)
}