Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ func newXMLEncoder(w io.Writer, floatPresicion int) *xmlEncoder {
// AddRelationship adds a relationship to the encoded model.
// Duplicated relationships will be removed before encoding.
func (enc *xmlEncoder) AddRelationship(r spec.Relationship) {
enc.relationships = append(enc.relationships, Relationship(r))
hasRelationship := false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to comment above, duplicate relationships should be removed. Logic below seems to prevent duplicate relationships from being added. Why do we need this? Is the duplicate removal not working?

for _, relationship := range enc.relationships {
if relationship.Path == r.Path {
hasRelationship = true
break
}
}
if !hasRelationship {
enc.relationships = append(enc.relationships, Relationship(r))
}
}

// FloatPresicion returns the float presicion to use
Expand Down
61 changes: 61 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"encoding/xml"
"errors"
"image/color"
"path/filepath"
"reflect"
"strconv"
"testing"

"github.com/go-test/deep"
"github.com/hpinc/go3mf/spec"
"github.com/hpinc/go3mf/utils"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -309,3 +311,62 @@ func TestEncoder_Encode_Roundtrip(t *testing.T) {
})
}
}

func TestEncoder_Relationship(t *testing.T) {
type request struct {
fileName string
}
type response struct {
relationships []Relationship
}

tests := []struct {
name string
request request
response response
}{ /*Verfy if a file rels is not update in case of relationship exists*/
{"file_with_texture_rel_but_no_texture",
request{fileName: "super_boogoku_tiny.3mf"},
response{relationships: []Relationship{
{ID: "rel2", Type: "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture", Path: "/Thumbnails/super_boo.png"},
{ID: "rel3", Type: "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture", Path: "/Thumbnails/goku_ss.png"},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
utils.WithProjectDirAndTestTempDirRemoveAtEnd(
filepath.Join("testdata"), func(testFileDir string, testTempDir string) {
testFile := filepath.Join(testFileDir, tt.request.fileName)
outputFile := filepath.Join(testTempDir, "SW3DBUG-2700_changed.3mf")
var model Model
r, err := OpenReader(testFile)
if err != nil {
t.Errorf("TestEncoder_Relationship() error = %v", err)
}
r.Decode(&model)
defer r.Close()
w, err := CreateWriter(outputFile)
if err != nil {
t.Errorf("TestEncoder_Relationship() error = %v", err)
}
w.Encode(&model)
defer w.Close()

var modelUpdated Model
r, err = OpenReader(outputFile)
if err != nil {
t.Errorf("TestEncoder_Relationship() error = %v", err)
}
r.Decode(&modelUpdated)
defer r.Close()

if diff := deep.Equal(modelUpdated.Relationships, tt.response.relationships); diff != nil {
t.Errorf("TestEncoder_Relationship() = %v", diff)
}
})

})
}

}
Binary file added testdata/super_boogoku_tiny.3mf
Binary file not shown.
39 changes: 39 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package utils

import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
)

func GetRootProjectDir() (string, error) {
_, filename, _, _ := runtime.Caller(0)
dir := filepath.Join(path.Dir(filename), "..")
err := os.Chdir(dir)
return dir, err
}

func WithRootProjectDir(callback func(string)) {
dir, err := GetRootProjectDir()
if err != nil {
panic(err)
}
callback(dir)
}

func WithProjectDirAndTestTempDirRemoveAtEnd(fileFolder string, callback func(testFileDir string, testTempDir string)) {
dir, err := GetRootProjectDir()
if err != nil {
panic(err)
}
testTempDir, err := ioutil.TempDir("", "testcase")
if err != nil {
panic(err)
}
defer os.RemoveAll(testTempDir)
defer os.Remove(testTempDir)

callback(filepath.Join(dir, fileFolder), testTempDir)
}