|
@@ -0,0 +1,84 @@
|
|
|
|
+package sampleapp_test
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "bytes"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/http/httptest"
|
|
|
|
+ "sampleapp"
|
|
|
|
+ "sampleapp/customersvc"
|
|
|
|
+ . "sampleapp/testutils"
|
|
|
|
+ "testing"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ sampleUser = customersvc.Customer{ // omit id
|
|
|
|
+ ID: "58ff6b3f673686f96801707b",
|
|
|
|
+ Name: "Luis",
|
|
|
|
+ Phone: customersvc.Phone{
|
|
|
|
+ Type: customersvc.TypMobile,
|
|
|
|
+ Number: "+5545991121214",
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func TestGetCustomerBadID(t *testing.T) {
|
|
|
|
+ s := sampleapp.New()
|
|
|
|
+ s.CustomerSVC = &CustomerSVCMock{}
|
|
|
|
+ ts := httptest.NewServer(s.Router())
|
|
|
|
+
|
|
|
|
+ res, err := http.Get(ts.URL + "/customer/1")
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ FailNotEq(t, res.StatusCode, 400)
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+func TestGetCustomer(t *testing.T) {
|
|
|
|
+ s := sampleapp.SampleApp{CustomerSVC: &CustomerSVCMock{}}
|
|
|
|
+
|
|
|
|
+ ts := httptest.NewServer(s.Router())
|
|
|
|
+
|
|
|
|
+ res, err := http.Get(ts.URL + "/customer/58ff6b3f673686f96801707b")
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ FailNotEq(t, res.StatusCode, 200)
|
|
|
|
+ c := customersvc.Customer{}
|
|
|
|
+ defer res.Body.Close()
|
|
|
|
+
|
|
|
|
+ json.NewDecoder(res.Body).Decode(&c)
|
|
|
|
+ FailNotEq(t, c.Name, "Luis")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestPostCustomer(t *testing.T) {
|
|
|
|
+ s := sampleapp.SampleApp{CustomerSVC: &CustomerSVCMock{}}
|
|
|
|
+ ts := httptest.NewServer(s.Router())
|
|
|
|
+
|
|
|
|
+ var bufData bytes.Buffer
|
|
|
|
+
|
|
|
|
+ newCustomer := sampleUser // Copy
|
|
|
|
+
|
|
|
|
+ json.NewEncoder(&bufData).Encode(newCustomer)
|
|
|
|
+ res, err := http.Post(ts.URL+"/customer", "application/json", &bufData)
|
|
|
|
+ FailNotEq(t, err, nil)
|
|
|
|
+
|
|
|
|
+ FailNotEq(t, res.StatusCode, 200)
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// CustomerSVCMock customer service mock
|
|
|
|
+type CustomerSVCMock struct{}
|
|
|
|
+
|
|
|
|
+func (cc *CustomerSVCMock) Store(c *customersvc.Customer) error {
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (cc *CustomerSVCMock) FetchByID(id string) (*customersvc.Customer, error) {
|
|
|
|
+ if id == sampleUser.ID {
|
|
|
|
+ dbUser := sampleUser // copy
|
|
|
|
+ return &dbUser, nil
|
|
|
|
+ }
|
|
|
|
+ return nil, customersvc.ErrNotFound
|
|
|
|
+}
|