Skip to content

Commit

Permalink
Improve UUID tests and replace thispersondoesnotexist service with ra…
Browse files Browse the repository at this point in the history
…ndomuser service for profile picture generation (#135)
  • Loading branch information
jaswdr authored Mar 22, 2023
1 parent 4d15ef2 commit 3bd11b1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestPersonImage(t *testing.T) {
p := New().Person()
image := p.Image()
Expect(t, fmt.Sprintf("%T", image), "*os.File")
Expect(t, strings.HasSuffix(image.Name(), ".jfif"), true, image.Name())
Expect(t, strings.HasSuffix(image.Name(), ".jpg"), true, image.Name())
}

func TestPersonaNameMale(t *testing.T) {
Expand Down
16 changes: 11 additions & 5 deletions profile_image.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package faker

import (
"fmt"
"io"
"log"
"os"
)

const profileImageBaseURL = "https://thispersondoesnotexist.com/image"
const (
profileImageBaseURL = "https://randomuser.me"
portraitsEndpoint = "api/portraits"
)

// ProfileImage is a faker struct for ProfileImage
type ProfileImage struct {
Expand All @@ -17,20 +21,22 @@ type ProfileImage struct {

// Image generates a *os.File with a random profile image using the thispersondoesnotexist.com service
func (pi ProfileImage) Image() *os.File {
resp, err := pi.HTTPClient.Get(profileImageBaseURL)
gender := pi.faker.RandomStringElement([]string{"women", "men"})
profileId := pi.faker.IntBetween(1, 99)
url := fmt.Sprintf("%s/%s/%s/%d.jpg", profileImageBaseURL, portraitsEndpoint, gender, profileId)
resp, err := pi.HTTPClient.Get(url)
if err != nil {
log.Println("Error while requesting", profileImageBaseURL, ":", err)
panic(err)
}

defer resp.Body.Close()

f, err := pi.TempFileCreator.TempFile("profil-picture-img-*.jfif")
f, err := pi.TempFileCreator.TempFile("profile-picture-img-*.jpg")
if err != nil {
log.Println("Error while creating a temp file:", err)
panic(err)
}

io.Copy(f, resp.Body)
resp.Body.Close()
return f
}
2 changes: 1 addition & 1 deletion profile_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestProfileImage(t *testing.T) {
f := New()
value := f.ProfileImage().Image()
Expect(t, fmt.Sprintf("%T", value), "*os.File")
Expect(t, strings.HasSuffix(value.Name(), ".jfif"), true, value.Name())
Expect(t, strings.HasSuffix(value.Name(), ".jpg"), true, value.Name())
}

func TestProfileImagePanicIfRequestFails(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ func (s Struct) rSlice(t reflect.Type, v reflect.Value, function string, size in
}

func (s Struct) rString(_ reflect.Type, v reflect.Value, function string) {
if function != "" {
v.SetString(s.Faker.Bothify(function))
} else {
if function == "" {
v.SetString(s.Faker.UUID().V4())
return
}

v.SetString(s.Faker.Bothify(function))
}

func (s Struct) rInt(t reflect.Type, v reflect.Value, function string) {
Expand Down
23 changes: 23 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,26 @@ func TestStructToBool(t *testing.T) {
NotExpect(t, nil, sf.BoolConst)
NotExpect(t, nil, sf.BoolGenerate)
}

func TestStructUUID(t *testing.T) {
var st struct {
UUID string `fake`
}
New().Struct().Fill(&st)
NotExpect(t, "", st.UUID)
}

func TestStructUUIDInSequence(t *testing.T) {
var st struct {
UUID string `fake`
}
fake := New()
before := ""
for i := 0; i < 100; i++ {
fake.Struct().Fill(&st)
after := st.UUID
NotExpect(t, true, after == "")
Expect(t, false, before == after)
before = after
}
}
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (creator ErrorRaiserTempFileCreator) TempFile(_ string) (*os.File, error) {

func TestHTTPClientImplCanDoGetRequests(t *testing.T) {
client := HTTPClientImpl{}
resp, err := client.Get("https://www.google.com")
resp, err := client.Get("https://www.example.com")
Expect(t, err, nil)
Expect(t, resp.StatusCode, http.StatusOK)
}
Expand Down
9 changes: 6 additions & 3 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func TestUUIDv4(t *testing.T) {

func TestUUIDV4UniqueInSequence(t *testing.T) {
f := New()
last := f.UUID().V4()
current := f.UUID().V4()
Expect(t, true, last != current)
before := f.UUID().V4()
for i := 0; i < 100; i++ {
after := f.UUID().V4()
Expect(t, true, before != after)
before = after
}
}

0 comments on commit 3bd11b1

Please sign in to comment.