Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slzakaria committed Aug 4, 2024
1 parent 608b556 commit d140c73
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 23 deletions.
22 changes: 0 additions & 22 deletions server/README.Docker.md

This file was deleted.

84 changes: 84 additions & 0 deletions server/github/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
)

func MockloadAPIKey() (string, error) {
_ = godotenv.Load()
apiKey := os.Getenv("GITHUB_APIKEY")
if apiKey == "" {
return "", fmt.Errorf("API key not found in environment variables")
}

return apiKey, nil
}
func TestGetRecentIssuesByLanguage(t *testing.T) {
// Load environment variables from .env file
apiKey, err := MockloadAPIKey()
if err != nil {
// Handle the error (e.g., log it and exit)
fmt.Printf("Error loading API key: %v\n", err)
os.Exit(1)
}

// Mock server to simulate GitHub API responses
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "token "+apiKey, r.Header.Get("Authorization"))

repositories := []map[string]interface{}{
{
"name": "repo1",
"description": "description1",
"open_issues_count": 5,
"html_url": "https://github.com/user/repo1",
"pushed_at": time.Now().AddDate(0, -3, 0).Format(time.RFC3339),
"stargazers_count": 10,
"language": "Go",
},
{
"name": "repo2",
"description": "description2",
"open_issues_count": 2,
"html_url": "https://github.com/user/repo2",
"pushed_at": time.Now().AddDate(0, -1, 0).Format(time.RFC3339),
"stargazers_count": 20,
"language": "Go",
},
}

response := struct {
Items []map[string]interface{} `json:"items"`
}{Items: repositories}

json.NewEncoder(w).Encode(response)
}))
defer mockServer.Close()

// Override the baseURL with the mock server URL

t.Run("successful response", func(t *testing.T) {
repos, err := GetRecentIssuesByLanguage("Go")
assert.NoError(t, err)
assert.Len(t, repos, 2)
assert.Equal(t, "repo1", repos[0].Name)
assert.Equal(t, "repo2", repos[1].Name)
})

t.Run("API key missing", func(t *testing.T) {
os.Setenv("GITHUB_APIKEY", "")
_, err := GetRecentIssuesByLanguage("Go")
assert.Error(t, err)
assert.Equal(t, "API key not found in environment variables", err.Error())
os.Setenv("GITHUB_APIKEY", apiKey) // Reset the API key for other tests
})
}
11 changes: 10 additions & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ require (
github.com/rs/cors v1.11.0
)

require github.com/joho/godotenv v1.5.1
require (
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
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-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit d140c73

Please sign in to comment.