-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
410ee99
commit 57d6510
Showing
11 changed files
with
155 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
db_path=data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
data/ | ||
data/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package format | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func generateRandomHeader() (int64, int32, int32) { | ||
return rand.Int63(), rand.Int31(), rand.Int31() | ||
} | ||
|
||
func TestEncodeAndDecodeHeader(t *testing.T) { | ||
timestamp, key_size, value_size := generateRandomHeader() | ||
encodedHeader := encodeHeader(timestamp, key_size, value_size) | ||
d_timestamp, d_key_size, d_value_size := DecodeHeader(encodedHeader.Bytes()) | ||
assert.Equal(t, timestamp, d_timestamp, "Timestamps are not equal!") | ||
assert.Equal(t, key_size, d_key_size, "Key sizes are not equal!") | ||
assert.Equal(t, value_size, d_value_size, "Value sizes are not equal!") | ||
} | ||
|
||
func TestEncodeAndDecodeKeyValue(t *testing.T) { | ||
timestamp := int64(rand.Int63()) | ||
key := "name" | ||
value := "abeshek" | ||
_, buf := EncodeKeyValue(timestamp, key, value) | ||
d_timestamp, d_key, d_value := DecodeKeyValue(buf) | ||
assert.Equal(t, timestamp, d_timestamp, "Timestamps are not equal!") | ||
assert.Equal(t, key, d_key, "Keys are not equal!") | ||
assert.Equal(t, value, d_value, "Values are not equal!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
module github.com/abesheknarayan/go-caskdb | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/joho/godotenv v1.4.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.7.1 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= | ||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go test ./format ./stores |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package stores | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var db *DiskStore | ||
var tempDir string // for storing db | ||
|
||
func TestGet(t *testing.T) { | ||
value := "pro tester" | ||
db.Set("name", value) | ||
assert.Equal(t, value, db.Get("name"), "Values are not equal!!") | ||
} | ||
|
||
func TestInvalidKey(t *testing.T) { | ||
// subject to change in future | ||
assert.Equal(t, "", db.Get("random_key")) | ||
} | ||
|
||
func TestPersistance(t *testing.T) { | ||
db.Set("football", "cr7") | ||
db.CloseDB() | ||
var err error | ||
db, err = InitDb("testdb", tempDir) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
assert.Equal(t, "cr7", db.Get("football"), "Persistance failure!") | ||
} | ||
|
||
func TestDbCleaup(t *testing.T) { | ||
db.Set("name", "God") | ||
db.CloseDB() | ||
db.Cleanup() | ||
assert.Equal(t, "", db.Get("name"), "Expected empty value") | ||
} | ||
|
||
func setupTests(t *testing.T) { | ||
fmt.Println("running setup") | ||
tempDir = t.TempDir() | ||
fmt.Println(tempDir) | ||
var err error | ||
db, err = InitDb("testdb", tempDir) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
func cleanupTests() { | ||
log.Println("Cleaning up tests") | ||
db.CloseDB() | ||
db.Cleanup() | ||
} | ||
|
||
// all setup is done here as this runs first, call all tests from here | ||
func TestMain(m *testing.M) { | ||
setupTests(&testing.T{}) | ||
exit := m.Run() | ||
cleanupTests() | ||
os.Exit(exit) | ||
} |