github.com/boki/go-xmp@v1.0.1/test/sync_test.go (about)

     1  // Copyright (c) 2017-2018 Alexander Eichhorn
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	_ "trimmer.io/go-xmp/models"
    22  	"trimmer.io/go-xmp/xmp"
    23  )
    24  
    25  var SyncMergeTestcases = map[xmp.Path]string{
    26  	xmp.Path("dc:format"):       "format",    // string
    27  	xmp.Path("dc:title"):        "the_title", // AltString
    28  	xmp.Path("dc:type"):         "type",      // StringArray
    29  	xmp.Path("myns:type"):       "hello",
    30  	xmp.Path("myns:root/child"): "hello2",
    31  }
    32  
    33  func TestMergeCreate(T *testing.T) {
    34  	d1 := xmp.NewDocument()
    35  	for n, v := range SyncMergeTestcases {
    36  		if err := d1.SetPath(xmp.PathValue{
    37  			Path:      n,
    38  			Value:     v,
    39  			Namespace: "exotic:ns:42",
    40  			Flags:     xmp.CREATE,
    41  		}); err != nil {
    42  			T.Errorf("%v: %v", n, err)
    43  		}
    44  	}
    45  	d2 := xmp.NewDocument()
    46  	if err := d2.Merge(d1, xmp.MERGE); err != nil {
    47  		T.Errorf("merge failed: %v", err)
    48  	}
    49  	for n, v := range SyncMergeTestcases {
    50  		if val, err := d2.GetPath(n); err != nil {
    51  			T.Errorf("%v: get failed: %v", n, err)
    52  		} else if v != val {
    53  			T.Errorf("%v: invalid contents, expected=%s got=%s", n, v, val)
    54  		}
    55  	}
    56  }
    57  
    58  func TestUpdateAfterMerge(T *testing.T) {
    59  	d1 := xmp.NewDocument()
    60  	for n, v := range SyncMergeTestcases {
    61  		if err := d1.SetPath(xmp.PathValue{
    62  			Path:      n,
    63  			Value:     v,
    64  			Namespace: "exotic:ns:42",
    65  			Flags:     xmp.CREATE,
    66  		}); err != nil {
    67  			T.Errorf("%v: %v", n, err)
    68  		}
    69  	}
    70  	d2 := xmp.NewDocument()
    71  	if err := d2.Merge(d1, xmp.MERGE); err != nil {
    72  		T.Errorf("merge failed: %v", err)
    73  	}
    74  	for n, v := range SyncMergeTestcases {
    75  		if err := d1.SetPath(xmp.PathValue{
    76  			Path:  n,
    77  			Value: v + "_updated",
    78  			Flags: xmp.REPLACE,
    79  		}); err != nil {
    80  			T.Errorf("%v: set failed: %v", n, err)
    81  		}
    82  		if val, err := d2.GetPath(n); err != nil {
    83  			T.Errorf("%v: get failed: %v", n, err)
    84  		} else if v != val {
    85  			T.Errorf("%v: invalid contents, expected=%s got=%s", n, v+"_updated", val)
    86  		}
    87  	}
    88  }
    89  
    90  func TestMergeDocuments(T *testing.T) {
    91  	for _, v := range testfiles {
    92  		f, err := os.Open(v)
    93  		if err != nil {
    94  			T.Logf("Cannot open sample '%s': %v", v, err)
    95  			continue
    96  		}
    97  		dec := xmp.NewDecoder(f)
    98  		d1 := xmp.NewDocument() //&xmp.Document{}
    99  		if err := dec.Decode(d1); err != nil {
   100  			T.Errorf("%s: %v", v, err)
   101  		}
   102  		f.Close()
   103  		// set the model dirty so ListPaths will call SyncToXmp to make sure all
   104  		// cross-model relations are in sync; this is required to get the same result
   105  		// compared to d2 (after merge) which will also run ListPaths on a dirty document
   106  		d1.SetDirty()
   107  		p1, err := d1.ListPaths()
   108  		if err != nil {
   109  			T.Errorf("%s: %v", v, err)
   110  			continue
   111  		}
   112  		d2 := xmp.NewDocument()
   113  		if err := d2.Merge(d1, xmp.MERGE); err != nil {
   114  			T.Errorf("merge failed for '%s': %v", v, err)
   115  			continue
   116  		}
   117  		p2, err := d2.ListPaths()
   118  		if err != nil {
   119  			T.Errorf("%s: %v", v, err)
   120  			continue
   121  		}
   122  		// compare all paths
   123  		if l1, l2 := len(p1), len(p2); l1 != l2 {
   124  			T.Errorf("merge '%s': invalid number of paths: exp=%d got=%d", v, l1, l2)
   125  			for _, v := range p1.Diff(p2) {
   126  				var op byte
   127  				switch v.Flags {
   128  				case xmp.DELETE:
   129  					op = '-'
   130  				case xmp.CREATE:
   131  					op = '+'
   132  				case xmp.REPLACE:
   133  					op = '#'
   134  				}
   135  				T.Errorf("> %c %s '%s'", op, v.Path.String(), v.Value)
   136  			}
   137  			continue
   138  		}
   139  		for i, x := range p1 {
   140  			if x.Value != p2[i].Value {
   141  				T.Errorf("merge '%s': value mismatch on path '%s': exp=%s got=%s", v, x.Path.String(), x.Value, p2[i].Value)
   142  			}
   143  		}
   144  	}
   145  }