Example #1
0
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{})

	})
}
Example #2
0
func SaveLocationJson(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var cl customer.CustomerLocation
	var err error

	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 ""
	}

	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		apierror.GenerateError("Trouble reading request body while saving location", err, rw, r)
		return ""
	}

	if err = json.Unmarshal(body, &cl); err != nil {
		apierror.GenerateError("Trouble unmarshalling json request body while saving location", err, rw, r)
		return ""
	}

	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))
}
Example #3
0
func GetLocationById(w http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	str_id := params["id"]
	if str_id == "" {
		apierror.GenerateError("You must supply a location identification number.", errors.New("No id."), w, r)
	}
	id, err := strconv.Atoi(str_id)
	if err != nil {
		apierror.GenerateError("You must supply a location identification number.", err, w, r)
	}
	var l customer.CustomerLocation
	l.Id = id
	// loc, err := customer.GetLocationById(id)
	err = l.Get()
	if err != nil {
		apierror.GenerateError("Error retrieving locations.", err, w, r)
	}

	return encoding.Must(enc.Encode(l))
}
Example #4
0
func DeleteLocation(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var cl customer.CustomerLocation
	var err error

	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.Delete(dtx); err != nil {
		apierror.GenerateError("Trouble deleting location", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(cl))
}
Example #5
0
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))
}
Example #6
0
func TestDealers_New(t *testing.T) {
	var err error
	var c customer.Customer
	var cs customer.Customers
	var loc customer.CustomerLocation
	var locs customer.CustomerLocations

	dtx, err := apicontextmock.Mock()
	if err != nil {
		t.Log(err)
	}
	//setup lcoation for getById
	loc.Address = "123 Test Ave."
	loc.Create(dtx)

	Convey("Testing Dealers_New", t, func() {
		//test get etailers
		thyme := time.Now()
		testThatHttp.RequestWithDtx("get", "/dealers/etailer", "", "?key="+dtx.APIKey, GetEtailers, nil, "", dtx)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds())
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cs)
		So(err, ShouldBeNil)
		So(cs, ShouldHaveSameTypeAs, customer.Customers{})
		So(len(cs), ShouldBeGreaterThanOrEqualTo, 0)

		//test get local dealers
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/local", "", "?key="+dtx.APIKey+"&latlng=43.853282,-95.571675,45.800981,-90.468526&center=44.83536,-93.0201", GetLocalDealers, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var dls customer.DealerLocations
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &dls)
		So(err, ShouldBeNil)
		So(dls, ShouldHaveSameTypeAs, customer.DealerLocations{})

		//test get local dealers
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/local/regions", "", "?key="+dtx.APIKey, GetLocalRegions, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*5) //Long test
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var stateRegions []customer.StateRegion
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &stateRegions)
		So(err, ShouldBeNil)
		So(stateRegions, ShouldHaveSameTypeAs, []customer.StateRegion{})

		//test get dealerTypes
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/local/types", "", "?key="+dtx.APIKey, GetLocalDealerTypes, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var types []customer.DealerType
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &types)
		So(err, ShouldBeNil)
		So(types, ShouldHaveSameTypeAs, []customer.DealerType{})

		//test get dealerTiers
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/local/tiers", "", "?key="+dtx.APIKey, GetLocalDealerTiers, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var tiers []customer.DealerTier
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &tiers)
		So(err, ShouldBeNil)
		So(tiers, ShouldHaveSameTypeAs, []customer.DealerTier{})

		//test get dealerTiers
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/etailer/platinum", "", "?key="+dtx.APIKey, PlatinumEtailers, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cs)
		So(err, ShouldBeNil)
		So(cs, ShouldHaveSameTypeAs, customer.Customers{})

		//test get location by id
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/location/", ":id", strconv.Itoa(loc.Id)+"?key="+dtx.APIKey, GetLocationById, nil, "")
		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 all business classes
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/business/classes", "", "?key="+dtx.APIKey, GetAllBusinessClasses, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var bcs customer.BusinessClasses
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &bcs)
		So(err, ShouldBeNil)
		So(bcs, ShouldHaveSameTypeAs, customer.BusinessClasses{})

		//test search Locations
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/search/", ":search", "test?key="+dtx.APIKey, SearchLocations, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &locs)
		So(err, ShouldBeNil)
		So(locs, ShouldHaveSameTypeAs, customer.CustomerLocations{})

		//test search Locations by type
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/search/type/", ":search", "test?key="+dtx.APIKey, SearchLocationsByType, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &locs)
		So(err, ShouldBeNil)
		So(locs, ShouldHaveSameTypeAs, customer.CustomerLocations{})

		//test search Locations by lat long
		thyme = time.Now()
		testThatHttp.Request("get", "/dealers/search/geo/", ":latitude/:longitude", fmt.Sprint(c.Latitude)+"/"+fmt.Sprint(c.Longitude)+"?key="+dtx.APIKey, SearchLocationsByType, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		So(testThatHttp.Response, ShouldNotBeNil)
		So(testThatHttp.Response.Body, ShouldNotBeNil)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &dls)
		So(err, ShouldBeNil)
		So(dls, ShouldHaveSameTypeAs, customer.DealerLocations{})

	})
	//teardown
	loc.Delete(dtx)
	_ = apicontextmock.DeMock(dtx)
}