Commit c640f803 authored by Strakhov Egor's avatar Strakhov Egor

main_test.go: coverage: 58.1%

parent 13c1c0d1
......@@ -37,7 +37,7 @@ ID and Timestamp will be generated automatically
Run the application:
```bash
go run test.go
go run main.go
```
Push json:
......
......@@ -3,6 +3,6 @@ module github.com/mongodb/mongo-go-driver/bson
go 1.13
require (
github.com/gorilla/mux v1.8.0 // indirect
go.mongodb.org/mongo-driver v1.7.2 // indirect
github.com/gorilla/mux v1.8.0
go.mongodb.org/mongo-driver v1.7.2
)
......@@ -22,13 +22,19 @@ type Kick struct {
Longitude float64 `json:"longitude,omitempty" bson:"longitude,omitempty"`
Attitude float64 `json:"attitude,omitempty" bson:"attitude,omitempty"`
Velocity float64 `json:"velocity,omitempty" bson:"velocity,omitempty"`
}
func CreateKick(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
var kick Kick
_ = json.NewDecoder(request.Body).Decode(&kick)
err := json.NewDecoder(request.Body).Decode(&kick)
if err != nil {
response.WriteHeader(http.StatusBadRequest)
return
}
collection := client.Database("mongodb").Collection("kicks")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
......@@ -36,8 +42,16 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
kick.Timestamp = now.Format("2006-01-02 15:04:05")
result, _ := collection.InsertOne(ctx, kick)
json.NewEncoder(response).Encode(result)
result, err := collection.InsertOne(ctx, kick)
if err != nil {
response.WriteHeader(http.StatusNotAcceptable)
}
err = json.NewEncoder(response).Encode(result)
if err != nil {
response.WriteHeader(http.StatusConflict)
}
}
func GetKicks(response http.ResponseWriter, request *http.Request) {
......
package main
import (
"os"
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"go.mongodb.org/mongo-driver/mongo"
"github.com/gorilla/mux"
"testing"
)
func connect(){
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, _ = mongo.Connect(ctx, clientOptions)
err := client.Ping(context.TODO(), nil)
if err != nil {
log.Fatalf("MongoDB connection error: %v", err)
} else {
log.Printf("Connected!")
}
}
func sendJson() {
r, err := os.Open("test.json")
resp, err := http.Post("http://localhost:8080/kick", "application/json", r)
if err != nil {
log.Fatalf("Can not Post json")
}
if resp.StatusCode == 400 {
log.Fatalf("Bad json: %v\n", r)
}
if resp.StatusCode == 406 {
log.Fatalf("Cannot Insert document")
}
if resp.StatusCode == 409 {
log.Fatalf("Cannot Encode json")
}
}
func getJsons() {
resp, err := http.Get("http://localhost:8080/kicks")
if err != nil {
log.Fatalf("Cannot Get jsons")
}
if resp.StatusCode != 200 {
log.Printf(resp.Status)
}
}
func listen(finished chan bool) {
router := mux.NewRouter()
router.HandleFunc("/kick", CreateKick).Methods("POST")
router.HandleFunc("/kicks", GetKicks).Methods("GET")
finished <- true
http.ListenAndServe(":8080", router)
}
func TestMain(t *testing.T) {
finished := make(chan bool)
go listen(finished)
<-finished
connect()
sendJson()
getJsons()
}
{
"company": "Sirius",
"longitude": 12345,
"attitude": 346,
"velocity": 1
"company": "company",
"longitude": 1,
"attitude": 2,
"velocity": 3
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment