Skip to content

Commit

Permalink
unstaged - do not merge
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Dec 9, 2021
1 parent 950114c commit 81ed83b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 13 deletions.
2 changes: 1 addition & 1 deletion executors_test.go
Expand Up @@ -1193,7 +1193,7 @@ func Test_Eager_Creation_Without_Associations(t *testing.T) {
transaction(func(tx *Connection) {
r := require.New(t)
code := CourseCode{
Course: Course{},
Course: &Course{},
}

err := tx.Eager().Create(&code)
Expand Down
26 changes: 21 additions & 5 deletions pop_test.go
Expand Up @@ -291,14 +291,30 @@ type Course struct {
}

type CourseCode struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
CourseID uuid.UUID `json:"course_id" db:"course_id"`
Course Course `json:"-" belongs_to:"course"`
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
CourseID uuid.NullUUID `json:"course_id" db:"course_id"`
Course *Course `json:"course" belongs_to:"course" fk_id:"CourseID"`
ClassID uuid.UUID `json:"class_id" db:"class_id"`
// Course Course `belongs_to:"course"`
}

type Teacher struct {
ID uuid.UUID `json:"id" db:"id"`
Name string `json:"name" db:"name"`
CurrentClass *Class `json:"class" has_one:"class" fk_id:"current_teacher_id"`
Classes []Class `json:"classes" has_many:"class" fk_id:"teacher_id"`
}

type Class struct {
ID uuid.UUID `json:"id" db:"id"`
Topic string `json:"topic" db:"topic"`
TeacherID uuid.NullUUID `json:"teachers_id" db:"teacher_id"`
CurrentTeacherID uuid.UUID `json:"current_teachers_id" db:"current_teacher_id"`
CourseCodes []CourseCode `json:"course_code_id" has_many:"course_codes"`
}

type ValidatableCar struct {
ID int64 `db:"id"`
Name string `db:"name"`
Expand Down
7 changes: 5 additions & 2 deletions preload_associations.go
Expand Up @@ -360,7 +360,9 @@ func preloadBelongsTo(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaI
fkids := []interface{}{}
mmi.iterate(func(val reflect.Value) {
if !isFieldNilPtr(val, fi) {
fkids = append(fkids, mmi.mapper.FieldByName(val, fi.Path).Interface())
if realValue := mmi.mapper.FieldByName(val, fi.Path).Interface(); !IsZeroOfUnderlyingType(realValue) {
fkids = append(fkids, realValue)
}
}
})

Expand All @@ -376,7 +378,8 @@ func preloadBelongsTo(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaI
q.eagerFields = []string{}

slice := asoc.toSlice()
err := q.Where(fmt.Sprintf("%s in (?)", fk), fkids).All(slice.Interface())
vals := slice.Interface()
err := q.Where(fmt.Sprintf("%s in (?)", fk), fkids).All(vals)
if err != nil {
return err
}
Expand Down
42 changes: 39 additions & 3 deletions preload_associations_test.go
@@ -1,10 +1,10 @@
package pop

import (
"testing"

"github.com/gobuffalo/nulls"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
"testing"
)

func Test_New_Implementation_For_Nplus1(t *testing.T) {
Expand Down Expand Up @@ -82,7 +82,7 @@ func Test_New_Implementation_For_Nplus1_With_UUID(t *testing.T) {
courses = append(courses, course)
if i == 0 {
a.NoError(tx.Create(&CourseCode{
CourseID: course.ID,
CourseID: uuid.NullUUID{UUID: course.ID, Valid: true},
}))
}
}
Expand Down Expand Up @@ -113,6 +113,42 @@ func Test_New_Implementation_For_Nplus1_With_UUID(t *testing.T) {
})
}

func Test_New_Implementation_For_Nplus1_With_NullUUIDs_And_FK_ID(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
teacher := &Teacher{
Name: "Alice",
Class: &Class{
Topic: "Is math related to science?",
CourseCodes: []CourseCode{{}},
},
}

a := require.New(t)

a.NoError(tx.Create(teacher))
for k, cc := range teacher.Class.CourseCodes {
if cc.Course != nil {
a.NoError(tx.Create(cc.Course))
teacher.Class.CourseCodes[k].CourseID = uuid.NullUUID{Valid: true, UUID: cc.Course.ID}
}
}
teacher.Class.TeacherID = uuid.NullUUID{UUID: teacher.ID, Valid: true}
a.NoError(tx.Eager().Create(teacher.Class))

var alice Teacher
a.NoError(tx.EagerPreload(
"Class.CourseCodes.Course",
).First(&alice))

aliceClass := alice.Class
a.Len(aliceClass.CourseCodes, 1)
a.Nil(aliceClass.CourseCodes[0].Course)
})
}

func Test_New_Implementation_For_Nplus1_Single(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
Expand Down
5 changes: 3 additions & 2 deletions testdata/migrations/20181104140606_course_codes.up.fizz
@@ -1,5 +1,6 @@
create_table("course_codes") {
t.Column("id", "uuid", {"primary": true})
t.Column("course_id", "uuid", {})
t.Column("course_id", "uuid", {"null":true})
t.Column("class_id", "uuid")
t.Timestamps()
}
}
1 change: 1 addition & 0 deletions testdata/migrations/20210104145902_teachers.down.fizz
@@ -0,0 +1 @@
drop_table('teachers')
13 changes: 13 additions & 0 deletions testdata/migrations/20210104145902_teachers.up.fizz
@@ -0,0 +1,13 @@
create_table("teachers") {
t.Column("id", "uuid", {"primary": true})
t.Column("name", "string")
t.DisableTimestamps()
}

create_table("classes") {
t.Column("id", "uuid", {"primary": true})
t.Column("topic", "string")
t.Column("teacher_id", "uuid", {"null": true})
t.ForeignKey("teacher_id", {"teachers": ["id"]}, {})
t.DisableTimestamps()
}

0 comments on commit 81ed83b

Please sign in to comment.