Skip to content

Commit

Permalink
Merge branch 'master' into feature/new-seed-with-number-constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswdr committed May 14, 2023
2 parents b8c2eba + 5d0f88f commit 358aa64
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 18 deletions.
4 changes: 2 additions & 2 deletions address.go
Expand Up @@ -272,7 +272,7 @@ func (a Address) Latitude() (latitude float64) {
}

// Longitude returns a fake longitude for Address
func (a Address) Longitude() (latitude float64) {
latitude, _ = strconv.ParseFloat(a.Faker.Numerify("##.######"), 64)
func (a Address) Longitude() (longitude float64) {
longitude, _ = strconv.ParseFloat(a.Faker.Numerify("##.######"), 64)
return
}
2 changes: 1 addition & 1 deletion person_test.go
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
@@ -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
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
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
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
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
2 changes: 1 addition & 1 deletion uuid.go
Expand Up @@ -17,5 +17,5 @@ func (UUID) V4() (uuid string) {
io.ReadFull(rand.Reader, uiq[:])
uiq[6] = (uiq[6] & 0x0f) | 0x40 // Version 4
uiq[8] = (uiq[8]&(0xff>>2) | (0x02 << 6)) // Variant RFC4122
return fmt.Sprintf("%x%x%x%x%x", uiq[0:4], uiq[4:6], uiq[6:8], uiq[8:10], uiq[10:])
return fmt.Sprintf("%x-%x-%x-%x-%x", uiq[0:4], uiq[4:6], uiq[6:8], uiq[8:10], uiq[10:])
}
11 changes: 7 additions & 4 deletions uuid_test.go
Expand Up @@ -8,14 +8,17 @@ import (
func TestUUIDv4(t *testing.T) {
f := New()
value := f.UUID().V4()
match, err := regexp.MatchString("^[a-fA-F0-9]{8}[a-fA-F0-9]{4}4[a-fA-F0-9]{3}[8|9|aA|bB][a-fA-F0-9]{3}[a-fA-F0-9]{12}$", value)
match, err := regexp.MatchString("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$", value)
Expect(t, true, err == nil)
Expect(t, true, match)
}

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 358aa64

Please sign in to comment.