func TestIDofIDSlice(t *testing.T) { is := is.New(t) var b, s []byte for i := 0; i < 64; i++ { b = append(b, 0xFF) s = append(s, 0x00) } bigBig := big.NewInt(0) bigSmall := big.NewInt(0) bigBig = bigBig.SetBytes(b) bigSmall = bigSmall.SetBytes(s) bigID := asset.ID(*bigBig) smallID := asset.ID(*bigSmall) encoder := asset.NewIDEncoder() is.OK(encoder) _, err := io.Copy(encoder, strings.NewReader(`c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111111c467RPWkcUr5dga8jgywjSup7CMoA9FNqkNjEFgAkEpF9vNktFnx77e2Js11EDL3BNu9MaKFUbacZRt1HYym4b8RNp`)) is.NoErr(err) id := encoder.ID() var idSlice asset.IDSlice idSlice.Push(&bigID) idSlice.Push(&smallID) sliceID, err := idSlice.ID() is.NoErr(err) is.Equal(sliceID.String(), id.String()) }
func TestNewWriter_1Sample(t *testing.T) { t.Parallel() is := is.New(t) f, err := ioutil.TempFile("", "wavPkgtest") is.NoErr(err) wr, err := wf.NewWriter(f) is.NoErr(err) err = wr.WriteSample([]byte{1, 1}) is.NoErr(err) is.Nil(wr.Close()) f, err = os.Open(f.Name()) is.NoErr(err) b, err := ioutil.ReadAll(f) is.NoErr(err) is.Equal(len(b), 46) is.True(bytes.Contains(b, riff)) is.True(bytes.Contains(b, wave)) is.True(bytes.Contains(b, fmt20)) is.Nil(os.Remove(f.Name())) }
func TestReadSample(t *testing.T) { t.Parallel() is := is.New(t) wavFile := bytes.NewReader(wavWithOneSample) wavReader, err := NewReader(wavFile, int64(len(wavWithOneSample))) is.NoErr(err) is.Equal(uint32(1), wavReader.GetSampleCount()) sample, err := wavReader.ReadSample() is.NoErr(err) is.Equal(257, sample) }
func TestLineComments(t *testing.T) { is := is.New(t) l, err := parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`)) is.NoErr(err) detail := l.Detail() is.OK(detail) is.Equal(detail.Key, "Key") is.Equal(detail.Value.Data, "Value") l, err = parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`)) is.NoErr(err) is.Equal(string(l.Bytes), `* Key: "Value"`) }
func TestParseCostRange(t *testing.T) { is := is.New(t) var l meander.CostRange var err error l, err = meander.ParseCostRange("$$...$$$") is.NoErr(err) is.Equal(l.From, meander.Cost2) is.Equal(l.To, meander.Cost3) l, err = meander.ParseCostRange("$...$$$$$") is.NoErr(err) is.Equal(l.From, meander.Cost1) is.Equal(l.To, meander.Cost5) }
func TestIDSliceString(t *testing.T) { is := is.New(t) var ids asset.IDSlice id1, err := asset.Identify(strings.NewReader("foo")) is.NoErr(err) id2, err := asset.Identify(strings.NewReader("bar")) is.NoErr(err) ids.Push(id1) ids.Push(id2) is.Equal(ids.String(), id1.String()+id2.String()) }
func TestIndexProject(t *testing.T) { is := is.New(t) projectRepositoryMock := &ProjectRepositoryMock{} projectRepositoryMock.GetAllProjectsCall.Returns.Projects = []Project{ Project{ Model: Model{ ID: 1, }, }, Project{ Model: Model{ ID: 2, }, }, } userContext := &ApplicationContext{ ProjectRepository: projectRepositoryMock, } r := getRequest(userContext, ``) w := httptest.NewRecorder() IndexProject(w, r) is.Equal(w.Code, http.StatusOK) var body []Project is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(body[0].Model.ID, 1) is.Equal(body[1].Model.ID, 2) }
func TestAfter(t *testing.T) { is := is.New(t) options := &respond.Options{} var aftercall map[string]interface{} options.After = func(w http.ResponseWriter, r *http.Request, status int, data interface{}) { aftercall = map[string]interface{}{ "w": w, "r": r, "status": status, "data": data, } } testHandler := &testHandler{ status: http.StatusOK, data: testdata, } handler := options.Handler(testHandler) w := httptest.NewRecorder() r := newTestRequest() handler.ServeHTTP(w, r) is.OK(aftercall) is.Equal(aftercall["w"], w) is.Equal(aftercall["r"], r) is.Equal(aftercall["status"], testHandler.status) is.Equal(aftercall["data"], testHandler.data) is.Equal(http.StatusOK, w.Code) var data map[string]interface{} is.NoErr(json.Unmarshal(w.Body.Bytes(), &data)) is.Equal(data, testdata) is.Equal(w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") }
func TestProjectGetByID(t *testing.T) { projectRepositoryMock := ProjectRepositoryMock{} projectRepositoryMock.GetByIDCall.Returns.Project = Project{ Model: Model{ ID: 123456789, }, Name: "Name1", Description: "Description1", ClientID: 1, IsActive: true, } var context = &ApplicationContext{ ProjectRepository: &projectRepositoryMock, } is := is.New(t) w := httptest.NewRecorder() r := getRequest(context, "") GetProjectByID(0).ServeHTTP(w, r) is.Equal(w.Code, http.StatusOK) var body Project is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(body.Model.ID, uint(123456789)) is.Equal(body.Name, "Name1") is.Equal(body.Description, "Description1") is.Equal(body.IsActive, true) }
func TestLogin(t *testing.T) { is := is.New(t) userRepositoryMock := &UserRepositoryMock{} userRepositoryMock.CheckCredentialsCall.Returns.UserType = "freelancer" userRepositoryMock.CheckCredentialsCall.Returns.User = User{ Model: Model{ ID: 1, }, FirstName: "firstname", LastName: "lastname", Password: "******", Email: "*****@*****.**", } userContext := &ApplicationContext{ UserRepository: userRepositoryMock, } r := getRequest(userContext, ` { "email": "*****@*****.**", "password": "******" } `) w := httptest.NewRecorder() Login(w, r) is.Equal(w.Code, http.StatusOK) var body map[string]interface{} is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(body["id"], 1) is.OK(body["token"]) is.Equal(body["type"], "freelancer") }
func TestIndexFreelancer(t *testing.T) { is := is.New(t) freelancerRepositoryMock := &FreelancerRepositoryMock{} freelancerRepositoryMock.GetAllFreelancersCall.Returns.Freelancers = []Freelancer{ Freelancer{ User: User{ Model: Model{ ID: 1, }, }, }, Freelancer{ User: User{ Model: Model{ ID: 2, }, }, }, } freelancerContext := &ApplicationContext{ FreelancerRepository: freelancerRepositoryMock, } r := getRequest(freelancerContext, ``) w := httptest.NewRecorder() IndexFreelancer(w, r) is.Equal(w.Code, http.StatusOK) var body []Freelancer is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(body[0].Model.ID, 1) is.Equal(body[1].Model.ID, 2) }
func TestGetFreelancerByID(t *testing.T) { is := is.New(t) freelancerRepositoryMock := &FreelancerRepositoryMock{} freelancerRepositoryMock.GetFreelancerCall.Returns.Freelancer = Freelancer{ User: User{ Model: Model{ ID: 1, }, }, } freelancerContext := &ApplicationContext{ FreelancerRepository: freelancerRepositoryMock, } r := getRequest(freelancerContext, ``) w := httptest.NewRecorder() GetFreelancerByID(1).ServeHTTP(w, r) is.Equal(w.Code, http.StatusOK) is.Equal(freelancerRepositoryMock.GetFreelancerCall.Receives.ID, 1) var body Freelancer is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(body.Model.ID, 1) }
func TestParser(t *testing.T) { is := is.New(t) groups, err := parse.ParseFile("../testfiles/success/comments.silk.md", "../testfiles/success/comments2.silk.md") is.NoErr(err) is.Equal(len(groups), 3) is.Equal(groups[0].Filename, "../testfiles/success/comments.silk.md") is.Equal(groups[1].Filename, "../testfiles/success/comments.silk.md") is.Equal(groups[2].Filename, "../testfiles/success/comments2.silk.md") is.Equal(groups[0].Title, "Comments and things") is.Equal(groups[1].Title, "Another group") group := groups[0] is.Equal(len(group.Requests), 2) is.Equal(len(group.Details), 1) is.Equal(group.Details[0].Detail().Key, "Root") is.Equal(group.Details[0].Detail().Value.Data, "http://localhost:8080/") req1 := group.Requests[0] is.Equal("POST", string(req1.Method)) is.Equal("/comments", string(req1.Path)) is.Equal(len(req1.Details), 1) is.Equal(req1.Details[0].Detail().Key, "Content-Type") is.Equal(req1.Details[0].Detail().Value.Data, "application/json") is.Equal(req1.ExpectedDetails[0].Detail().Key, "Status") is.Equal(req1.ExpectedDetails[0].Detail().Value.Data, 201) is.Equal(req1.Body.String(), `{ "name": "Mat", "comment": "Good work" }`) is.Equal(req1.BodyType, "json") is.Equal(req1.ExpectedBody.String(), `{ "id": "123", "name": "Mat", "comment": "Good work" }`) is.Equal(req1.ExpectedBodyType, "json") req2 := group.Requests[1] is.Equal("GET", req2.Method) is.Equal("/comments/{id}", req2.Path) is.Equal(len(req2.Params), 1) is.Equal(req2.Params[0].Detail().Key, "pretty") is.Equal(req2.Params[0].Detail().Value.Data, true) is.Equal(req2.ExpectedDetails[0].Detail().Key, "Status") is.Equal(req2.ExpectedDetails[0].Detail().Value.Data, 200) is.Equal(req2.ExpectedDetails[1].Detail().Key, "Content-Type") is.Equal(req2.ExpectedDetails[1].Detail().Value.Data, "application/json") is.Equal(req2.ExpectedBody.String(), `{ "id": "123", "name": "Mat", "comment": "Good work" }`) is.Equal(req2.ExpectedBody.Number(), 46) group = groups[1] is.Equal(len(group.Requests), 1) }
func TestJobWithJobApplicationError(t *testing.T) { var jobContext = &ApplicationContext{} is := is.New(t) w := httptest.NewRecorder() requestBody := `{}` r := getRequest(jobContext, requestBody) next := func(jobApplication *JobApplication) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) } WithJobApplication{next}.ServeHTTP(w, r) is.Equal(w.Code, http.StatusBadRequest) var body map[string]string is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(len(body), 7) is.Equal(body["Message"], "non zero value required") is.Equal(body["Milestones"], "non zero value required") is.Equal(body["Samples"], "non zero value required") is.Equal(body["DeliveryEstimate"], "non zero value required") is.Equal(body["FreelancerID"], "non zero value required") is.Equal(body["Hours"], "non zero value required") is.Equal(body["HourPrice"], "non zero value required") }
func TestJobGetJobByID(t *testing.T) { jobRepositoryMock := JobRepositoryMock{} jobRepositoryMock.GetJobCall.Returns.Job = Job{ Model: Model{ ID: 123456789, }, Name: "Name1", Summary: "Summary1", Details: "Details1", ClientID: 1, IsActive: true, Price: 100, } var jobContext = &ApplicationContext{ JobRepository: &jobRepositoryMock, } is := is.New(t) w := httptest.NewRecorder() r := getRequest(jobContext, "") GetJobByID(0).ServeHTTP(w, r) is.Equal(w.Code, http.StatusOK) var body Job is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) is.Equal(body.Model.ID, uint(123456789)) is.Equal(body.Name, "Name1") is.Equal(body.Summary, "Summary1") is.Equal(body.Details, "Details1") is.Equal(body.IsActive, true) is.Equal(body.Price, 100) }
func TestIdentify(t *testing.T) { is := is.New(t) id, err := asset.Identify(iotest.DataErrReader(strings.NewReader("foo"))) is.NoErr(err) is.Equal(id.String(), "c45XyDwWmrPQwJPdULBhma6LGNaLghKtN7R9vLn2tFrepZJ9jJFSDzpCKei11EgA5r1veenBu3Q8qfvWeDuPc7fJK2") }
func TestPut(t *testing.T) { is := is.New(t) tmp := test.TempDir(is) defer test.DeleteDir(&tmp) db_path := tmp + "/c4.db" test_db, err := db.Open(db_path) is.NoErr(err) err = test_db.CreateBucket("bucket") is.NoErr(err) err = test_db.Put("bucket", []byte("key"), []byte("value: 42")) is.NoErr(err) }
func TestAddClient(t *testing.T) { is := is.New(t) clientRepositoryMock := &ClientRepositoryMock{} userContext := &ApplicationContext{ ClientRepository: clientRepositoryMock, } r := getRequest(userContext, ``) w := httptest.NewRecorder() user := &User{ Model: Model{ ID: 1, }, } AddClient(user).ServeHTTP(w, r) is.Equal(w.Code, http.StatusOK) is.Equal(clientRepositoryMock.AddClientCall.Receives.Client.User.ID, 1) var body map[string]interface{} is.NoErr(json.Unmarshal(w.Body.Bytes(), &body)) userMap := body["user"].(map[string]interface{}) is.Equal(userMap["id"], 1) is.Equal(body["type"], "client") }
func TestAppendOrder(t *testing.T) { is := is.New(t) byteData := [4][]byte{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0d, 0x24}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0xfa, 0x28}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xac, 0xad, 0x10}, } expectedIDs := [4]string{ `c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111121`, `c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111211`, `c41111111111111111111111111111111111111111111111111111111111111111111111111111111111112111`, `c41111111111111111111111111111111111111111111111111111111111111111111111111111111111121111`, } for k := 0; k < 4; k++ { b := byteData[k] bignum := big.NewInt(0) bignum = bignum.SetBytes(b) id := asset.ID(*bignum) is.Equal(id.String(), expectedIDs[k]) id2, err := asset.ParseID(expectedIDs[k]) is.NoErr(err) bignum2 := big.Int(*id2) b = (&bignum2).Bytes() size := len(b) for size < 64 { b = append([]byte{0}, b...) size++ } for i, bb := range b { is.Equal(bb, byteData[k][i]) } } }
func TestPlaceJSON(t *testing.T) { is := is.New(t) var e meander.Place j := `{ "geometry" : { "location" : { "lat" : -33.870775, "lng" : 151.199025 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png", "id" : "21a0b251c9b8392186142c798263e289fe45b4aa", "name" : "Rhythmboat Cruises" }` is.NoErr(json.NewDecoder(strings.NewReader(j)).Decode(&e)) is.Equal("Rhythmboat Cruises", e.Name) is.Equal("http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png", e.Icon) is.Equal(-33.870775, e.Lat) is.Equal(151.199025, e.Lng) }
func TestWithError(t *testing.T) { is := is.New(t) w := httptest.NewRecorder() r := newTestRequest() err := errors.New("something went wrong") opts := &respond.Options{ Before: func(w http.ResponseWriter, r *http.Request, status int, data interface{}) (int, interface{}) { if err, ok := data.(error); ok { return status, map[string]interface{}{"error": err.Error()} } return status, data }, } testHandler := &testHandler{ status: http.StatusInternalServerError, data: err, } handler := opts.Handler(testHandler) handler.ServeHTTP(w, r) is.Equal(http.StatusInternalServerError, w.Code) var data map[string]interface{} is.NoErr(json.Unmarshal(w.Body.Bytes(), &data)) is.Equal(data, map[string]interface{}{"error": err.Error()}) is.Equal(w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") }
func TestFindString(t *testing.T) { is := is.New(t) s := "This is a #long string written by @mat containing links to https://downlist.io/." notes, err := anno.FindString(anno.URLs, s) is.NoErr(err) is.Equal(len(notes), 1) is.Equal(notes[0].String(), "https://downlist.io/") }
func TestLineDetail(t *testing.T) { is := is.New(t) l, err := parse.ParseLine(0, []byte(`* Key-Here: "Value"`)) is.NoErr(err) detail := l.Detail() is.Equal(detail.Key, "Key-Here") is.Equal(detail.Value.Data, "Value") }
func TestIDEncoderReset(t *testing.T) { is := is.New(t) e := asset.NewIDEncoder() is.OK(e) for i := 0; i < 10; i++ { s := strconv.Itoa(i) e2 := asset.NewIDEncoder() _, err := io.Copy(e, strings.NewReader(s)) is.NoErr(err) _, err2 := io.Copy(e2, strings.NewReader(s)) is.NoErr(err2) id1 := e.ID() id2 := e2.ID() is.Equal(id1, id2) e.Reset() } }
func TestFieldFinder(t *testing.T) { is := is.New(t) s := []byte("A field finder will be able to find fields.") var fn anno.Finder fn = anno.FieldFunc("thiskind", func(f []byte) (bool, []byte) { if string(f) == "field" { return true, f } if string(f) == "able" { return true, f } if string(f) == "find" { return true, f } return false, f }) notes, err := fn.Find(s) is.NoErr(err) is.Equal(len(notes), 3) is.Equal(notes[0].Val, []byte("field")) is.Equal(notes[0].Start, 2) is.Equal(notes[0].End(), 2+len(notes[0].Val)) is.Equal(notes[0].Kind, "thiskind") is.Equal(notes[1].Val, []byte("able")) is.Equal(notes[1].Start, 23) is.Equal(notes[1].End(), 23+len(notes[1].Val)) is.Equal(notes[1].Kind, "thiskind") is.Equal(notes[2].Val, []byte("find")) is.Equal(notes[2].Start, 8) is.Equal(notes[2].End(), 8+len(notes[2].Val)) is.Equal(notes[2].Kind, "thiskind") // sort the notes sort.Sort(notes) is.Equal(len(notes), 3) is.Equal(notes[0].Val, []byte("field")) is.Equal(notes[0].Start, 2) is.Equal(notes[0].End(), 2+len(notes[0].Val)) is.Equal(notes[0].Kind, "thiskind") is.Equal(notes[1].Val, []byte("find")) is.Equal(notes[1].Start, 8) is.Equal(notes[1].End(), 8+len(notes[2].Val)) is.Equal(notes[1].Kind, "thiskind") is.Equal(notes[2].Val, []byte("able")) is.Equal(notes[2].Start, 23) is.Equal(notes[2].End(), 23+len(notes[1].Val)) is.Equal(notes[2].Kind, "thiskind") }
func TestTryDoSuccessful(t *testing.T) { is := is.New(t) callCount := 0 err := try.Do(func(attempt int) (bool, error) { callCount++ return attempt < 5, nil }) is.NoErr(err) is.Equal(callCount, 1) }
func TestSum(t *testing.T) { is := is.New(t) id1, err := asset.Identify(strings.NewReader("foo")) is.NoErr(err) id2, err := asset.Identify(strings.NewReader("bar")) is.NoErr(err) is.True(id2.Less(id1)) sr := strings.NewReader(id2.String() + id1.String()) expectedSum, err := asset.Identify(sr) is.NoErr(err) testSum, err := id1.Sum(id2) is.NoErr(err) is.Equal(expectedSum, testSum) }
func TestLineComments(t *testing.T) { is := is.New(t) l, err := parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`)) is.NoErr(err) is.Equal(l.Comment, " comments should be ignored") detail := l.Detail() is.OK(detail) is.Equal(detail.Key, "Key") is.Equal(detail.Value.Data, "Value") l, err = parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`)) is.NoErr(err) is.Equal(string(l.Bytes), `* Key: "Value"`) // if the comment contains a {placeholder}, grab it l, err = parse.ParseLine(0, []byte(`* Key: "Value" // will be stored as {id}`)) is.NoErr(err) is.Equal(l.Comment, " will be stored as {id}") is.Equal(l.Capture(), "id") }
func TestFindManyString(t *testing.T) { is := is.New(t) src := "This is a #long string written by @mat containing links to https://downlist.io/." notes, err := anno.FindManyString(src, anno.URLs, anno.Mentions, anno.Hashtags) is.NoErr(err) is.Equal(len(notes), 3) }
func TestFindManyURLsWithPunctuation(t *testing.T) { is := is.New(t) src := "What do you think about Facebook.com and Yahoo.com and Google.com?" notes, err := anno.FindManyString(src, anno.URLs) is.NoErr(err) is.Equal(len(notes), 3) }