...

Source file src/github.com/clbanning/mxj/v2/updatevalues_test.go

Documentation: github.com/clbanning/mxj/v2

     1  // modifyvalues_test.go - test keyvalues.go methods
     2  
     3  package mxj
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  )
     9  
    10  func TestUVHeader(t *testing.T) {
    11  	fmt.Println("\n----------------  updatevalues_test.go ...")
    12  }
    13  
    14  func TestUpdateValuesForPath_Author(t *testing.T) {
    15  	m, merr := NewMapXml(doc1)
    16  	if merr != nil {
    17  		t.Fatal("merr:", merr.Error())
    18  	}
    19  	fmt.Println("m:", m)
    20  
    21  	ss, _ := m.ValuesForPath("doc.books.book.author")
    22  	for _, v := range ss {
    23  		fmt.Println("v:", v)
    24  	}
    25  	fmt.Println("m.UpdateValuesForPath(\"author:NoName\", \"doc.books.book.author\")")
    26  	n, err := m.UpdateValuesForPath("author:NoName", "doc.books.book.author")
    27  	if err != nil {
    28  		t.Fatal("err:", err.Error())
    29  	}
    30  	fmt.Println(n, "updates")
    31  	ss, _ = m.ValuesForPath("doc.books.book.author")
    32  	for _, v := range ss {
    33  		fmt.Println("v:", v)
    34  	}
    35  
    36  	fmt.Println("m.UpdateValuesForPath(\"author:William Gadddis\", \"doc.books.book.author\", \"title:The Recognitions\")")
    37  	n, err = m.UpdateValuesForPath("author:William Gadddis", "doc.books.book.author", "title:The Recognitions")
    38  	o, _ := m.UpdateValuesForPath("author:Austin Tappen Wright", "doc.books.book", "title:Islandia")
    39  	p, _ := m.UpdateValuesForPath("author:John Hawkes", "doc.books.book", "title:The Beetle Leg")
    40  	q, _ := m.UpdateValuesForPath("author:T. E. Porter", "doc.books.book", "title:King's Day")
    41  	if err != nil {
    42  		t.Fatal("err:", err.Error())
    43  	}
    44  	fmt.Println(n+o+p+q, "updates")
    45  	ss, _ = m.ValuesForPath("doc.books.book.author")
    46  	for _, v := range ss {
    47  		fmt.Println("v:", v)
    48  	}
    49  
    50  	fmt.Println("m.UpdateValuesForPath(\"author:William T. Gaddis\", \"doc.books.book.*\", \"title:The Recognitions\")")
    51  	n, _ = m.UpdateValuesForPath("author:William T. Gaddis", "doc.books.book.*", "title:The Recognitions")
    52  	fmt.Println(n, "updates")
    53  	ss, _ = m.ValuesForPath("doc.books.book.author")
    54  	for _, v := range ss {
    55  		fmt.Println("v:", v)
    56  	}
    57  
    58  	fmt.Println("m.UpdateValuesForPath(\"title:The Cannibal\", \"doc.books.book.title\", \"author:John Hawkes\")")
    59  	n, _ = m.UpdateValuesForPath("title:The Cannibal", "doc.books.book.title", "author:John Hawkes")
    60  	o, _ = m.UpdateValuesForPath("review:A novel on his experiences in WWII.", "doc.books.book.review", "title:The Cannibal")
    61  	fmt.Println(n+o, "updates")
    62  	ss, _ = m.ValuesForPath("doc.books.book")
    63  	for _, v := range ss {
    64  		fmt.Println("v:", v)
    65  	}
    66  
    67  	fmt.Println("m.UpdateValuesForPath(\"books:\", \"doc.books\")")
    68  	n, _ = m.UpdateValuesForPath("books:", "doc.books")
    69  	fmt.Println(n, "updates")
    70  	fmt.Println("m:", m)
    71  
    72  	fmt.Println("m.UpdateValuesForPath(mm, \"*\")")
    73  	mm, _ := NewMapXml(doc1)
    74  	n, err = m.UpdateValuesForPath(mm, "*")
    75  	if err != nil {
    76  		t.Fatal("err:", err.Error())
    77  	}
    78  	fmt.Println(n, "updates")
    79  	fmt.Println("m:", m)
    80  
    81  	// ---------------------- newDoc
    82  	var newDoc = []byte(`<tag color="green" shape="square">simple element</tag>`)
    83  	m, merr = NewMapXml(newDoc)
    84  	if merr != nil {
    85  		t.Fatal("merr:", merr.Error())
    86  	}
    87  	fmt.Println("\nnewDoc:", string(newDoc))
    88  	fmt.Println("m:", m)
    89  	fmt.Println("m.UpdateValuesForPath(\"" + textK + ":maybe not so simple element\", \"tag\")")
    90  	n, _ = m.UpdateValuesForPath(textK + ":maybe not so simple element", "tag")
    91  	fmt.Println("n:", n, "m:", m)
    92  	fmt.Println("m.UpdateValuesForPath(\"" + textK + ":simple element again\", \"*\")")
    93  	n, _ = m.UpdateValuesForPath(textK + ":simple element again", "*")
    94  	fmt.Println("n:", n, "m:", m)
    95  
    96  	/*
    97  		fmt.Println("UpdateValuesForPath, doc.books.book, title:The Recognitions : NoBook")
    98  		n, err = m.UpdateValuesForPath("NoBook", "doc.books.book", "title:The Recognitions")
    99  		if err != nil {
   100  			t.Fatal("err:", err.Error())
   101  		}
   102  		fmt.Println(n, "updates")
   103  		ss, _ = m.ValuesForPath("doc.books.book")
   104  		for _, v := range ss {
   105  			fmt.Println("v:", v)
   106  		}
   107  
   108  		fmt.Println("UpdateValuesForPath, doc.books.book.title -seq=3: The Blood Oranges")
   109  		n, err = m.UpdateValuesForPath("The Blood Oranges", "doc.books.book.title", "-seq:3")
   110  		if err != nil {
   111  			t.Fatal("err:", err.Error())
   112  		}
   113  		fmt.Println(n, "updates")
   114  		ss, _ = m.ValuesForPath("doc.books.book.title")
   115  		for _, v := range ss {
   116  			fmt.Println("v:", v)
   117  		}
   118  	*/
   119  }
   120  
   121  var authorDoc = []byte(`
   122  <biblio>
   123  	<author>
   124  		<name>William Gaddis</name>
   125  		<books>
   126  			<book>
   127  				<title>The Recognitions</title>
   128  				<date>1955</date>
   129  				<review>A novel that changed the face of American literature.</review>
   130  			</book>
   131  			<book>
   132  				<title>JR</title>
   133  				<date>1975</date>
   134  				<review>Winner of National Book Award for Fiction.</review>
   135  			</book>
   136  		</books>
   137  	</author>
   138  	<author>
   139  		<name>John Hawkes</name>
   140  		<books>
   141  			<book>
   142  				<title>The Cannibal</title>
   143  				<date>1949</date>
   144  				<review>A novel on his experiences in WWII.</review>
   145  			</book>
   146  			<book>
   147  				<title>The Beetle Leg</title>
   148  				<date>1951</date>
   149  				<review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
   150  			</book>
   151  			<book>
   152  				<title>The Blood Oranges</title>
   153  				<date>1970</date>
   154  				<review>Where everyone wants to vacation.</review>
   155  			</book>
   156  		</books>
   157  	</author>
   158  </biblio>`)
   159  
   160  func TestAuthorDoc(t *testing.T) {
   161  	m, merr := NewMapXml(authorDoc)
   162  	if merr != nil {
   163  		t.Fatal("merr:", merr.Error())
   164  	}
   165  	fmt.Println(m.StringIndent())
   166  
   167  	fmt.Println("m.UpdateValuesForPath(\"review:National Book Award winner.\", \"*.*.*.*\", \"title:JR\")")
   168  	n, _ := m.UpdateValuesForPath("review:National Book Award winner.", "*.*.*.*", "title:JR")
   169  	fmt.Println(n, "updates")
   170  	ss, _ := m.ValuesForPath("biblio.author", "name:William Gaddis")
   171  	for _, v := range ss {
   172  		fmt.Println("v:", v)
   173  	}
   174  
   175  	fmt.Println("m.UpdateValuesForPath(newVal, path, oldVal)")
   176  	path := m.PathForKeyShortest("date")
   177  	v, _ := m.ValuesForPath(path)
   178  	var counter int
   179  	for _, vv := range v {
   180  		oldVal := "date:" + vv.(string)
   181  		newVal := "date:" + vv.(string) + ":num"
   182  		n, _ = m.UpdateValuesForPath(newVal, path, oldVal)
   183  		counter += n
   184  	}
   185  	fmt.Println(counter, "updates")
   186  	fmt.Println(m.StringIndent())
   187  }
   188  

View as plain text