github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/alter/sum_test.go (about)

     1  package alter_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/lmorg/murex/test/count"
     9  	"github.com/lmorg/murex/utils/alter"
    10  )
    11  
    12  func sumTest(t *testing.T, test *plan) {
    13  	t.Helper()
    14  	count.Tests(t, 1)
    15  
    16  	pathS, err := alter.SplitPath(test.path)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	var expV interface{}
    22  	if err := json.Unmarshal([]byte(test.expected), &expV); err != nil {
    23  		panic(err)
    24  	}
    25  	b, err := json.Marshal(expV)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	test.expected = string(b)
    30  
    31  	var old interface{}
    32  	err = json.Unmarshal([]byte(test.original), &old)
    33  	if err != nil {
    34  		t.Error("Error unmarshalling original for alter.Sum()")
    35  		t.Logf("  original: %s", test.original)
    36  		t.Logf("  path:     %s: %v", test.path, pathS)
    37  		t.Logf("  change:   %s", test.change)
    38  		t.Logf("  expected: %s.(%T)", test.expected, expV)
    39  		t.Logf("  actual:   %s", "n/a")
    40  		t.Logf("  error:    %s", err)
    41  		return
    42  	}
    43  
    44  	new := alter.StrToInterface(test.change)
    45  	v, err := alter.Sum(context.TODO(), old, pathS, new)
    46  	if err != nil {
    47  		t.Error("Error received from alter.Sum()")
    48  		t.Logf("  original: %s", test.original)
    49  		t.Logf("  path:     %s: %v", test.path, pathS)
    50  		t.Logf("  change:   %s", test.change)
    51  		t.Logf("  expected: %s.(%T)", test.expected, expV)
    52  		t.Logf("  actual:   %s", "n/a")
    53  		t.Logf("  error:    %s", err)
    54  		return
    55  	}
    56  
    57  	actual, err := json.Marshal(v)
    58  	if err != nil {
    59  		t.Error("Error marshalling v from alter.Sum()")
    60  		t.Logf("  original: %s", test.original)
    61  		t.Logf("  path:     %s: %v", test.path, pathS)
    62  		t.Logf("  change:   %s", test.change)
    63  		t.Logf("  expected: %s.(%T)", test.expected, expV)
    64  		t.Logf("  actual:   %s", "n/a")
    65  		t.Logf("  error:    %s", err)
    66  		return
    67  	}
    68  
    69  	if string(actual) != test.expected {
    70  		t.Error("Expected does not match actual")
    71  		t.Logf("  original: %s.(%T)", test.original, old)
    72  		t.Logf("  path:     %s: %v", test.path, pathS)
    73  		t.Logf("  change:   %s", test.change)
    74  		t.Logf("  expected: %s.(%T)", test.expected, expV)
    75  		t.Logf("  actual:   %s.(%T)", string(actual), v)
    76  		t.Logf("  error:    %s", "nil")
    77  	}
    78  }
    79  
    80  func TestSumMapInt(t *testing.T) {
    81  	test := plan{
    82  		original: `
    83  			{
    84  				"a": 1,
    85  				"b": 2,
    86  				"c": 3
    87  			}`,
    88  		path: "/",
    89  		change: `
    90  			{
    91  				"a": 9,
    92  				"b": 7,
    93  				"c": 5
    94  			}`,
    95  		expected: `
    96  			{
    97  				"a": 10,
    98  				"b": 9,
    99  				"c": 8
   100  			}`,
   101  	}
   102  
   103  	sumTest(t, &test)
   104  }
   105  
   106  func TestSumMapFloat64(t *testing.T) {
   107  	test := plan{
   108  		original: `
   109  			{
   110  				"a": 1.1,
   111  				"b": 2.2,
   112  				"c": 3.3
   113  			}`,
   114  		path: "/",
   115  		change: `
   116  			{
   117  				"a": 9.9,
   118  				"b": 7.7,
   119  				"c": 5.5
   120  			}`,
   121  		expected: `
   122  			{
   123  				"a": 11,
   124  				"b": 9.9,
   125  				"c": 8.8
   126  			}`,
   127  	}
   128  
   129  	sumTest(t, &test)
   130  }
   131  
   132  func TestSumMapInterface(t *testing.T) {
   133  	test := plan{
   134  		original: `
   135  			{
   136  				"a": "1",
   137  				"b": "2",
   138  				"c": "3"
   139  			}`,
   140  		path: "/",
   141  		change: `
   142  			{
   143  				"a": "9",
   144  				"b": "7.5",
   145  				"c": "5"
   146  			}`,
   147  		expected: `
   148  			{
   149  				"a": 10,
   150  				"b": 9.5,
   151  				"c": 8
   152  			}`,
   153  	}
   154  
   155  	sumTest(t, &test)
   156  }