Skip to content

Commit f00bcf5

Browse files
committedAug 12, 2021
Added tests, updated readme
1 parent 1dcde35 commit f00bcf5

File tree

4 files changed

+77
-36
lines changed

4 files changed

+77
-36
lines changed
 

‎README.md

+33-27
Original file line numberDiff line numberDiff line change
@@ -48,77 +48,83 @@ MAX_FILE_SIZE=30
4848

4949
### Running
5050

51-
- To create postgres container `make postgres`
52-
- To create db `make createdb`
53-
- To drop db `make dropdb`
51+
- To create the Postgres container - `make postgres`
52+
- To create the db - `make createdb`
53+
- To drop db - `make dropdb`
5454

55-
#### run `go mod tidy` to install packages
56-
#### cli commands
55+
#### Run `go mod tidy` to install packages
56+
#### CLI commands
5757
```
5858
go run main.go migrate_up
5959
go run main.go dropdb
6060
go run main.go migrate_steps --steps int
61+
go run main.go generate_docs
6162
go run main.go runserver --host localhost --port port (localhost, 5000 are default)
6263
```
6364

64-
#### to run migrations on the test database
65+
#### To run migrations on the test database
6566
```
6667
go run main.go migrate_up -t
6768
go run main.go dropdb -t
6869
go run main.go migrate_steps -t --steps int
6970
```
7071

71-
### Use the make file its your best friend 🛠
72-
#### Make commands
73-
##### If you are on windows please use git bash or wsl also you would have to install make for windows
74-
##### to install make for windows run `winget install GnuWin32.Make`
72+
### Use the make file, its your best friend 🛠
73+
#### Make commands -
74+
If you are on windows please use Git Bash or WSL. You also have to install Make for Windows
75+
To install Make for Windows run `winget install GnuWin32.Make`
7576

7677
```shell
77-
make postgres #creates docker container for postgres12
78-
# reads env variables from app.env
79-
make createdb #creates the db in the postgres container
80-
make dropdb #drops the db
81-
make migrate_up #migrates to the latest schema
82-
make sqlc_generate #generates sqlc code if you write queries
83-
make test # tests your code and shows coverage
84-
#its a big output make sure to read it all
78+
make postgres # Creates docker container for postgres12
79+
# Reads env variables from app.env
80+
make createdb # Creates the db in the postgres container
81+
make dropdb # Drops the db
82+
make migrate_up # Migrates to the latest schema
83+
make sqlc_generate # Generates sqlc code if you write queries
84+
make generate_docs # Generates documentation
85+
make test # Tests your code and shows coverage
86+
# Its a big output make sure to read it all
8587
```
8688

8789
## 🐳 Running with Docker
8890

89-
Start the cdn `docker-compose up`
91+
Start the cdn with `docker-compose up`
9092

9193
## 🗒️Docs
9294

9395
While adding new endpoints, you need add docs in the form of comments. For example:
9496
```go
95-
9697
/*
97-
Return Type: String
98+
Response: String
9899
99100
URL Parameters: None
100101
101-
Returns `Hello, World` when called.
102+
Request Body:
103+
- Name: username
104+
- Type: String
105+
- Description: "Username to register so and so . . ."
106+
107+
Description: "Returns `Hello, World` when called."
102108
*/
103109
func GetAllAssets(w http.ResponseWriter, r *http.Request) {
104110
w.WriteHeader(statusCode)
105111
json.NewEncoder(w).Encode("Hello, World")
106112
}
107113
```
108114

109-
And you will need to update the routes variable in [main.go](/main.go)
115+
And you will need to update the routes variable in [routes.go](/api/routes.go)
110116

111117
## 🚨 Tests
112-
To test the cdn we can use two methods
118+
There are two methods to test the cdn -
113119
```sh
114120
make test
115121
```
116-
If you don't have make installed
122+
If you don't have make installed -
117123
```sh
118124
go run main.go migrate_up -t
119125
go test ./... -v
120126
```
121-
**When you contribute, you need to add tests on the features you add.**
127+
**When you contribute, you need to add tests for the features you add.**
122128

123129
## ⛏️ Built Using
124130

@@ -127,4 +133,4 @@ go test ./... -v
127133
- [sqlc](https://github.com/kyleconroy/sqlc) - Database Query Helper
128134

129135
## ✍️ Authors
130-
See the list of [contributors](https://github.com/Tech-With-Tim/cdn/contributors) who participated in this project.
136+
See the list of [contributors](https://github.com/Tech-With-Tim/cdn/contributors) who participated in this project.

‎api/handlers/create_asset.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ Response: JSON
7575
URL Parameters: None
7676
7777
Request Body:
78-
- name: name
79-
type: String
80-
Desc: "Name under which to store the asset in the CDN."
81-
- name: url_path
82-
type: String
83-
Desc: "The URL Path under which to store the asset in the CDN.
78+
- Name: name
79+
Type: String
80+
Description: "Name under which to store the asset in the CDN."
81+
- Name: url_path
82+
Type: String
83+
Description: "The URL Path under which to store the asset in the CDN.
8484
If none is provided, a random path is selected."
8585
8686
Description: "Create Asset creates an asset with a given file,

‎api/handlers_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"mime/multipart"
1111
"net/http"
1212
"net/http/httptest"
13+
"os"
1314
"strconv"
1415
"testing"
1516
"time"
1617

1718
db "github.com/Tech-With-Tim/cdn/db/sqlc"
19+
"github.com/Tech-With-Tim/cdn/docs"
1820

1921
"github.com/Tech-With-Tim/cdn/utils"
2022
"github.com/golang-jwt/jwt"
@@ -92,6 +94,15 @@ func TestHelloWorld(t *testing.T) {
9294
checkResponseCode(t, http.StatusUnauthorized, response.Code)
9395
}
9496

97+
func TestDocs(t *testing.T) {
98+
99+
GenerateDocs(t)
100+
101+
req, _ := http.NewRequest("GET", "/docs/docs.json", nil)
102+
response := executeRequest(req)
103+
checkResponseCode(t, http.StatusUnauthorized, response.Code)
104+
}
105+
95106
func TestCreateAsset(t *testing.T) {
96107
authToken, err := createAuthToken(time.Now().Add(time.Hour * 24).Unix())
97108
require.NoError(t, err)
@@ -201,3 +212,27 @@ func TestCreateAsset(t *testing.T) {
201212
checkResponseCode(t, http.StatusBadRequest, fileRes.Code)
202213
}
203214
}
215+
216+
func GenerateDocs(t *testing.T) {
217+
err := os.Chdir("./api/handlers")
218+
219+
if err != nil {
220+
t.Fatal(err)
221+
}
222+
223+
for route, handler := range Routes {
224+
err := docs.AddDocs(route, handler)
225+
226+
if err != nil {
227+
t.Fatal(err)
228+
}
229+
}
230+
231+
docs.GenerateDocs()
232+
233+
_, err = os.Stat("../../docs/docs-template/public/docs.json")
234+
235+
if os.IsNotExist(err) {
236+
t.Fatal(err)
237+
}
238+
}

‎docs/register.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type RouteInfo struct {
2424
Route string
2525

2626
RequestBody []struct {
27-
Name string `yaml:"name"`
28-
Type string `yaml:"type"`
29-
Desc string `yaml:"Desc"`
27+
Name string `yaml:"Name"`
28+
Type string `yaml:"Type"`
29+
Desc string `yaml:"Description"`
3030
} `yaml:"Request Body"`
3131

3232
Description string `yaml:"Description"`

0 commit comments

Comments
 (0)