From 81ed7eb31b1cb23a03158c792ed6476db654845a Mon Sep 17 00:00:00 2001 From: sumita Date: Sat, 5 Mar 2022 10:28:24 +0530 Subject: [PATCH] Unit test for jobs --- .github/workflows/build.yml | 32 +++- db/sql.go | 18 ++- go.mod | 7 +- go.sum | 3 +- jobs/go.mod | 9 ++ jobs/go.sum | 11 +- jobs/jobs_test.go | 173 ++++++++++++++++++++++ jobs/templates/career-email-template.html | 40 +++++ test_assets/test_text_file.pdf | Bin 0 -> 32 bytes utils/go.mod | 6 + utils/go.sum | 11 ++ utils/test.go | 134 +++++++++++++++++ 12 files changed, 436 insertions(+), 8 deletions(-) create mode 100644 jobs/jobs_test.go create mode 100644 jobs/templates/career-email-template.html create mode 100644 test_assets/test_text_file.pdf create mode 100644 utils/test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d6fabfbf8..6f2a46a14 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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/checkout@v2.3.3 + + - 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: "sumita.k@canopas.com" + ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }} + SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }} + REGION: ${{ secrets.AWS_REGION }} + build: runs-on: ubuntu-latest permissions: @@ -29,4 +59,4 @@ jobs: - name: Build backend run: | - GOOS=linux GOARCH=amd64 go build -o main main.go \ No newline at end of file + GOOS=linux GOARCH=amd64 go build -o main main.go diff --git a/db/sql.go b/db/sql.go index e347875ea..d94dcd09f 100644 --- a/db/sql.go +++ b/db/sql.go @@ -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) diff --git a/go.mod b/go.mod index 21b5f2272..2c8527542 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -40,6 +40,7 @@ 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 @@ -47,5 +48,5 @@ require ( 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 ) diff --git a/go.sum b/go.sum index ca4a2dcba..8ecfa4795 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/jobs/go.mod b/jobs/go.mod index ecf61b97d..daacd2c72 100644 --- a/jobs/go.mod +++ b/jobs/go.mod @@ -4,21 +4,27 @@ 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 @@ -26,9 +32,12 @@ require ( 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 ) diff --git a/jobs/go.sum b/jobs/go.sum index db7d448e2..df13458b7 100644 --- a/jobs/go.sum +++ b/jobs/go.sum @@ -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= @@ -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= @@ -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= diff --git a/jobs/jobs_test.go b/jobs/jobs_test.go new file mode 100644 index 000000000..eab314e1a --- /dev/null +++ b/jobs/jobs_test.go @@ -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"] = "

 Develop mobile applications (iOS: Swift)

\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"] = "developer@gmail.com" + 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 +} diff --git a/jobs/templates/career-email-template.html b/jobs/templates/career-email-template.html new file mode 100644 index 000000000..9ee53e272 --- /dev/null +++ b/jobs/templates/career-email-template.html @@ -0,0 +1,40 @@ + + + + + + + + + + + +
+

+ Application: + {{.JobTitle}} - {{.Name}} +

+

+ Message: + {{.Message}} +

+

+ Email: + {{.Email}} +

+

+ Phone: + {{.Phone}} +

+

+ City: + {{.Place}} +

+

+ Reference: + {{.References}} +

