github.com/m3db/m3@v1.5.0/src/x/net/http/convert_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package xhttp
    22  
    23  import (
    24  	"reflect"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestDurationToNanosBytes(t *testing.T) {
    32  	type ret struct {
    33  		output    string
    34  		shouldErr bool
    35  	}
    36  	testCases := map[string]ret{
    37  		`{"field":"value"}`:                                            ret{`{"field":"value"}`, false},
    38  		`{"fieldDuration":"1s"}`:                                       ret{`{"fieldNanos":1000000000}`, false},
    39  		`{"fieldDuration":1234}`:                                       ret{`{"fieldNanos":1234}`, false},
    40  		`{"field":"value","fieldDuration":"1s"}`:                       ret{`{"field":"value","fieldNanos":1000000000}`, false},
    41  		`{"realDuration":"50ns","nanoDuration":100,"normalNanos":200}`: ret{`{"nanoNanos":100,"normalNanos":200,"realNanos":50}`, false},
    42  		`{"field":"value","moreFields":{"innerDuration":"2ms","innerField":"innerValue"}}`: ret{`{"field":"value","moreFields":{"innerField":"innerValue","innerNanos":2000000}}`, false},
    43  		`{"field":[{"attrs":{"fieldDuration":"0s"}}]}`:                                     ret{`{"field":[{"attrs":{"fieldNanos":0}}]}`, false},
    44  		`not json`:                                       ret{"", true},
    45  		`{"fieldDuration":[]}`:                           ret{"", true},
    46  		`{"fieldDuration":{}}`:                           ret{"", true},
    47  		`{"fieldDuration":"badDuration"}`:                ret{"", true},
    48  		`{"fieldDuration":100.5}`:                        ret{"", true},
    49  		`{"moreFields":{"innerDuration":"badDuration"}}`: ret{"", true},
    50  	}
    51  
    52  	for k, v := range testCases {
    53  		output, err := DurationToNanosBytes(strings.NewReader(k))
    54  		if v.shouldErr {
    55  			assert.Error(t, err)
    56  		} else {
    57  			assert.NoError(t, err)
    58  		}
    59  
    60  		if output != nil {
    61  			assert.Equal(t, v.output, string(output))
    62  		}
    63  	}
    64  }
    65  
    66  func TestNanoToDurationBytes(t *testing.T) {
    67  	type ret struct {
    68  		output    map[string]interface{}
    69  		shouldErr bool
    70  	}
    71  	testCases := map[string]ret{
    72  		`{"field":"value"}`:                                         ret{map[string]interface{}{"field": "value"}, false},
    73  		`{"fieldNanos":1000000000}`:                                 ret{map[string]interface{}{"fieldDuration": "1s"}, false},
    74  		`{"fieldNanos":0}`:                                          ret{map[string]interface{}{"fieldDuration": "0s"}, false},
    75  		`{"field":"value","fieldNanos":1000000000}`:                 ret{map[string]interface{}{"field": "value", "fieldDuration": "1s"}, false},
    76  		`{"realNanos":50,"nanoNanos":100,"normalDuration":"200ns"}`: ret{map[string]interface{}{"nanoDuration": "100ns", "normalDuration": "200ns", "realDuration": "50ns"}, false},
    77  		`{"field":"value","moreFields":{"innerNanos":2000000,"innerField":"innerValue"}}`: ret{map[string]interface{}{"field": "value", "moreFields": map[string]interface{}{"innerField": "innerValue", "innerDuration": "2ms"}}, false},
    78  		`{"field":[{"attrs":{"fieldNanos":0}}]}`:                                          ret{map[string]interface{}{"field": []interface{}{map[string]interface{}{"attrs": map[string]interface{}{"fieldDuration": "0s"}}}}, false},
    79  		`not json`:                                                                        ret{nil, true},
    80  		`{"fieldNanos":[]}`:                                                               ret{nil, true},
    81  		`{"fieldNanos":{}}`:                                                               ret{nil, true},
    82  		`{"fieldNanos":"badNanos"}`:                                                       ret{nil, true},
    83  		`{"fieldNanos":100.5}`:                                                            ret{nil, true},
    84  		`{"moreFields":{"innerNanos":"badNanos"}}`:                                        ret{nil, true},
    85  	}
    86  
    87  	for k, v := range testCases {
    88  		output, err := NanosToDurationBytes(strings.NewReader(k))
    89  		if v.shouldErr {
    90  			assert.Error(t, err)
    91  		} else {
    92  			assert.NoError(t, err)
    93  		}
    94  
    95  		if output != nil {
    96  			if !reflect.DeepEqual(v.output, output) {
    97  				t.Errorf("expected = %v, actual %v", v, output)
    98  			}
    99  		}
   100  	}
   101  }