Skip to content

Commit

Permalink
feat(loader): adding more tests for loader (#48)
Browse files Browse the repository at this point in the history
adding more tests for loader
  • Loading branch information
Jacobbrewer1 authored Oct 24, 2024
1 parent e48f50d commit d62ca81
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
6 changes: 2 additions & 4 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (l *loader) loadDiff(old, newT any) error {
if oElem.Type().Field(i).Anonymous {
// If the embedded field is a pointer, dereference it
if oElem.Field(i).Kind() == reflect.Ptr {
if !oElem.Field(i).IsNil() && !nElem.Field(i).IsNil() {
if !oElem.Field(i).IsNil() && !nElem.Field(i).IsNil() { // If both are not nil, we need to recursively call LoadDiff
if err := l.loadDiff(oElem.Field(i).Interface(), nElem.Field(i).Interface()); err != nil {
return err
}
Expand Down Expand Up @@ -107,9 +107,7 @@ func (l *loader) loadDiff(old, newT any) error {

// Compare the old and new fields.
//
// New fields take priority over old fields if they are provided.
//
// We calculate whether we should be updating the field based off the LoaderOption's provided.
// New fields take priority over old fields if they are provided based on the configuration.
if !nElem.Field(i).IsZero() || l.includeZeroValues {
oElem.Field(i).Set(nElem.Field(i))
} else if nElem.Field(i).Kind() == reflect.Ptr && nElem.Field(i).IsNil() && l.includeNilValues {
Expand Down
87 changes: 87 additions & 0 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ func (s *loadDiffSuite) TestLoadDiff_Success() {
s.Equal(26, old.Age)
}

func (s *loadDiffSuite) TestLoadDiff_Success_Pointed_Fields() {
s.l.includeNilValues = true

type testStruct struct {
Name *string
Age *int
}

old := testStruct{
Name: ptr("John"),
Age: ptr(25),
}

n := testStruct{
Name: ptr("John Smith"),
Age: ptr(26),
}

err := s.l.loadDiff(&old, &n)
s.NoError(err)
s.Equal("John Smith", *old.Name)
s.Equal(26, *old.Age)
}

func (s *loadDiffSuite) TestLoadDiff_Success_ZeroValue() {
type testStruct struct {
Name string
Expand Down Expand Up @@ -135,6 +159,69 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct() {
s.Equal(24, old.Partner.Age)
}

func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct_Reverse() {
type testStruct struct {
Name string
Age int
Partner *testStruct
}

old := testStruct{
Name: "John",
Age: 25,
Partner: &testStruct{
Name: "Sarah",
Age: 24,
},
}

n := testStruct{
Partner: &testStruct{
Name: "Sarah Thompson",
Age: 27,
},
}

err := s.l.loadDiff(&old, &n)
s.NoError(err)
s.Equal("John", old.Name)
s.Equal(25, old.Age)
s.Equal("Sarah Thompson", old.Partner.Name)
s.Equal(27, old.Partner.Age)
}

func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct_NotPointed() {
type testEmbed struct {
Name string
Age int
}

type testStruct struct {
Name string
Age int
Partner testEmbed
}

old := testStruct{
Name: "John",
Age: 25,
}

n := testStruct{
Partner: testEmbed{
Name: "Sarah",
Age: 24,
},
}

err := s.l.loadDiff(&old, &n)
s.NoError(err)
s.Equal("John", old.Name)
s.Equal(25, old.Age)
s.Equal("Sarah", old.Partner.Name)
s.Equal(24, old.Partner.Age)
}

func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStructWithNewValue() {
type testStruct struct {
Name string
Expand Down

0 comments on commit d62ca81

Please sign in to comment.