func TestCustomerLocation(t *testing.T) { var err error var loc customer.CustomerLocation Convey("Testing customer/Location", t, func() { //test create customer location form := url.Values{"name": {"Dave Grohl"}, "address": {"404 S. Barstow St."}, "city": {"Eau Claire"}} v := form.Encode() body := strings.NewReader(v) thyme := time.Now() testThatHttp.Request("post", "/customer/location", "", "", SaveLocation, body, "application/x-www-form-urlencoded") So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*2) So(testThatHttp.Response.Code, ShouldEqual, 200) err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &loc) So(err, ShouldBeNil) So(loc, ShouldHaveSameTypeAs, customer.CustomerLocation{}) So(loc.Id, ShouldBeGreaterThan, 0) //test update location with json loc.Fax = "715-839-0000" bodyBytes, _ := json.Marshal(loc) bodyJson := bytes.NewReader(bodyBytes) thyme = time.Now() testThatHttp.Request("put", "/customer/location/", ":id", strconv.Itoa(loc.Id), SaveLocation, bodyJson, "application/json") So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2) So(testThatHttp.Response.Code, ShouldEqual, 200) err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &loc) So(err, ShouldBeNil) So(loc, ShouldHaveSameTypeAs, customer.CustomerLocation{}) //test get location thyme = time.Now() testThatHttp.Request("get", "/customer/location/", ":id", strconv.Itoa(loc.Id), GetLocation, bodyJson, "application/json") So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2) So(testThatHttp.Response.Code, ShouldEqual, 200) err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &loc) So(err, ShouldBeNil) So(loc, ShouldHaveSameTypeAs, customer.CustomerLocation{}) //test delete location thyme = time.Now() testThatHttp.Request("delete", "/customer/location/", ":id", strconv.Itoa(loc.Id), DeleteLocation, bodyJson, "application/json") So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2) So(testThatHttp.Response.Code, ShouldEqual, 200) err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &loc) So(err, ShouldBeNil) So(loc, ShouldHaveSameTypeAs, customer.CustomerLocation{}) }) }
func SaveLocation(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string { var cl customer.CustomerLocation var err error if r.FormValue("id") != "" || params["id"] != "" { id := r.FormValue("id") if id == "" { id = params["id"] } if cl.Id, err = strconv.Atoi(id); err != nil { apierror.GenerateError("Trouble getting location ID", err, rw, r) return "" } if err = cl.Get(); err != nil { apierror.GenerateError("Trouble getting location", err, rw, r) return "" } } name := r.FormValue("name") address := r.FormValue("address") city := r.FormValue("city") state := r.FormValue("stateId") email := r.FormValue("email") phone := r.FormValue("phone") fax := r.FormValue("fax") latitude := r.FormValue("latitude") longitude := r.FormValue("longitude") customerID := r.FormValue("customerId") contactPerson := r.FormValue("contactPerson") isPrimary := r.FormValue("isPrimary") postalCode := r.FormValue("postalCode") shippingDefault := r.FormValue("shippingDefault") if name != "" { cl.Name = name } if address != "" { cl.Address = address } if city != "" { cl.City = city } if state != "" { if cl.State.Id, err = strconv.Atoi(state); err != nil { apierror.GenerateError("Trouble setting state ID", err, rw, r) return "" } } if email != "" { cl.Email = email } if phone != "" { cl.Phone = phone } if fax != "" { cl.Fax = fax } if latitude != "" { if cl.Coordinates.Latitude, err = strconv.ParseFloat(latitude, 64); err != nil { cl.Coordinates.Latitude = 0 } } if longitude != "" { if cl.Coordinates.Longitude, err = strconv.ParseFloat(longitude, 64); err != nil { cl.Coordinates.Longitude = 0 } } if customerID != "" { if cl.CustomerId, err = strconv.Atoi(customerID); err != nil { apierror.GenerateError("Trouble getting customer ID", err, rw, r) return "" } } if contactPerson != "" { cl.ContactPerson = contactPerson } if isPrimary != "" { if cl.IsPrimary, err = strconv.ParseBool(isPrimary); err != nil { cl.IsPrimary = false } } if postalCode != "" { cl.PostalCode = postalCode } if shippingDefault != "" { if cl.ShippingDefault, err = strconv.ParseBool(shippingDefault); err != nil { cl.ShippingDefault = false } } if cl.Id > 0 { err = cl.Update(dtx) } else { err = cl.Create(dtx) } if err != nil { msg := "Trouble creating location" if cl.Id > 0 { msg = "Trouble updating location" } apierror.GenerateError(msg, err, rw, r) return "" } return encoding.Must(enc.Encode(cl)) }