Skip to content

Commit

Permalink
Unit test for jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Mar 5, 2022
1 parent 48b4889 commit 81ed7eb
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 8 deletions.
32 changes: 31 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@ name: Build
on: push

jobs:
run_tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: website_admin_test
steps:
- name: Checkout
uses: actions/[email protected]

- uses: actions/setup-go@v2
with:
go-version: '^1.17.5'

- name: Run API test
run: |
go test jobs
env:
DB_HOST: localhost
DB_NAME: website_admin_test
SENDER: ${{ secrets.SENDER }}
RECEIVER: "[email protected]"
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}
REGION: ${{ secrets.AWS_REGION }}

build:
runs-on: ubuntu-latest
permissions:
Expand Down Expand Up @@ -29,4 +59,4 @@ jobs:
- name: Build backend
run: |
GOOS=linux GOARCH=amd64 go build -o main main.go
GOOS=linux GOARCH=amd64 go build -o main main.go
18 changes: 17 additions & 1 deletion db/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ func NewSql() *sqlx.DB {
var db *sqlx.DB
username := os.Getenv("DB_USERNAME")

if username == "" {
username = "root"
}

password := os.Getenv("DB_PASSWORD")

if password == "" {
password = "root"
}

host := os.Getenv("DB_HOST")

if host == "" {
host = "localhost"
}

port := os.Getenv("DB_PORT")

if port == "" {
port = "3306"
}

name := os.Getenv("DB_NAME")

db = sqlx.MustConnect("mysql", username+":"+password+"@tcp("+host+":"+port+")/"+name)
db = sqlx.MustConnect("mysql", username+":"+password+"@("+host+":"+port+")/"+name)

db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
db.SetConnMaxLifetime(time.Minute * 1)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ replace utils => ./utils

require (
contact v0.0.0-00010101000000-000000000000
db v0.0.0-00010101000000-000000000000
db v0.0.0
github.com/apex/gateway v1.1.2
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.7
github.com/jmoiron/sqlx v1.3.4
jobs v0.0.0-00010101000000-000000000000
jobs v0.0.0
sitemap v0.0.0-00010101000000-000000000000
)

Expand All @@ -40,12 +40,13 @@ require (
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/guregu/null.v3 v3.5.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
utils v0.0.0-00010101000000-000000000000 // indirect
utils v0.0.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
Expand Down
9 changes: 9 additions & 0 deletions jobs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@ go 1.17

replace utils => ../utils

replace db => ../db

require (
github.com/aws/aws-sdk-go v1.43.5
github.com/gin-gonic/gin v1.7.7
github.com/jmoiron/sqlx v1.3.4
github.com/sirupsen/logrus v1.8.1
github.com/tj/assert v0.0.3
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/guregu/null.v3 v3.5.0
utils v0.0.0-00010101000000-000000000000
)

require (
db v0.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
)
11 changes: 9 additions & 2 deletions jobs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -48,8 +49,11 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
Expand Down Expand Up @@ -82,3 +86,6 @@ gopkg.in/guregu/null.v3 v3.5.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4Yltd
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
173 changes: 173 additions & 0 deletions jobs/jobs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package jobs

import (
"bytes"
"embed"
"io"
"net/http"
"net/http/httptest"
"testing"
"utils"

"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
"github.com/tj/assert"
)

//go:embed templates/career-email-template.html
var templateFS embed.FS

var contentType string
var repo *CareerRepository
var err error
var testDB *sqlx.DB
var testRequests []utils.TestRequest

const (
GET_ALL_JOBS_API_URL = "/api/careers"
GET_JOBS_BY_ID_API_URL = "/api/careers/ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d"
SEND_CAREER_MAIL = "/api/send-career-mail"
)

func Test_init(t *testing.T) {
repo, err = initializeRepo()
assert.Nil(t, err)
testRequests = []utils.TestRequest{
{
Url: GET_ALL_JOBS_API_URL,
Method: "GET",
Headers: nil,
Body: nil,
ResponseCode: http.StatusOK,
ResponseTypeArray: true,
ExpectedData: []interface{}{expectedJobsData()},
},
{
Url: GET_JOBS_BY_ID_API_URL,
Method: "GET",
Headers: nil,
Body: nil,
ResponseCode: http.StatusOK,
ResponseTypeArray: false,
ExpectedData: expectedJobsData(),
},
{
Url: SEND_CAREER_MAIL,
Method: "POST",
Headers: map[string]interface{}{"Content-Type": contentType},
Body: prepareRequestBody(),
ResponseCode: http.StatusOK,
ResponseTypeArray: false,
ExpectedData: expectedMailData(),
},
}
}

func TestAllAPIs(t *testing.T) {

asserts := assert.New(t)
engine := gin.New()

setUpRouter(engine)

for _, testData := range testRequests {

w := httptest.NewRecorder()
var req *http.Request
var got interface{}

if testData.Body != nil {
req, err = http.NewRequest(testData.Method, testData.Url, testData.Body.(io.Reader))
} else {
req, err = http.NewRequest(testData.Method, testData.Url, nil)
}

asserts.NoError(err)

// set headers if present in request
utils.SetRequestHeaders(req, testData.Headers, contentType)

engine.ServeHTTP(w, req)
assert.Equal(t, testData.ResponseCode, w.Code)
if testData.ResponseTypeArray {
got = utils.GotArrayData(w, t)
} else {
got = utils.GotData(w, t)
}

assert.Equal(t, testData.ExpectedData, got)

}
}

func initializeRepo() (*CareerRepository, error) {

testDB, err = utils.TestDB()
if err != nil {
return nil, err
}

err = utils.CreateTables(testDB)
if err != nil {
return nil, err
}

utils.TruncateTables(testDB)
utils.PrepareTablesData(testDB)

repo = New(testDB, templateFS)

return repo, err
}

// configure api you want to test
func setUpRouter(engine *gin.Engine) {
engine.GET(GET_ALL_JOBS_API_URL, repo.Careers)
engine.GET("/api/careers/:id", repo.CareerById)
engine.POST(SEND_CAREER_MAIL, repo.SendCareerMail)
}

func expectedJobsData() map[string]interface{} {
jobs := make(map[string]interface{})
jobs["id"] = 1.0
jobs["title"] = "IOS Developer"
jobs["summary"] = "We have an opening for a passionate iOS developer who can develop and maintain applications for iOS devices. As an iOS developer you will be a part of a highly agile team tasked with developing new features. If you love to tackle new challenges, we would love to meet you!"
jobs["description"] = "<p><span style=\"color:#16a085;\"><span style=\"font-family:Arial,Helvetica,sans-serif;\">&nbsp;Develop mobile applications (iOS: Swift)</span></span></p>\r\n"
jobs["button_name"] = "Apply"
jobs["qualification"] = "B.E/B.Tech/BCA/MCA/MSc IT degree in Computer Science, Engineering, or a related subject "
jobs["employment_type"] = "full_time"
jobs["base_salary"] = 15000.0
jobs["experience"] = "0-3 years"
jobs["is_active"] = true
jobs["skills"] = ""
jobs["total_openings"] = 1.0
jobs["responsibilities"] = ""
jobs["icon_name"] = "fab fa-apple"
jobs["unique_id"] = "ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d"
jobs["seo_title"] = "iOS developer jobs in surat at Canopas, Mobile developer jobs"
jobs["seo_description"] = "Find iOS developer job in surat. If we hire you as an iOS developer, you will be responsible for designing and coding the base application, ensuring the quality, fixing bugs, maintaining the code, and implementing app updates."
jobs["apply_seo_title"] = "Apply for iOS developer job in surat or mobile developer jobs in surat at canopas software"
jobs["apply_seo_description"] = "Apply for iOS developer job at Canopas and be part of a dynamic and versatile iOS app development team."
return jobs
}
func expectedMailData() map[string]interface{} {
career := make(map[string]interface{})

career["job_title"] = "Web Developer from testing"
career["name"] = "New Web Developer"
career["email"] = "[email protected]"
career["phone"] = "1234567890"
career["place"] = "surat"
career["references"] = "From canopas"
career["message"] = "I'm a very good programer"
return career
}

func prepareRequestBody() *bytes.Buffer {

content_type, body := utils.PrepareTextFileFormData()

contentType = content_type

return body
}
Loading

0 comments on commit 81ed7eb

Please sign in to comment.