github.com/bhojpur/cache@v0.0.4/pkg/file/database/methods_test.go (about)

     1  package database
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/bhojpur/cache/pkg/file/types"
    27  )
    28  
    29  func TestPutFile(t *testing.T) {
    30  	filedb, err := Open("myfiledb", FILEDB)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer filedb.Close()
    35  
    36  	file, err := types.NewFile("test_file.txt")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	fid, err := filedb.PutItem(*file)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	t.Logf("fileID: %s\n", fid.String())
    47  }
    48  
    49  func TestGetFile(t *testing.T) {
    50  
    51  	/* -- PUT -- */
    52  
    53  	filedb, err := Open("myfiledb", FILEDB)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	defer filedb.Close()
    58  
    59  	file, err := types.NewFile("test_file.txt")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	fid, err := filedb.PutItem(*file)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	t.Logf("fileID: %s\n", fid.String())
    70  
    71  	/* -- GET -- */
    72  
    73  	readFile, err := filedb.GetItem(fid)
    74  	if err != nil {
    75  		t.Logf("%v", file)
    76  		t.Fatal(err)
    77  	}
    78  
    79  	v := readFile.(*types.File)
    80  	t.Logf("file '%s': %v\n", fid.String(), v)
    81  }
    82  
    83  func TestPutShard(t *testing.T) {
    84  	sharddb, err := Open("mynodesharddb", NSHARDDB)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	defer sharddb.Close()
    89  
    90  	shard, err := types.NewShard([]byte("I represent some bytes in a shard"))
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	sid, err := sharddb.PutItem(*shard)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	t.Logf("shardID: %s\n", sid.String())
   101  }
   102  
   103  func TestGetShard(t *testing.T) {
   104  	sharddb, err := Open("mynodesharddb", NSHARDDB)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	defer sharddb.Close()
   109  
   110  	sid, err := IDFromString("f910670bbb0012b2eb6c4f321a02251d2f38c97a8c28acdda16bb0a3b79c1ab5")
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	shard, err := sharddb.GetItem(sid)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	v := shard.(*types.Shard)
   121  	t.Logf("shard '%s': %v\n", sid, v)
   122  }