From 83b292c9bae4d9f293a80147bb2f2b1c832214a7 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Thu, 12 Mar 2020 08:46:14 +0800 Subject: [PATCH] Modify: travis CI build and test --- .travis.yml | 12 ++++++--- models/group_test.go | 4 +-- models/share_test.go | 62 ------------------------------------------- models/user_test.go | 63 +++++--------------------------------------- 4 files changed, 18 insertions(+), 123 deletions(-) diff --git a/.travis.yml b/.travis.yml index e763275..01d5da5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,16 @@ git: depth: 1 install: - go get github.com/rakyll/statik - - sudo apt-get -y install gcc-mingw-w64-x86-64 - - sudo apt-get -y install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross before_script: - statik -src=models -f - - chmod +x ./build.sh script: + - go test -coverprofile=coverage.txt -covermode=atomic ./... +after_success: + - bash <(curl -s https://codecov.io/bash) +before_deploy: + - sudo apt-get -y install gcc-mingw-w64-x86-64 + - sudo apt-get -y install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross + - chmod +x ./build.sh - ./build.sh -r b deploy: provider: releases @@ -19,6 +23,8 @@ deploy: file: release/* draft: true skip_cleanup: true + on: + tags: true # script: # - go test -coverprofile=coverage.txt -covermode=atomic ./... # after_success: diff --git a/models/group_test.go b/models/group_test.go index caa297c..2f487ce 100644 --- a/models/group_test.go +++ b/models/group_test.go @@ -23,7 +23,7 @@ func TestGetGroupByID(t *testing.T) { ID: 1, }, Name: "管理员", - PolicyID: 1, + Policies: "[1]", PolicyList: []uint{1}, }, group) @@ -42,7 +42,7 @@ func TestGroup_AfterFind(t *testing.T) { ID: 1, }, Name: "管理员", - PolicyID: 1, + Policies: "[1]", } err := testCase.AfterFind() asserts.NoError(err) diff --git a/models/share_test.go b/models/share_test.go index a8fb364..0f0c185 100644 --- a/models/share_test.go +++ b/models/share_test.go @@ -196,7 +196,6 @@ func TestShare_CanBeDownloadBy(t *testing.T) { }, }, } - share.Score = 1 asserts.Error(share.CanBeDownloadBy(user)) } @@ -210,7 +209,6 @@ func TestShare_CanBeDownloadBy(t *testing.T) { }, }, } - share.Score = 1 asserts.NoError(share.CanBeDownloadBy(user)) } } @@ -260,66 +258,6 @@ func TestShare_DownloadBy(t *testing.T) { asserts.True(ok) } -func TestShare_Purchase(t *testing.T) { - asserts := assert.New(t) - - // 不需要购买 - { - share := Share{} - user := User{} - asserts.NoError(share.Purchase(&user)) - - share.Score = 1 - user.Group.OptionsSerialized.ShareFree = true - asserts.NoError(share.Purchase(&user)) - - user.Group.OptionsSerialized.ShareFree = false - share.UserID = 1 - user.ID = 1 - asserts.NoError(share.Purchase(&user)) - } - - // 积分不足 - { - share := Share{ - Score: 1, - UserID: 2, - } - user := User{} - asserts.Error(share.Purchase(&user)) - } - - // 成功 - { - cache.Set("setting_share_score_rate", "80", 0) - share := Share{ - Score: 10, - UserID: 2, - User: User{ - Model: gorm.Model{ - ID: 1, - }, - }, - } - user := User{ - Score: 10, - } - - mock.ExpectBegin() - mock.ExpectExec("UPDATE(.+)"). - WillReturnResult(sqlmock.NewResult(1, 1)) - mock.ExpectCommit() - mock.ExpectBegin() - mock.ExpectExec("UPDATE(.+)"). - WillReturnResult(sqlmock.NewResult(1, 1)) - mock.ExpectCommit() - asserts.NoError(share.Purchase(&user)) - asserts.NoError(mock.ExpectationsWereMet()) - asserts.EqualValues(0, user.Score) - asserts.EqualValues(8, share.User.Score) - } -} - func TestShare_Viewed(t *testing.T) { asserts := assert.New(t) share := Share{} diff --git a/models/user_test.go b/models/user_test.go index b12f261..530c19c 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -188,37 +188,15 @@ func TestUser_GetPolicyID(t *testing.T) { asserts := assert.New(t) newUser := NewUser() + newUser.Group.PolicyList = []uint{1} - testCases := []struct { - preferred uint - available []uint - expected uint - }{ - { - available: []uint{1}, - expected: 1, - }, - { - available: []uint{5, 2, 3}, - expected: 5, - }, - { - preferred: 1, - available: []uint{5, 1, 3}, - expected: 1, - }, - { - preferred: 9, - available: []uint{5, 1, 3}, - expected: 5, - }, - } + asserts.EqualValues(1, newUser.GetPolicyID(0)) - for key, testCase := range testCases { - newUser.OptionsSerialized.PreferredPolicy = testCase.preferred - newUser.Group.PolicyList = testCase.available - asserts.Equal(testCase.expected, newUser.GetPolicyID(0), "测试用例 #%d 未通过", key) - } + newUser.Group.PolicyList = nil + asserts.EqualValues(0, newUser.GetPolicyID(0)) + + newUser.Group.PolicyList = []uint{} + asserts.EqualValues(0, newUser.GetPolicyID(0)) } func TestUser_GetRemainingCapacity(t *testing.T) { @@ -395,33 +373,6 @@ func TestUser_Root(t *testing.T) { } } -func TestUser_PayScore(t *testing.T) { - asserts := assert.New(t) - user := User{Score: 5} - - asserts.True(user.PayScore(0)) - asserts.False(user.PayScore(10)) - - mock.ExpectBegin() - mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1)) - mock.ExpectCommit() - asserts.True(user.PayScore(5)) - asserts.EqualValues(0, user.Score) - asserts.NoError(mock.ExpectationsWereMet()) -} - -func TestUser_AddScore(t *testing.T) { - asserts := assert.New(t) - user := User{Score: 5} - - mock.ExpectBegin() - mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1)) - mock.ExpectCommit() - user.AddScore(5) - asserts.NoError(mock.ExpectationsWereMet()) - asserts.EqualValues(10, user.Score) -} - func TestNewAnonymousUser(t *testing.T) { asserts := assert.New(t)