github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/datatools/alter_test.go (about)

     1  package datatools_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	_ "github.com/lmorg/murex/builtins"
     8  	"github.com/lmorg/murex/test"
     9  	"github.com/lmorg/murex/utils/json"
    10  )
    11  
    12  type plan struct {
    13  	original string
    14  	path     string
    15  	change   string
    16  	expected string
    17  }
    18  
    19  func reMarshal(s string) string {
    20  	var v interface{}
    21  	err := json.Unmarshal([]byte(s), &v)
    22  	if err != nil {
    23  		panic(err.Error())
    24  	}
    25  	b, err := json.Marshal(&v, false)
    26  	if err != nil {
    27  		panic(err.Error())
    28  	}
    29  	return string(b)
    30  }
    31  
    32  func alterTest(t *testing.T, p *plan, flag string) {
    33  	test.RunMurexTests([]test.MurexTest{
    34  		{
    35  			Block: fmt.Sprintf("tout json (%s) -> alter: %s %s (%s)",
    36  				p.original, flag, p.path, p.change),
    37  			Stdout: reMarshal(p.expected),
    38  		},
    39  	}, t)
    40  }
    41  
    42  func TestUpdateMap(t *testing.T) {
    43  	test := plan{
    44  		original: `{"1": "foo", "2": "bar"}`,
    45  		path:     "/2",
    46  		change:   `test`,
    47  		expected: `{
    48  						"1": "foo",
    49  						"2": "test"
    50  					}`,
    51  	}
    52  
    53  	alterTest(t, &test, "")
    54  }
    55  
    56  func TestUpdateNestedMap(t *testing.T) {
    57  	test := plan{
    58  		original: `
    59  			{
    60  				"1": "foo",
    61  				"2": "bar",
    62  				"3": {
    63  					"a": "aye",
    64  					"b": "bee",
    65  					"c": "cee"
    66  				}
    67  			}`,
    68  		path: "/3",
    69  		change: `
    70  			{
    71  				"d": "dee",
    72  				"e": "ee",
    73  				"f": "eff"
    74  			}`,
    75  		expected: `
    76  		{
    77  			"1": "foo",
    78  			"2": "bar",
    79  			"3": {
    80  				"d": "dee",
    81  				"e": "ee",
    82  				"f": "eff"
    83  			}
    84  		}`,
    85  	}
    86  
    87  	alterTest(t, &test, "")
    88  }
    89  
    90  func TestNewMap(t *testing.T) {
    91  	test := plan{
    92  		original: `{"1": "foo", "2": "bar"}`,
    93  		path:     "/3",
    94  		change:   `{"3": "test"}`,
    95  		expected: `{
    96  						"1": "foo",
    97  						"2": "bar",
    98  						"3": {
    99  							"3": "test"
   100  						}
   101  					}`,
   102  	}
   103  
   104  	alterTest(t, &test, "")
   105  }
   106  
   107  func TestUpdateArrayAlpha(t *testing.T) {
   108  	test := plan{
   109  		original: `["foo", "bar"]`,
   110  		path:     "/1",
   111  		change:   `test`,
   112  		expected: `[
   113  						"foo",
   114  						"test"
   115  					]`,
   116  	}
   117  
   118  	alterTest(t, &test, "")
   119  }
   120  
   121  func TestUpdateArrayNumeric(t *testing.T) {
   122  	test := plan{
   123  		original: `[1, 2]`,
   124  		path:     "/1",
   125  		change:   `3`,
   126  		expected: `[
   127  						1,
   128  						3
   129  					]`,
   130  	}
   131  
   132  	alterTest(t, &test, "")
   133  }
   134  
   135  func TestNewArray(t *testing.T) {
   136  	test := plan{
   137  		original: `{"1": "foo", "2": "bar"}`,
   138  		path:     "/3",
   139  		change:   `[4, 5, 6]`,
   140  		expected: `{
   141  						"1": "foo",
   142  						"2": "bar",
   143  						"3": [
   144  							4,
   145  							5,
   146  							6
   147  						]
   148  					}`,
   149  	}
   150  
   151  	alterTest(t, &test, "")
   152  }
   153  
   154  func TestUpdateNestedArrayAlpha(t *testing.T) {
   155  	test := plan{
   156  		original: `
   157  			{
   158  				"1": "foo",
   159  				"2": "bar",
   160  				"3": [
   161  					"aye",
   162  					"bee",
   163  					"cee"
   164  				]
   165  			}`,
   166  		path: "/3",
   167  		change: `
   168  			[
   169  				"dee",
   170  				"ee",
   171  				"eff"
   172  			]`,
   173  		expected: `
   174  		{
   175  			"1": "foo",
   176  			"2": "bar",
   177  			"3": [
   178  				"dee",
   179  				"ee",
   180  				"eff"
   181  			]
   182  		}`,
   183  	}
   184  
   185  	alterTest(t, &test, "")
   186  }
   187  
   188  func TestUpdateNestedArrayNumeric(t *testing.T) {
   189  	test := plan{
   190  		original: `
   191  			{
   192  				"1": "foo",
   193  				"2": "bar",
   194  				"3": [
   195  					4,
   196  					5,
   197  					6
   198  				]
   199  			}`,
   200  		path: "/3",
   201  		change: `
   202  			[
   203  				7,
   204  				8,
   205  				9
   206  			]`,
   207  		expected: `
   208  		{
   209  			"1": "foo",
   210  			"2": "bar",
   211  			"3": [
   212  				7,
   213  				8,
   214  				9
   215  			]
   216  		}`,
   217  	}
   218  
   219  	alterTest(t, &test, "")
   220  }
   221  
   222  func TestUpdateArrayDiffDataTypesInt(t *testing.T) {
   223  	test := plan{
   224  		original: `[ 1, 2, 3 ]`,
   225  		path:     "/1",
   226  		change:   `10`,
   227  		expected: `[ 1, 10, 3 ]`,
   228  	}
   229  
   230  	alterTest(t, &test, "")
   231  }
   232  
   233  func TestUpdateArrayDiffDataTypesFloat(t *testing.T) {
   234  	test := plan{
   235  		original: `[ 1.1, 2.2, 3.3 ]`,
   236  		path:     "/1",
   237  		change:   `10.01`,
   238  		expected: `[ 1.1, 10.01, 3.3 ]`,
   239  	}
   240  
   241  	alterTest(t, &test, "")
   242  }
   243  
   244  func TestUpdateArrayDiffDataTypesBoolTrue(t *testing.T) {
   245  	test := plan{
   246  		original: `[ true, true, true ]`,
   247  		path:     "/1",
   248  		change:   `false`,
   249  		expected: `[ true, false, true ]`,
   250  	}
   251  
   252  	alterTest(t, &test, "")
   253  }
   254  
   255  func TestUpdateArrayDiffDataTypesBoolFalse(t *testing.T) {
   256  	test := plan{
   257  		original: `[ false, false, false ]`,
   258  		path:     "/1",
   259  		change:   `true`,
   260  		expected: `[ false, true, false ]`,
   261  	}
   262  
   263  	alterTest(t, &test, "")
   264  }
   265  
   266  func TestUpdateArrayDiffDataTypesString(t *testing.T) {
   267  	test := plan{
   268  		original: `[ "a", "b", "c" ]`,
   269  		path:     "/1",
   270  		change:   `z`,
   271  		expected: `[ "a", "z", "c" ]`,
   272  	}
   273  
   274  	alterTest(t, &test, "")
   275  }
   276  
   277  func TestUpdateArrayFloat(t *testing.T) {
   278  	test := plan{
   279  		original: `[ 1.1, 2.2, 3.3 ]`,
   280  		path:     "/1",
   281  		change:   `4.4`,
   282  		expected: `[ 1.1, 4.4, 3.3 ]`,
   283  	}
   284  
   285  	alterTest(t, &test, "")
   286  }