Commit 2bc68685 authored by Strakhov Egor's avatar Strakhov Egor

coverage: 59.5% of statements

parent af052a92
......@@ -32,6 +32,7 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
if err != nil {
response.WriteHeader(http.StatusBadRequest)
fmt.Println("Bad Json")
return
}
......@@ -63,7 +64,6 @@ func GetKicks(response http.ResponseWriter, request *http.Request) {
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
return
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
......@@ -74,7 +74,6 @@ func GetKicks(response http.ResponseWriter, request *http.Request) {
if err := cursor.Err(); err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
return
}
json.NewEncoder(response).Encode(kicks)
}
......
package main
import (
"io/ioutil"
"go.mongodb.org/mongo-driver/bson"
"encoding/json"
"os"
"context"
"log"
......@@ -12,6 +15,24 @@ import (
"testing"
)
func init() {
finished := make(chan bool)
go listen(finished)
<-finished
connect()
}
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 connect(){
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
......@@ -20,31 +41,60 @@ func connect(){
err := client.Ping(context.TODO(), nil)
if err != nil {
log.Fatalf("MongoDB connection error: %v", err)
} else {
log.Printf("Connected!")
}
}
func sendJson() {
func sendJson(url string) {
//reading initial struct
var initial Kick
file, _ := ioutil.ReadFile("test.json")
_ = json.Unmarshal([]byte(file), &initial)
log.Printf("%v\n", initial)
//posting json
r, err := os.Open("test.json")
resp, err := http.Post("http://localhost:8080/kick", "application/json", r)
resp, err := http.Post(url, "application/json", r)
r.Close()
//checking errors
if err != nil {
log.Fatalf("Can not Post json")
log.Printf("Can not Post json")
}
if resp.StatusCode == 400 {
log.Fatalf("Bad json: %v\n", r)
log.Printf("Bad json: %v\n", r)
}
if resp.StatusCode == 406 {
log.Fatalf("Cannot Insert document")
log.Printf("Cannot Insert document")
}
if resp.StatusCode == 409 {
log.Fatalf("Cannot Encode json")
log.Printf("Cannot Encode json")
}
//reading last inserted document from BD
var kick Kick
collection := client.Database("mongodb").Collection("kicks")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cursor, err := collection.Find(ctx, bson.M{})
defer cursor.Close(ctx)
for cursor.Next(ctx) {
cursor.Decode(&kick)
}
//checking if the initial struct equals the readed one
if (kick.Company != initial.Company) ||
(kick.Longitude != initial.Longitude) ||
(kick.Attitude != initial.Attitude) ||
(kick.Velocity != initial.Velocity) {
log.Println("Document Inserted Uncorrectly")
}
//delete testing document from BD
collection.DeleteOne(ctx, bson.M{"_id": kick.ID})
}
func getJsons() {
......@@ -59,23 +109,16 @@ func getJsons() {
}
}
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) {
sendJson("http://localhost:8080/kick")
getJsons()
}
func TestMain(t *testing.T) {
finished := make(chan bool)
go listen(finished)
<-finished
/*
func TestCreation(t *testing.T) {
sendJson("http://localhost:8080/kick")
}
connect()
*/
sendJson()
getJsons()
}
{
"company": "company",
"company": "Malchik",
"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