Beispiel #1
0
func TestApplicationGuide(t *testing.T) {
	var a applicationGuide.ApplicationGuide
	var err error
	Convey("Testing ApplicationGuide", t, func() {
		//test create
		form := url.Values{"url": {"test"}, "fileType": {"pdf"}, "website_id": {"1"}}
		v := form.Encode()
		body := strings.NewReader(v)
		testThatHttp.Request("post", "/applicationGuide", "", "", CreateApplicationGuide, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &a)
		So(err, ShouldBeNil)
		So(a, ShouldHaveSameTypeAs, applicationGuide.ApplicationGuide{})

		//test create using json
		var jsonAppGuide applicationGuide.ApplicationGuide
		jsonAppGuide.Url = "www.www.com"
		jsonAppGuide.FileType = "pdf"
		jsonAppGuide.Website.ID = 1
		bodyBytes, _ := json.Marshal(jsonAppGuide)
		bodyJson := bytes.NewReader(bodyBytes)
		testThatHttp.Request("post", "/applicationGuide", "", "", CreateApplicationGuide, bodyJson, "application/json")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &jsonAppGuide)
		So(err, ShouldBeNil)
		So(jsonAppGuide, ShouldHaveSameTypeAs, applicationGuide.ApplicationGuide{})

		//test get
		testThatHttp.Request("get", "/applicationGuide/", ":id", strconv.Itoa(a.ID), GetApplicationGuide, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &a)
		So(err, ShouldBeNil)
		So(a, ShouldHaveSameTypeAs, applicationGuide.ApplicationGuide{})
		So(a.Url, ShouldEqual, "test")

		//test get by website
		testThatHttp.Request("get", "/applicationGuide/website/", ":id", strconv.Itoa(a.Website.ID), GetApplicationGuidesByWebsite, nil, "")
		var as []applicationGuide.ApplicationGuide
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &as)
		So(err, ShouldBeNil)
		So(len(as), ShouldBeGreaterThanOrEqualTo, 0)
		So(a.Url, ShouldEqual, "test")

		//test delete
		testThatHttp.Request("delete", "/applicationGuide/", ":id", strconv.Itoa(a.ID), DeleteApplicationGuide, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &a)
		So(err, ShouldBeNil)

		//cleanup
		err = jsonAppGuide.Delete()

	})
}
Beispiel #2
0
func DeleteApplicationGuide(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var ag applicationGuide.ApplicationGuide
	id, err := strconv.Atoi(params["id"])
	ag.ID = id
	err = ag.Delete()
	if err != nil {
		apierror.GenerateError("Error deleting Application Guide", err, rw, req)
	}

	//Return JSON
	return encoding.Must(enc.Encode(ag))
}