+
+ + + \ No newline at end of file diff --git a/test_assets/test_text_file.pdf b/test_assets/test_text_file.pdf new file mode 100644 index 0000000000000000000000000000000000000000..b138d26b7435c96d3a3e2efecaf246a6b1ea15bf GIT binary patch literal 32 kcmWH^$ShU>qT ../db + require ( + db v0.0.0 github.com/aws/aws-sdk-go v1.43.5 + github.com/jmoiron/sqlx v1.3.4 github.com/sirupsen/logrus v1.8.1 + gopkg.in/guregu/null.v3 v3.5.0 ) require ( + github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect ) diff --git a/utils/go.sum b/utils/go.sum index db9152f27..b83e97893 100644 --- a/utils/go.sum +++ b/utils/go.sum @@ -3,10 +3,19 @@ github.com/aws/aws-sdk-go v1.43.5/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4o github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w= +github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -24,5 +33,7 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/guregu/null.v3 v3.5.0 h1:xTcasT8ETfMcUHn0zTvIYtQud/9Mx5dJqD554SZct0o= +gopkg.in/guregu/null.v3 v3.5.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/utils/test.go b/utils/test.go new file mode 100644 index 000000000..b2d1c73af --- /dev/null +++ b/utils/test.go @@ -0,0 +1,134 @@ +package utils + +import ( + "bytes" + "db" + "encoding/json" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/jmoiron/sqlx" + log "github.com/sirupsen/logrus" +) + +type TestRequest struct { + Url string + Method string + Headers map[string]interface{} + Body interface{} + ResponseCode int + ResponseTypeArray bool + ExpectedData interface{} +} + +const PDF_FILE_PATH = "../test_assets/test_text_file.pdf" + +func GotData(w *httptest.ResponseRecorder, t *testing.T) interface{} { + var got interface{} + if len(w.Body.Bytes()) != 0 { + err := json.Unmarshal(w.Body.Bytes(), &got) + if err != nil { + t.Fatal(err) + } + } + return got +} + +func GotArrayData(w *httptest.ResponseRecorder, t *testing.T) []interface{} { + var got []interface{} + if len(w.Body.Bytes()) != 0 { + err := json.Unmarshal(w.Body.Bytes(), &got) + if err != nil { + t.Fatal(err) + } + } + return got +} + +func SetRequestHeaders(req *http.Request, headers map[string]interface{}, contentType string) { + if len(headers) > 0 { + for key, value := range headers { + if key == "Content-Type" { + req.Header.Add(key, contentType) + } else { + req.Header.Add(key, value.(string)) + } + } + } +} + +func TestDB() (*sqlx.DB, error) { + + err := os.Setenv("DB_NAME", "website_admin_test") + if err != nil { + return nil, err + } + + DB := db.NewSql() + return DB, nil +} + +func CreateTables(db *sqlx.DB) error { + + jobs := "CREATE TABLE IF NOT EXISTS `jobs` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`title` varchar(255) NULL DEFAULT NULL,`summary` varchar(500) NULL DEFAULT NULL,`description` text NULL DEFAULT NULL,`button_name` varchar(225) NULL DEFAULT NULL,`qualification` varchar(225) NULL DEFAULT NULL,`employment_type` varchar(225) NULL DEFAULT NULL,`base_salary` int(11) NULL DEFAULT NULL,`experience` varchar(225) NULL DEFAULT NULL,`is_active` tinyint(1) NULL DEFAULT 0,`skills` text NULL DEFAULT NULL,`total_openings` int(11) NULL DEFAULT 1,`responsibilities` text NULL DEFAULT NULL, `icon_name` varchar(255) NULL DEFAULT NULL , `unique_id` varchar(255) NULL DEFAULT NULL, `seo_title` varchar(255) NULL DEFAULT NULL, `seo_description` text NULL DEFAULT NULL, `apply_seo_title` varchar(255) NULL DEFAULT NULL, `apply_seo_description` text NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL,`updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`))" + + _, err := db.Exec(jobs) + + if err != nil { + return err + } + + return nil +} + +func TruncateTables(db *sqlx.DB) { + db.MustExec("TRUNCATE TABLE jobs") +} + +func PrepareTablesData(db *sqlx.DB) { + db.MustExec("INSERT INTO jobs(`id`, `title`, `summary`, `description`, `button_name`,`qualification`, `employment_type`, `base_salary`, `experience`, `is_active`, `skills`, `total_openings`, `responsibilities`, `icon_name`, `unique_id`, `seo_title`, `seo_description`, `apply_seo_title`, `apply_seo_description`, `created_at`, `updated_at`) VALUES(1, 'IOS Developer', '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!', '

 Develop mobile applications (iOS: Swift)

\r\n', 'Apply', 'B.E/B.Tech/BCA/MCA/MSc IT degree in Computer Science, Engineering, or a related subject ', 'full_time', 15000 , '0-3 years', true, '', 1, '','fab fa-apple', 'ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d', 'iOS developer jobs in surat at Canopas, Mobile developer jobs', '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.', 'Apply for iOS developer job in surat or mobile developer jobs in surat at canopas software', 'Apply for iOS developer job at Canopas and be part of a dynamic and versatile iOS app development team.', UTC_TIMESTAMP(), UTC_TIMESTAMP())") +} + +func PrepareTextFileFormData() (string, *bytes.Buffer) { + + var ( + file *os.File + err error + ) + + file, err = os.Open(PDF_FILE_PATH) + + if err != nil { + log.Error(err) + } + defer file.Close() + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) // create fileWriter to write bytes in body + + filePart, err := writer.CreateFormFile("file", filepath.Base(file.Name())) // create filePart + + if err != nil { + log.Error(err) + } + + io.Copy(filePart, file) // copy fileBytes to file + + contentType := writer.FormDataContentType() // get contentType of file + + writer.WriteField("job_title", "Web Developer from testing") + writer.WriteField("name", "New Web Developer") + writer.WriteField("email", "developer@gmail.com") + writer.WriteField("phone", "1234567890") + writer.WriteField("place", "surat") + writer.WriteField("references", "From canopas") + writer.WriteField("message", "I'm a very good programer") + writer.Close() + + return contentType, body +}