Skip to content

Commit

Permalink
Fix embedded primary field (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs02 committed Oct 21, 2023
1 parent 2e07fc1 commit 738fb89
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions document_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (cdm *cachedDocumentMeta) mergeEmbedded(other cachedDocumentMeta, indexPref
cdm.hasOne = appendWithPrefix(cdm.hasOne, other.hasOne, namePrefix)
cdm.hasMany = appendWithPrefix(cdm.hasMany, other.hasMany, namePrefix)
cdm.primaryField = appendWithPrefix(cdm.primaryField, other.primaryField, namePrefix)
for index := range other.primaryIndex {
cdm.primaryIndex = append(cdm.primaryIndex, append([]int{indexPrefix}, index))
for _, index := range other.primaryIndex {
cdm.primaryIndex = append(cdm.primaryIndex, append([]int{indexPrefix}, index...))
}
cdm.preload = appendWithPrefix(cdm.preload, other.preload, namePrefix)
cdm.flag |= other.flag
Expand Down
33 changes: 32 additions & 1 deletion document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestDocument_IndexFieldEmbedded(t *testing.T) {
assert.Equal(t, index, doc.Index())
}

func TestDocument_EmbeddedNameConfict(t *testing.T) {
func TestDocument_embeddedNameConfict(t *testing.T) {
type Embedded struct {
Name string
}
Expand All @@ -276,6 +276,37 @@ func TestDocument_EmbeddedNameConfict(t *testing.T) {
})
}

func TestDocument_embeddedNested(t *testing.T) {
type Base struct {
ID string
}

type Audited struct {
Base
CreatedAt time.Time
UpdatedAt time.Time
}
type Contact struct {
Audited
Name string
}

var (
contact Contact
doc = NewDocument(&contact)
index = map[string][]int{
"id": {0, 0, 0},
"created_at": {0, 1},
"updated_at": {0, 2},
"name": {1},
}
)

assert.Equal(t, index, doc.Index())
assert.Equal(t, []string{"id"}, doc.meta.primaryField)
assert.Equal(t, [][]int{{0, 0, 0}}, doc.meta.primaryIndex)
}

func TestDocument_Types(t *testing.T) {
var (
entity = struct {
Expand Down
55 changes: 55 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,61 @@ func TestRepository_Update_lockVersion(t *testing.T) {
adapter.AssertExpectations(t)
}

func TestRepository_Update_embed(t *testing.T) {
type Base struct {
Id string
}

type Audited struct {
Base
CreatedAt time.Time
UpdatedAt time.Time
}

type Contact struct {
Audited
Name string
}

var (
adapter = &testAdapter{}
repo = New(adapter)
contact = Contact{
Audited: Audited{
Base: Base{
Id: "1",
},
CreatedAt: Now(),
UpdatedAt: Now(),
},
Name: "name",
}
mutates = map[string]Mutate{
"id": Set("id", "1"),
"name": Set("name", "name"),
"created_at": Set("created_at", Now()),
"updated_at": Set("updated_at", Now()),
}
queries = From("contacts").Where(Eq("id", contact.Id))
)

adapter.On("Update", queries, "id", mutates).Return(1, nil).Once()

assert.Nil(t, repo.Update(context.TODO(), &contact))
assert.Equal(t, Contact{
Audited: Audited{
Base: Base{
Id: "1",
},
CreatedAt: Now(),
UpdatedAt: Now(),
},
Name: "name",
}, contact)

adapter.AssertExpectations(t)
}

func TestRepository_Update_notFound(t *testing.T) {
var (
user = User{ID: 1}
Expand Down

0 comments on commit 738fb89

Please sign in to comment.