github.com/tomwright/dasel@v1.27.3/storage/csv_test.go (about)

     1  package storage_test
     2  
     3  import (
     4  	"github.com/tomwright/dasel/storage"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  var csvBytes = []byte(`id,name
    10  1,Tom
    11  2,Jim
    12  `)
    13  var csvMap = []map[string]interface{}{
    14  	{
    15  		"id":   "1",
    16  		"name": "Tom",
    17  	},
    18  	{
    19  		"id":   "2",
    20  		"name": "Jim",
    21  	},
    22  }
    23  
    24  func TestCSVParser_FromBytes(t *testing.T) {
    25  	got, err := (&storage.CSVParser{}).FromBytes(csvBytes)
    26  	if err != nil {
    27  		t.Errorf("unexpected error: %s", err)
    28  		return
    29  	}
    30  	if !reflect.DeepEqual(&storage.CSVDocument{
    31  		Value:   csvMap,
    32  		Headers: []string{"id", "name"},
    33  	}, got) {
    34  		t.Errorf("expected %v, got %v", csvMap, got)
    35  	}
    36  }
    37  
    38  func TestCSVParser_FromBytes_Error(t *testing.T) {
    39  	_, err := (&storage.CSVParser{}).FromBytes(nil)
    40  	if err == nil {
    41  		t.Errorf("expected error but got none")
    42  		return
    43  	}
    44  	_, err = (&storage.CSVParser{}).FromBytes([]byte(`a,b
    45  a,b,c`))
    46  	if err == nil {
    47  		t.Errorf("expected error but got none")
    48  		return
    49  	}
    50  	_, err = (&storage.CSVParser{}).FromBytes([]byte(`a,b,c
    51  a,b`))
    52  	if err == nil {
    53  		t.Errorf("expected error but got none")
    54  		return
    55  	}
    56  }
    57  
    58  func TestCSVParser_ToBytes(t *testing.T) {
    59  	t.Run("CSVDocument", func(t *testing.T) {
    60  		got, err := (&storage.CSVParser{}).ToBytes(&storage.CSVDocument{
    61  			Value:   csvMap,
    62  			Headers: []string{"id", "name"},
    63  		})
    64  		if err != nil {
    65  			t.Errorf("unexpected error: %s", err)
    66  			return
    67  		}
    68  		if !reflect.DeepEqual(csvBytes, got) {
    69  			t.Errorf("expected %v, got %v", string(csvBytes), string(got))
    70  		}
    71  	})
    72  	t.Run("SingleDocument", func(t *testing.T) {
    73  		got, err := (&storage.CSVParser{}).ToBytes(&storage.BasicSingleDocument{
    74  			Value: map[string]interface{}{
    75  				"id":   "1",
    76  				"name": "Tom",
    77  			},
    78  		})
    79  		if err != nil {
    80  			t.Errorf("unexpected error: %s", err)
    81  			return
    82  		}
    83  		deepEqualOneOf(t, got, []byte(`id,name
    84  1,Tom
    85  `), []byte(`name,id
    86  Tom,1
    87  `))
    88  	})
    89  	t.Run("SingleDocumentSlice", func(t *testing.T) {
    90  		got, err := (&storage.CSVParser{}).ToBytes(&storage.BasicSingleDocument{
    91  			Value: []interface{}{
    92  				map[string]interface{}{
    93  					"id":   "1",
    94  					"name": "Tom",
    95  				},
    96  				map[string]interface{}{
    97  					"id":   "2",
    98  					"name": "Tommy",
    99  				},
   100  			},
   101  		})
   102  		if err != nil {
   103  			t.Errorf("unexpected error: %s", err)
   104  			return
   105  		}
   106  		deepEqualOneOf(t, got, []byte(`id,name
   107  1,Tom
   108  2,Tommy
   109  `), []byte(`name,id
   110  Tom,1
   111  `))
   112  	})
   113  	t.Run("MultiDocument", func(t *testing.T) {
   114  		got, err := (&storage.CSVParser{}).ToBytes(&storage.BasicMultiDocument{
   115  			Values: []interface{}{
   116  				map[string]interface{}{
   117  					"id":   "1",
   118  					"name": "Tom",
   119  				},
   120  				map[string]interface{}{
   121  					"id":   "2",
   122  					"name": "Jim",
   123  				},
   124  			},
   125  		})
   126  		if err != nil {
   127  			t.Errorf("unexpected error: %s", err)
   128  			return
   129  		}
   130  		deepEqualOneOf(t, got, []byte(`id,name
   131  1,Tom
   132  id,name
   133  2,Jim
   134  `), []byte(`name,id
   135  Tom,1
   136  id,name
   137  2,Jim
   138  `), []byte(`id,name
   139  1,Tom
   140  name,id
   141  Jim,2
   142  `), []byte(`name,id
   143  Tom,1
   144  name,id
   145  Jim,2
   146  `))
   147  	})
   148  	t.Run("DefaultDocType", func(t *testing.T) {
   149  		got, err := (&storage.CSVParser{}).ToBytes([]interface{}{"x", "y"})
   150  		if err != nil {
   151  			t.Errorf("unexpected error: %s", err)
   152  			return
   153  		}
   154  		deepEqualOneOf(t, got, []byte(`[x y]
   155  `))
   156  	})
   157  }
   158  
   159  func deepEqualOneOf(t *testing.T, got []byte, exps ...[]byte) {
   160  	for _, exp := range exps {
   161  		if reflect.DeepEqual(exp, got) {
   162  			return
   163  		}
   164  	}
   165  	t.Errorf("%s did not match any of the expected values", string(got))
   166  }
   167  
   168  func TestCSVDocument_Documents(t *testing.T) {
   169  	in := &storage.CSVDocument{
   170  		Value: []map[string]interface{}{
   171  			{
   172  				"id":   1,
   173  				"name": "Tom",
   174  			},
   175  			{
   176  				"id":   2,
   177  				"name": "Jim",
   178  			},
   179  		},
   180  		Headers: []string{"id", "name"},
   181  	}
   182  	exp := []interface{}{
   183  		map[string]interface{}{
   184  			"id":   1,
   185  			"name": "Tom",
   186  		},
   187  		map[string]interface{}{
   188  			"id":   2,
   189  			"name": "Jim",
   190  		},
   191  	}
   192  	got := in.Documents()
   193  	if !reflect.DeepEqual(exp, got) {
   194  		t.Errorf("expected %v, got %v", exp, got)
   195  	}
   196  }
   197  
   198  func TestCSVDocument_RealValue(t *testing.T) {
   199  	exp := []map[string]interface{}{
   200  		{
   201  			"id":   1,
   202  			"name": "Tom",
   203  		},
   204  		{
   205  			"id":   2,
   206  			"name": "Jim",
   207  		},
   208  	}
   209  	in := &storage.CSVDocument{
   210  		Value:   exp,
   211  		Headers: []string{"id", "name"},
   212  	}
   213  	got := in.RealValue()
   214  	if !reflect.DeepEqual(exp, got) {
   215  		t.Errorf("expected %v, got %v", exp, got)
   216  	}
   217  }