pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/jsonutil/jsonutil_test.go (about)

     1  package jsonutil
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"errors"
    12  	"io/ioutil"
    13  	"os"
    14  	"testing"
    15  
    16  	. "pkg.re/essentialkaos/check.v1"
    17  
    18  	"pkg.re/essentialkaos/ek.v12/fsutil"
    19  )
    20  
    21  // ////////////////////////////////////////////////////////////////////////////////// //
    22  
    23  const _JSON_DATA = `{
    24    "string": "test",
    25    "integer": 912,
    26    "boolean": true
    27  }
    28  `
    29  
    30  // ////////////////////////////////////////////////////////////////////////////////// //
    31  
    32  type TestStruct struct {
    33  	String  string `json:"string"`
    34  	Integer int    `json:"integer"`
    35  	Boolean bool   `json:"boolean"`
    36  }
    37  
    38  // ////////////////////////////////////////////////////////////////////////////////// //
    39  
    40  func Test(t *testing.T) { TestingT(t) }
    41  
    42  type JSONSuite struct {
    43  	TmpDir string
    44  }
    45  
    46  // ////////////////////////////////////////////////////////////////////////////////// //
    47  
    48  var _ = Suite(&JSONSuite{})
    49  
    50  // ////////////////////////////////////////////////////////////////////////////////// //
    51  
    52  type ErrReaderWriter struct{}
    53  
    54  func (e *ErrReaderWriter) Read(p []byte) (n int, err error) {
    55  	return 0, errors.New("ERROR")
    56  }
    57  
    58  func (e *ErrReaderWriter) Write(p []byte) (n int, err error) {
    59  	return 0, nil
    60  }
    61  
    62  // ////////////////////////////////////////////////////////////////////////////////// //
    63  
    64  func (s *JSONSuite) SetUpSuite(c *C) {
    65  	s.TmpDir = c.MkDir()
    66  }
    67  
    68  func (s *JSONSuite) TestDecoding(c *C) {
    69  	jsonFile := s.TmpDir + "/file1.json"
    70  
    71  	err := ioutil.WriteFile(jsonFile, []byte(_JSON_DATA), 0644)
    72  
    73  	c.Assert(err, IsNil)
    74  
    75  	testStruct := &TestStruct{}
    76  
    77  	err = Read(s.TmpDir+"/file-not-exists.json", &TestStruct{})
    78  
    79  	c.Assert(err, NotNil)
    80  
    81  	err = Read(s.TmpDir+"/file1.json", testStruct)
    82  
    83  	c.Assert(err, IsNil)
    84  	c.Assert(testStruct.String, Equals, "test")
    85  	c.Assert(testStruct.Integer, Equals, 912)
    86  	c.Assert(testStruct.Boolean, Equals, true)
    87  }
    88  
    89  func (s *JSONSuite) TestEncoding(c *C) {
    90  	jsonFile := s.TmpDir + "/file2.json"
    91  
    92  	testStruct := &TestStruct{
    93  		String:  "test",
    94  		Integer: 912,
    95  		Boolean: true,
    96  	}
    97  
    98  	err := Write(jsonFile, testStruct, 0640)
    99  
   100  	c.Assert(err, IsNil)
   101  	c.Assert(fsutil.GetMode(jsonFile), Equals, os.FileMode(0640))
   102  
   103  	data, err := ioutil.ReadFile(jsonFile)
   104  
   105  	c.Assert(err, IsNil)
   106  	c.Assert(string(data), Equals, _JSON_DATA)
   107  	c.Assert(string(data), Equals, _JSON_DATA)
   108  
   109  	err = Write("/test.json", testStruct)
   110  
   111  	c.Assert(err, NotNil)
   112  
   113  	err = Write(jsonFile, map[float64]int{3.14: 123})
   114  
   115  	c.Assert(err, NotNil)
   116  }
   117  
   118  func (s *JSONSuite) TestCompression(c *C) {
   119  	jsonFile := s.TmpDir + "/file3.gz"
   120  
   121  	testStruct := &TestStruct{
   122  		String:  "test",
   123  		Integer: 912,
   124  		Boolean: true,
   125  	}
   126  
   127  	err := WriteGz(jsonFile, testStruct)
   128  
   129  	c.Assert(err, IsNil)
   130  	c.Assert(fsutil.IsNonEmpty(jsonFile), Equals, true)
   131  
   132  	testStructDec := &TestStruct{}
   133  
   134  	err = ReadGz(jsonFile, testStructDec)
   135  
   136  	c.Assert(err, IsNil)
   137  	c.Assert(testStruct.String, Equals, testStructDec.String)
   138  	c.Assert(testStruct.Integer, Equals, testStructDec.Integer)
   139  	c.Assert(testStruct.Boolean, Equals, testStructDec.Boolean)
   140  }
   141  
   142  func (s *JSONSuite) TestAux(c *C) {
   143  	erw := &ErrReaderWriter{}
   144  
   145  	err := readData(erw, nil, true)
   146  	c.Assert(err, NotNil)
   147  
   148  	GzipLevel = -15
   149  	err = writeData(erw, nil, true)
   150  	c.Assert(err, NotNil)
   151  	GzipLevel = 1
   152  }