code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/helpers_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package gql
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestSafeStringUint64(t *testing.T) {
    26  	convTests := []struct {
    27  		in          string
    28  		out         uint64
    29  		expectError bool
    30  	}{
    31  		{"-1", 0, true},
    32  		{"-9223372036854775808", 0, true},
    33  		{"x';INSERT INTO users ('email','passwd') VALUES ('ned@fladers.org','hello');--", 0, true},
    34  		{"0", 0, false},
    35  		{"100", 100, false},
    36  		{"9223372036854775807", 9223372036854775807, false},
    37  		{"18446744073709551615", 18446744073709551615, false},
    38  	}
    39  
    40  	for _, tt := range convTests {
    41  		c, err := safeStringUint64(tt.in)
    42  
    43  		assert.Equal(t, tt.out, c)
    44  
    45  		if tt.expectError {
    46  			assert.NotNil(t, err)
    47  		} else {
    48  			assert.Nil(t, err)
    49  		}
    50  	}
    51  }
    52  
    53  func TestSecondsTSToDatetime(t *testing.T) {
    54  	aTime := "2020-05-30T00:00:00Z"
    55  	testTime, err := time.Parse(time.RFC3339Nano, aTime)
    56  	assert.NoError(t, err)
    57  
    58  	stringified := secondsTSToDatetime(testTime.Unix())
    59  	assert.EqualValues(t, aTime, stringified)
    60  
    61  	badValue := secondsTSToDatetime(testTime.UnixNano())
    62  	assert.NotEqual(t, aTime, badValue)
    63  }
    64  
    65  func TestNanoTSToDatetime(t *testing.T) {
    66  	aTime := "2020-05-30T00:00:00Z"
    67  	testTime, err := time.Parse(time.RFC3339Nano, aTime)
    68  	assert.NoError(t, err)
    69  
    70  	stringified := nanoTSToDatetime(testTime.UnixNano())
    71  	assert.EqualValues(t, aTime, stringified)
    72  
    73  	badValue := nanoTSToDatetime(testTime.Unix())
    74  	assert.NotEqual(t, aTime, badValue)
    75  }