github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/meta/meta_test.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package meta
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/vescale/zgraph/storage"
    23  	"github.com/vescale/zgraph/storage/kv"
    24  )
    25  
    26  func TestMeta_GlobalID(t *testing.T) {
    27  	assert := assert.New(t)
    28  	store, err := storage.Open(t.TempDir())
    29  	assert.Nil(err)
    30  
    31  	err = kv.TxnContext(context.Background(), store, func(_ context.Context, txn kv.Transaction) error {
    32  		meta := New(txn)
    33  		id, err := meta.GlobalID()
    34  		assert.Nil(err)
    35  		assert.Zero(id)
    36  		return nil
    37  	})
    38  	assert.Nil(err)
    39  
    40  	err = kv.TxnContext(context.Background(), store, func(_ context.Context, txn kv.Transaction) error {
    41  		meta := New(txn)
    42  		id, err := meta.NextGlobalID()
    43  		assert.Nil(err)
    44  		assert.Equal(int64(1), id)
    45  		return nil
    46  	})
    47  	assert.Nil(err)
    48  
    49  	err = kv.TxnContext(context.Background(), store, func(_ context.Context, txn kv.Transaction) error {
    50  		meta := New(txn)
    51  		id, err := meta.AdvanceGlobalID(10)
    52  		assert.Nil(err)
    53  		assert.Equal(int64(1), id)
    54  
    55  		id, err = meta.GlobalID()
    56  		assert.Nil(err)
    57  		assert.Equal(int64(11), id)
    58  		return nil
    59  	})
    60  	assert.Nil(err)
    61  
    62  	err = kv.TxnContext(context.Background(), store, func(_ context.Context, txn kv.Transaction) error {
    63  		meta := New(txn)
    64  		ids, err := meta.GenGlobalIDs(3)
    65  		assert.Nil(err)
    66  		assert.Equal([]int64{12, 13, 14}, ids)
    67  		return nil
    68  	})
    69  	assert.Nil(err)
    70  }