ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.
https://www.ardanlabs.com
Apache License 2.0
3.4k stars 613 forks source link

Unit test #378

Closed Ali-Farhadnia closed 1 month ago

Ali-Farhadnia commented 1 month ago

In business tests. Why is the result stored in the database not checked and only the result returned by the core API is tested?

for example in user update test:

func userUpdate(dbt *dbtest.Test, sd dbtest.SeedData) []dbtest.UnitTable { email, _ := mail.ParseAddress("jack@ardanlabs.com")

table := []dbtest.UnitTable{
    {
        Name: "basic",
        ExpResp: userbus.User{
            ID:          sd.Users[0].ID,
            Name:        "Jack Kennedy",
            Email:       *email,
            Roles:       []userbus.Role{userbus.RoleAdmin},
            Department:  "IT",
            Enabled:     true,
            DateCreated: sd.Users[0].DateCreated,
        },
        ExcFunc: func(ctx context.Context) any {
            uu := userbus.UpdateUser{
                Name:            dbtest.StringPointer("Jack Kennedy"),
                Email:           email,
                Roles:           []userbus.Role{userbus.RoleAdmin},
                Department:      dbtest.StringPointer("IT"),
                Password:        dbtest.StringPointer("1234"),
                PasswordConfirm: dbtest.StringPointer("1234"),
            }

            resp, err := dbt.Core.BusCrud.User.Update(ctx, sd.Users[0].User, uu)
            if err != nil {
                return err
            }

            return resp
        },
        CmpFunc: func(got any, exp any) string {
            gotResp, exists := got.(userbus.User)
            if !exists {
                return "error occurred"
            }

            if err := bcrypt.CompareHashAndPassword(gotResp.PasswordHash, []byte("1234")); err != nil {
                return err.Error()
            }

            expResp := exp.(userbus.User)

            expResp.PasswordHash = gotResp.PasswordHash
            expResp.DateUpdated = gotResp.DateUpdated

            return cmp.Diff(gotResp, expResp)
        },
    },
}

return table

}

ardan-bkennedy commented 1 month ago

Everything gets check by virtual of running all the tests. Remember we seed the database and query. If there is no error on these create tests, I feel I can assume it's ok.

Ali-Farhadnia commented 1 month ago

OK, but do we cover something like this(https://github.com/ardanlabs/service/commit/2750ba139db2100c5aba688a95eec8cdbf45ac0f) in the tests? Because this item was added to the tests before refactoring the tests.

Ali-Farhadnia commented 1 month ago

Sorry i mean this https://github.com/ardanlabs/service/commit/a31dccbfd909f942f0e5746cace5cd47eee4e14d

ardan-bkennedy commented 1 month ago

I changed the test to follow the table table model. This also allowed me to write individual tests for the different activities. All that being said, it's not necessary wrong to add a query call to the CUD tests. The question is how important is it since we have an individual query test? I think one problem with tests (and I am guity) is a lot of duplication.

Ali-Farhadnia commented 1 month ago

Thanks for clarifying, Bill.