jaswdr / faker

:rocket: Ultimate fake data generator for Go with zero dependencies
https://pkg.go.dev/github.com/jaswdr/faker
MIT License
549 stars 59 forks source link

[BUG] UUID generation is not random #134

Closed zenire closed 1 year ago

zenire commented 1 year ago

Describe the bug When I generate UUID's rapidly (for a test for example) the UUIDs are not random and unique.

To Reproduce

func (m *Scan) Factory() {
    fake := faker.New()
    fake.Struct().Fill(&m)
}
for i := 0; i < 5; i++ {
    object := models.Object{}
    object.Factory()
    fmt.Println(object)
}

Expected behavior Generate a random and unique UUID each time.

Desktop (please complete the following information):

Additional context My suggestion is to generate UUID's using a library like "github.com/google/uuid"

jaswdr commented 1 year ago

Hello @zenire, can you share the code of the models.Object{} struct? I tested with the below test in the struct_test.go file and it worked as expected.

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
    }
}

Regarding the usage of the google/uuid library. The purpose of this library is to be self-sufficient and do not depend on any external code.

zenire commented 1 year ago

Thank you for looking into this @jaswdr

package models

import (
    "time"

    "github.com/jaswdr/faker"
    "gorm.io/gorm"
)

// Object is the model for the Object
type Object struct {
    ID                  uuid.UUID `gorm:"primary_key;unique;type:uuid;column:id;default:uuid_generate_v4()"`
    Name                string
    Description              string
    CreatedAt           time.Time
    UpdatedAt           time.Time
    DeletedAt           gorm.DeletedAt `gorm:"index"`
}

// Factory is used to generate fake data
func (m *Object) Factory() {
    fake := faker.New()
    fake.Struct().Fill(m)
    // We don't want to delete the model
    m.DeletedAt = gorm.DeletedAt{}
}

I understand the purpose of the library, and I think it's a good idea to be self-sufficient.

jaswdr commented 1 year ago

I tested the below code with the latest version and it worked as expected, please use the latest version and try again.

func TestFactory(t *testing.T) {
    var st struct {
        UUID string `gorm:"primary_key;unique;type:uuid;column:id;default:uuid_generate_v4()"`
    }

    fake := New()
    fake.Struct().Fill(&st)
    Expect(t, false, st.UUID == "")
}
zenire commented 1 year ago

Unfortunately I'm experiencing the same issue with commit 3bd11b19b027f3c2c15e620a79d15c5013ebaa6d. Will test again next week and share some more details.

MrTbag commented 1 year ago

I opened a PR after mildly changing the way the variant bits were set. It should fix the problem now I think.

jaswdr commented 1 year ago

Fix was merged, I'm closing this for now, if you still face the issue please reopen.