Skip to content

Commit

Permalink
test(tests): add acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Mar 12, 2020
1 parent c248661 commit 98cb2ba
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 16 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ web-test:
gotest -v ./tests/web/... -args $(WEB_TEST_CONFIG_PATH)
rm -rf ./tests/web/User*

unit-test:
gotest -v ./adm/...
gotest -v ./context/...
gotest -v ./modules/auth/...
gotest -v ./modules/collection/...
gotest -v ./modules/config/...
gotest -v ./modules/db/...
gotest -v ./modules/language/...
gotest -v ./modules/logger/...
gotest -v ./modules/menu/...
gotest -v ./modules/utils/...
gotest -v ./plugins/admin/controller/...
gotest -v ./plugins/admin/modules/parameter/...
gotest -v ./plugins/admin/modules/table/...
gotest -v ./plugins/admin/modules/...

fix-gf:
go get -u -v github.com/gogf/[email protected]
sudo echo "\nfunc (s *Server) DefaultHttpHandle(w http.ResponseWriter, r *http.Request) { \n s.handleRequest(w, r) \n}\n" >> $(GOPATH)/pkg/mod/github.com/gogf/[email protected]/net/ghttp/ghttp_server_handler.go
Expand Down
14 changes: 13 additions & 1 deletion modules/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ func TestConfig_GetIndexUrl(t *testing.T) {

assert.Equal(t, Get().GetIndexURL(), "/admin")

count = 0
Set(Config{
UrlPrefix: "/admin",
IndexUrl: "/",
})

assert.Equal(t, Get().GetIndexURL(), "/admin")

count = 0
Set(Config{
UrlPrefix: "/admin",
IndexUrl: "/",
Expand All @@ -29,6 +31,7 @@ func TestConfig_GetIndexUrl(t *testing.T) {
}

func TestConfig_Index(t *testing.T) {
count = 0
Set(Config{
UrlPrefix: "admin",
IndexUrl: "/",
Expand All @@ -38,13 +41,15 @@ func TestConfig_Index(t *testing.T) {
}

func TestConfig_Prefix(t *testing.T) {
count = 0
Set(Config{
UrlPrefix: "admin",
IndexUrl: "/",
})

assert.Equal(t, Get().Prefix(), "/admin")

count = 0
Set(Config{
UrlPrefix: "/admin",
IndexUrl: "/",
Expand All @@ -54,13 +59,15 @@ func TestConfig_Prefix(t *testing.T) {
}

func TestConfig_Url(t *testing.T) {
count = 0
Set(Config{
UrlPrefix: "admin",
IndexUrl: "/",
})

assert.Equal(t, Get().Url("/info/user"), "/admin/info/user")

count = 0
Set(Config{
UrlPrefix: "/admin",
IndexUrl: "/",
Expand All @@ -72,6 +79,7 @@ func TestConfig_Url(t *testing.T) {

func TestConfig_UrlRemovePrefix(t *testing.T) {

count = 0
Set(Config{
UrlPrefix: "/admin",
IndexUrl: "/",
Expand All @@ -82,13 +90,15 @@ func TestConfig_UrlRemovePrefix(t *testing.T) {

func TestConfig_PrefixFixSlash(t *testing.T) {

count = 0
Set(Config{
UrlPrefix: "/admin",
IndexUrl: "/",
})

assert.Equal(t, Get().PrefixFixSlash(), "/admin")

count = 0
Set(Config{
UrlPrefix: "admin",
IndexUrl: "/",
Expand All @@ -103,7 +113,9 @@ func TestSetDefault(t *testing.T) {
}

func TestSet(t *testing.T) {
count = 0
Set(Config{Theme: "abc"})
count = 0
Set(Config{Theme: "bcd"})
assert.Equal(t, Get().Theme, "abc")
assert.Equal(t, Get().Theme, "bcd")
}
25 changes: 12 additions & 13 deletions modules/db/statement_postgresql_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package db

import (
"fmt"
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/postgres"
"golang.org/x/tools/go/ssa/interp/testdata/src/fmt"
"os"
"os/exec"
"path"
"testing"
)

var driverTestPgConn Connection

func init() {

conn := testConnDSN(DriverPostgresql, fmt.Sprintf("host=127.0.0.1 port=5433 user=postgres "+
"password=root dbname=%s sslmode=disable", driverTestDBName))

_, err := conn.Exec(fmt.Sprintf("SELECT 'CREATE DATABASE %s' WHERE NOT EXISTS "+
"(SELECT FROM pg_database WHERE datname = '%s')", driverTestDBName, driverTestDBName))

if err != nil {
panic(err)
}
cmd := exec.Command("createdb -p 5433 -U postgres " + driverTestDBName)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PGPASSWORD=root")
_ = cmd.Run()

cmd := exec.Command("export PGPASSWORD=root psql", "-h", "localhost", "-U", "root", "-proot", "-d", driverTestDBName,
"-f", testCurrentPath()+"/../../data/admin.pgsql")
err = cmd.Run()
cmd = exec.Command("psql", "-h", "localhost", "-U", "root", "-proot", "-d", driverTestDBName,
"-f", path.Dir(path.Dir(testCurrentPath()))+"/data/admin.pgsql")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PGPASSWORD=root")
err := cmd.Run()
if err != nil {
panic(err)
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (admin *Admin) InitPlugin(services service.List) {

cfg := config.Get()

// TODO: find a better way to manage the dependencies

admin.services = services
admin.conn = db.GetConnection(admin.services)
st := table.NewSystemTable(admin.conn)
Expand Down
2 changes: 1 addition & 1 deletion tests/tables/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func GetUserTable(ctx *context.Context) (userTable table.Table) {
}))
info.AddActionButton("Preview", action.PopUp("/admin/preview", "Preview",
func(ctx *context.Context) (success bool, msg string, data interface{}) {
return true, "", "<h2>hello world</h2>"
return true, "", "<h2>preview content</h2>"
}))
info.AddButton("jump", icon.User, action.JumpInNewTab("/admin/info/authors", "authors"))
info.AddButton("popup", icon.Terminal, action.PopUp("/admin/popup", "Popup Example",
Expand Down
4 changes: 4 additions & 0 deletions tests/web/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func printlnWithColor(msg string, color string) {
fmt.Println(ansi.Color(msg, color))
}

func fill(t *testing.T, xpath, content string) {
assert.Equal(t, page.FindByXPath(xpath).Fill(content), nil)
}

const (
colorBlue = "blue"
colorGreen = "green"
Expand Down
89 changes: 88 additions & 1 deletion tests/web/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func TestInfoTablePageOperations(t *testing.T) {
rowSelectAvatarCheckbox = `//*[@id="pjax-container"]/section[2]/div/div/div[1]/div/div[1]/ul/li[1]/ul/li[5]/label/div`
actionDropDown = `//*[@id="pjax-container"]/section[2]/div/div/div[3]/table/tbody/tr[2]/td[1]/div`
exportBtn = `//*[@id="pjax-container"]/section[2]/div/div/div[1]/span/div/button`
rowActionDropDown = `//*[@id="pjax-container"]/section[2]/div/div/div[3]/table/tbody/tr[2]/td[7]/div/a`
previewAction = `//*[@id="pjax-container"]/section[2]/div/div/div[3]/table/tbody/tr[2]/td[7]/div/ul/li[7]/a`
closePreviewAction = `//*[@id="pjax-container"]/section[2]/div/div/div[4]/div[2]/div/div/div[3]/button`
previewPopup = `//*[@id="pjax-container"]/section[2]/div/div/div[4]/div[2]`
rowAjaxAction = `//*[@id="pjax-container"]/section[2]/div/div/div[3]/table/tbody/tr[2]/td[7]/div/ul/li[6]/a`
rowAjaxPopup = `/html/body/div[3]`
closeRowAjaxPopup = `/html/body/div[3]/div[7]/div/button`
)

assert.Equal(t, page.Navigate(url(config.Get().Url("/info/user"))), nil)
Expand Down Expand Up @@ -160,10 +167,90 @@ func TestInfoTablePageOperations(t *testing.T) {
// Action Button Check
// =============================

click(t, rowActionDropDown)
sleep(1)
click(t, previewAction)
sleep(1)
contain(t, "preview content")
sleep(1)
click(t, closePreviewAction)
css(t, page.FindByXPath(previewPopup), "display", "block")

click(t, closePreviewAction)

sleep(1)

css(t, page.FindByXPath(previewPopup), "display", "none")

click(t, rowActionDropDown)
click(t, rowAjaxAction)

css(t, page.FindByXPath(rowAjaxPopup), "display", "block")

click(t, closeRowAjaxPopup)

sleep(1)

css(t, page.FindByXPath(rowAjaxPopup), "display", "none")

sleep(2)
}

func testNewPageOperations(t *testing.T) {
func TestNewPageOperations(t *testing.T) {
const (
newPageBtn = `//*[@id="pjax-container"]/section[2]/div/div/div[1]/div/div[3]/a`
saveBtn = `//*[@id="pjax-container"]/section[2]/div/div/div[2]/form/div[2]/div[2]/div[1]/button`
resetBtn = `//*[@id="pjax-container"]/section[2]/div/div[2]/div[2]/form/div[2]/div[2]/div[2]/button`
nameField = `//*[@id="name"]`
ageField = `//*[@id="age"]`
passwordField = `//*[@id="password"]`
ipField = `//*[@id="ip"]`
amountField = `//*[@id="currency"]`
fruitOptField = `//*[@id="bootstrap-duallistbox-nonselected-list_fruit[]"]/option[1]`
genderBoyCheckBox = `//*[@id="tab-form-1"]/div[3]/div/div[1]`
experienceDropDown = `//*[@id="tab-form-1"]/div[5]/div/span/span[1]/span/span[2]`
twoYearsSelection = `/html/body/span/span/span[2]/ul/li[1]`
inputTab = `//*[@id="pjax-container"]/section[2]/div/div[2]/div[2]/form/div[1]/div/div/ul/li[1]`
)

click(t, newPageBtn)
sleep(2)

click(t, saveBtn)
contain(t, "error")
fillNewForm(t)
click(t, resetBtn)
click(t, inputTab)
text(t, page.FindByXPath(ipField), "")
fillNewForm(t)
click(t, saveBtn)
}

func fillNewForm(t *testing.T) {
const (
nameField = `//*[@id="name"]`
ageField = `//*[@id="age"]`
passwordField = `//*[@id="password"]`
ipField = `//*[@id="ip"]`
amountField = `//*[@id="currency"]`
fruitOptField = `//*[@id="bootstrap-duallistbox-nonselected-list_fruit[]"]/option[1]`
genderGirlCheckBox = `//*[@id="tab-form-1"]/div[3]/div/div[2]`
experienceDropDown = `//*[@id="tab-form-1"]/div[5]/div/span/span[1]/span/span[2]`
twoYearsSelection = `/html/body/span/span/span[2]/ul/li[1]`
selectTab = `//*[@id="pjax-container"]/section[2]/div/div[2]/div[2]/form/div[1]/div/div/ul/li[2]`
)

fill(t, nameField, "jane")
fill(t, ageField, "15")
fill(t, passwordField, "12345678")
fill(t, ipField, "127.0.0.1")
fill(t, amountField, "15")
click(t, selectTab)
click(t, fruitOptField)
click(t, genderGirlCheckBox)
click(t, experienceDropDown)
sleep(1)
click(t, twoYearsSelection)
}

func testDetailPageOperations(t *testing.T) {
Expand Down

0 comments on commit 98cb2ba

Please sign in to comment.