code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/snapshot/database_meta_data_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 snapshot
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestExtractIntervalFromViewDefinition(t *testing.T) {
    26  	viewDefinition := ` SELECT balances.account_id,
    27  	time_bucket('01:00:00'::interval, balances.vega_time) AS bucket,
    28  		last(balances.balance, balances.vega_time) AS balance,
    29  		last(balances.tx_hash, balances.vega_time) AS tx_hash,
    30  		last(balances.vega_time, balances.vega_time) AS vega_time
    31  	FROM balances
    32  	GROUP BY balances.account_id, (time_bucket('01:00:00'::interval, balances.vega_time));`
    33  
    34  	interval, err := extractIntervalFromViewDefinition(viewDefinition)
    35  	require.NoError(t, err)
    36  	assert.Equal(t, "01:00:00", interval)
    37  
    38  	viewDefinition = ` SELECT balances.account_id,
    39  	time_bucket('1 day'::interval, balances.vega_time) AS bucket,
    40  		last(balances.balance, balances.vega_time) AS balance,
    41  		last(balances.tx_hash, balances.vega_time) AS tx_hash,
    42  		last(balances.vega_time, balances.vega_time) AS vega_time
    43  	FROM balances
    44  	GROUP BY balances.account_id, (time_bucket('1 day'::interval, balances.vega_time));`
    45  
    46  	interval, err = extractIntervalFromViewDefinition(viewDefinition)
    47  	require.NoError(t, err)
    48  	assert.Equal(t, "1 day", interval)
    49  }