github.com/klaytn/klaytn@v1.12.1/storage/database/dynamodb_readonly_test.go (about)

     1  // Copyright 2020 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  //
    17  // You need to set AWS credentials to access to dynamoDB.
    18  //    sh$ export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
    19  //    sh$ export AWS_SECRET_ACCESS_KEY=YOUR_SECRET
    20  
    21  package database
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/klaytn/klaytn/common"
    27  	"github.com/klaytn/klaytn/storage"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestDynamoDBReadOnly_Put(t *testing.T) {
    32  	storage.SkipLocalTest(t)
    33  
    34  	dynamo, err := newDynamoDBReadOnly(GetTestDynamoConfig())
    35  	defer dynamo.deleteDB()
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	t.Log("dynamoDB", dynamo.config.TableName)
    40  
    41  	testKey := common.MakeRandomBytes(32)
    42  	testVal := common.MakeRandomBytes(500)
    43  
    44  	// check if not found for not put key, value
    45  	val, err := dynamo.Get(testKey)
    46  	assert.Nil(t, val)
    47  	assert.Error(t, err)
    48  	assert.Equal(t, err.Error(), dataNotFoundErr.Error())
    49  
    50  	// put key, value
    51  	assert.NoError(t, dynamo.Put(testKey, testVal))
    52  
    53  	// check if not found for put key, value
    54  	val, err = dynamo.Get(testKey)
    55  	assert.Nil(t, val)
    56  	assert.Error(t, err)
    57  	assert.Equal(t, err.Error(), dataNotFoundErr.Error())
    58  }
    59  
    60  func TestDynamoDBReadOnly_Write(t *testing.T) {
    61  	storage.SkipLocalTest(t)
    62  
    63  	dynamo, err := newDynamoDBReadOnly(GetTestDynamoConfig())
    64  	defer dynamo.deleteDB()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	t.Log("dynamoDB", dynamo.config.TableName)
    69  
    70  	var testKeys [][]byte
    71  	var testVals [][]byte
    72  	batch := dynamo.NewBatch()
    73  
    74  	itemNum := 25
    75  	// put items
    76  	for i := 0; i < itemNum; i++ {
    77  		testKey := common.MakeRandomBytes(32)
    78  		testVal := common.MakeRandomBytes(500)
    79  
    80  		testKeys = append(testKeys, testKey)
    81  		testVals = append(testVals, testVal)
    82  
    83  		assert.NoError(t, batch.Put(testKey, testVal))
    84  	}
    85  	assert.NoError(t, batch.Write())
    86  
    87  	// check if not exist
    88  	for i := 0; i < itemNum; i++ {
    89  		val, err := dynamo.Get(testKeys[i])
    90  		assert.Nil(t, val)
    91  		assert.Error(t, err)
    92  		assert.Equal(t, err.Error(), dataNotFoundErr.Error())
    93  	}
    94  }
    95  
    96  func (dynamo *dynamoDBReadOnly) deleteDB() {
    97  	dynamo.Close()
    98  	dynamo.deleteTable()
    99  	dynamo.fdb.deleteBucket()
   100  }