Exemplo n.º 1
0
func TestCustomer(t *testing.T) {
	var err error
	var c customer.Customer
	var cu customer.CustomerUser
	once.Do(initDtx)
	defer apicontextmock.DeMock(dtx)

	cu.Id = dtx.UserID
	err = cu.Get(dtx.APIKey)
	if err != nil {
		t.Log(err)
	}
	c.Users = append(c.Users, cu)

	Convey("Testing customer/Customer", t, func() {
		//test create customer
		c.Name = "Jason Voorhees"
		c.Email = "*****@*****.**"
		bodyBytes, _ := json.Marshal(c)
		bodyJson := bytes.NewReader(bodyBytes)
		thyme := time.Now()
		testThatHttp.Request("put", "/customer", "", "?key=", SaveCustomer, 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(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

		//test update customer
		c.Fax = "666-1313"
		c.State.Id = 1
		bodyBytes, _ = json.Marshal(c)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("put", "/customer/", ":id", strconv.Itoa(c.Id)+"?key=", SaveCustomer, 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(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

		thyme = time.Now()
		testThatHttp.RequestWithDtx("post", "/customer", "", "?key=", GetCustomer, nil, "", dtx)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

		// get customer locations
		thyme = time.Now()
		testThatHttp.RequestWithDtx("get", "/customer/locations", "", "?key=", GetLocations, nil, "", dtx)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*6)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c.Locations)
		So(err, ShouldBeNil)
		So(c.Locations, ShouldHaveSameTypeAs, []customer.CustomerLocation{})

		// //get user
		thyme = time.Now()
		testThatHttp.RequestWithDtx("post", "/customer/user", "", "?key="+dtx.APIKey, GetUser, nil, "", nil)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
		So(err, ShouldBeNil)
		So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})

		//get users
		thyme = time.Now()
		testThatHttp.RequestWithDtx("get", "/customer/users", "", "?key=", GetUsers, nil, "", dtx)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*5)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var cus []customer.CustomerUser
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cus)
		So(err, ShouldBeNil)
		So(cus, ShouldHaveSameTypeAs, []customer.CustomerUser{})

		// get customer price
		// price.CustID = c.Id
		// price.Create()
		// thyme = time.Now()
		// testThatHttp.Request("get", "/new/customer/price/", ":id", strconv.Itoa(p.ID)+"?key="+apiKey, GetCustomerPrice, nil, "")
		// So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		// So(testThatHttp.Response.Code, ShouldEqual, 200)
		// var price float64
		// err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &price)
		// So(err, ShouldBeNil)
		// So(price, ShouldHaveSameTypeAs, 7.1)

		// //get customer cart reference
		// ci.CustID = c.Id
		// ci.Create()
		// thyme = time.Now()
		// testThatHttp.Request("get", "/new/customer/cartRef/", ":id", strconv.Itoa(p.ID)+"?key="+apiKey, GetCustomerCartReference, nil, "")
		// So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		// So(testThatHttp.Response.Code, ShouldEqual, 200)
		// var reference int
		// err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &reference)
		// So(err, ShouldBeNil)
		// So(reference, ShouldHaveSameTypeAs, 7)

		//test delete customer
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/", ":id", strconv.Itoa(c.Id)+"?key=", DeleteCustomer, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

	})
	//cleanup
	err = cu.Delete()
	err = c.Delete()

}