Skip to content

Commit

Permalink
Implement unit test for sitemap api
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-shruti-s committed Mar 10, 2022
1 parent 609d4dd commit 12b9df0
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
run: |
go test jobs
go test contact
go test sitemap
env:
DB_HOST: localhost
DB_NAME: website_admin_test
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/gin-gonic/gin v1.7.7
github.com/jmoiron/sqlx v1.3.4
jobs v0.0.0
sitemap v0.0.0-00010101000000-000000000000
sitemap v0.0.0
)

require (
Expand Down
13 changes: 11 additions & 2 deletions sitemap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@ replace jobs => ../jobs

replace utils => ../utils

replace db => ../db

require (
db v0.0.0 // indirect
github.com/gin-gonic/gin v1.7.7
github.com/jmoiron/sqlx v1.3.4
github.com/tj/assert v0.0.3
jobs v0.0.0-00010101000000-000000000000
utils v0.0.0-00010101000000-000000000000
)

require (
github.com/aws/aws-sdk-go v1.43.5 // 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/jmoiron/sqlx v1.3.4 // 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/sirupsen/logrus v1.8.1 // 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/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
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
)
11 changes: 9 additions & 2 deletions sitemap/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=
2 changes: 1 addition & 1 deletion sitemap/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type URL struct {
type URLset struct {
XMLName xml.Name `xml:"urlset"`
XMLNS string `xml:"xmlns,attr"`
URL []URL
URL []URL `xml:"url"`
}

func (repository *SitemapRepository) GenerateSitemap(c *gin.Context) {
Expand Down
129 changes: 129 additions & 0 deletions sitemap/sitemap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package sitemap

import (
"bytes"
"embed"
"encoding/json"
"encoding/xml"
"jobs"
"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 repo *SitemapRepository
var careerRepo *jobs.CareerRepository
var err error
var testDB *sqlx.DB
var testRequests []utils.TestRequest

const (
GET_SITEMAP_API_URL = "/sitemap?baseUrl=http://localhost:8080"
)

func Test_init(t *testing.T) {
repo, err = initializeRepo()
assert.Nil(t, err)
testRequests = []utils.TestRequest{
{
Url: GET_SITEMAP_API_URL,
Method: "GET",
Headers: nil,
Body: nil,
ResponseCode: http.StatusOK,
ResponseTypeArray: false,
ExpectedData: expectedSitemapData(),
},
}
}

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

if testData.Body != nil {
requestByte, _ := json.Marshal(testData.Body)
reqBodyData := bytes.NewReader(requestByte)
req, err = http.NewRequest(testData.Method, testData.Url, reqBodyData)
} else {
req, err = http.NewRequest(testData.Method, testData.Url, nil)
}

asserts.NoError(err)

engine.ServeHTTP(w, req)
assert.Equal(t, testData.ResponseCode, w.Code)

var urlset URLset

err = xml.Unmarshal(w.Body.Bytes(), &urlset)

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

}
}

func initializeRepo() (*SitemapRepository, error) {

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

careerRepo = jobs.New(testDB, templateFS)
repo = New(careerRepo)

return repo, err
}

func setUpRouter(engine *gin.Engine) {
engine.GET("/sitemap", repo.GenerateSitemap)
}

func expectedSitemapData() URLset {
sitemap := URLset{}

sitemap.XMLName.Space = "http://www.sitemaps.org/schemas/sitemap/0.9"
sitemap.XMLName.Local = "urlset"
sitemap.XMLNS = "http://www.sitemaps.org/schemas/sitemap/0.9"
sitemap.URL = append(sitemap.URL, expectedURLData()...)

return sitemap
}

func expectedURLData() []URL {

sitemapUrls := []URL{
{Loc: "http://localhost:8080", Priority: `1`},
{Loc: "http://localhost:8080/jobs", Priority: `1`},
{Loc: "http://localhost:8080/contact", Priority: `0.9`},
{Loc: "https://blog.canopas.com", Priority: `0.8`},
{Loc: "http://localhost:8080/jobs/ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d", Priority: `0.9`},
{Loc: "http://localhost:8080/jobs/apply/ios-developer-a9b45f34-a1a5-419f-b536-b7c290925d6d", Priority: `0.9`},
}

for i := range sitemapUrls {
sitemapUrls[i].XMLName.Space = "http://www.sitemaps.org/schemas/sitemap/0.9"
sitemapUrls[i].XMLName.Local = "url"
sitemapUrls[i].ChangeFreq = "monthly"
sitemapUrls[i].LastMod = "2022-03-01T00:00:00.000Z"
}

return sitemapUrls
}
40 changes: 40 additions & 0 deletions sitemap/templates/career-email-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="HandheldFriendly" content="true" />
</head>

<body>
<div class="review-application-div">
<p>
<span><strong>Application: </strong> </span>
<span class="title-span">{{.JobTitle}} - {{.Name}}</span>
</p>
<p>
<span><strong>Message: </strong> </span>
<span>{{.Message}}</span>
</p>
<p>
<span><strong>Email: </strong> </span>
<span>{{.Email}}</span>
</p>
<p>
<span><strong>Phone:</strong> </span>
<span>{{.Phone}}</span>
</p>
<p>
<span><strong>City: </strong></span>
<span>{{.Place}}</span>
</p>
<p>
<span><strong>Reference: </strong></span>
<span>{{.References}}</span>
</p>
</div>
</body>

</html>

0 comments on commit 12b9df0

Please sign in to comment.