Skip to content

Commit

Permalink
test(db): add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
eng618 committed Dec 19, 2023
1 parent 07dc9f8 commit 4386b20
Showing 1 changed file with 78 additions and 5 deletions.
83 changes: 78 additions & 5 deletions interview/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package db_test

import (
"fmt"
"reflect"
"testing"
"time"

"github.com/eng618/go-eng/interview/db"
Expand All @@ -10,21 +12,92 @@ import (
func Example() {
db := db.NewDatabase()

db.Set("foo", "bar")
t := db.Set("foo", "bar")
fmt.Println("just set foo to:", db.Get("foo"))

db.Set("foo", "baz")
fmt.Println("the latest foo is:", db.Get("foo"))

// DB can set ints also
// You can use the common ok idiom to get a key with a particular time stamp.
if val, ok := db.GetForTime("foo", t); ok {
fmt.Println("the foo at", t, ":", val)
}

db.Set("age", 36)
age := db.Get("age")
fmt.Println("the age in database is:", age)

// You can use the common ok idiom to get a key with a particular time stamp.
if val, ok := db.GetForTime("foo", time.Now()); ok {
fmt.Println(val)
db.Print()
}

func getTestDatabase() *db.InMemDB {
db := db.NewDatabase()
db.Set("foo", "bar")
db.Set("hello", "world")
return db
}

func TestNewDatabase(t *testing.T) {
if got := db.NewDatabase(); got == nil {
t.Errorf("NewDatabase() = %v, wanted not nil", got)
}
}

func TestInMemDB_Set(t *testing.T) {
db := getTestDatabase()
if got := db.Get("foo"); got != "bar" {
t.Error("Failed to get foo")
}
}

func TestInMemDB_Set_multipleValuesForKey(t *testing.T) {
db := getTestDatabase()
if got := db.Get("foo"); got != "bar" {
t.Error("Failed to get foo")
}
}

func TestInMemDB_Get_basic(t *testing.T) {
db := getTestDatabase()
// foo originally set in getTestDatabase(), set an additional value
db.Set("foo", "baz")
if got := db.Get("foo"); got != "baz" {
t.Error("Failed to get foo")
}
}

func TestInMemDB_Get_noValue(t *testing.T) {
db := getTestDatabase()
if got := db.Get("none"); got != nil {
t.Error("Failed to get foo")
}
}

func TestInMemDB_GetForTime(t *testing.T) {
db := getTestDatabase()
setTime := db.Set("test", "testValue")
defaultTimeType := reflect.ValueOf(time.Now())
setTimeType := reflect.ValueOf(setTime)

if defaultTimeType != setTimeType {
t.Error("Failed, did not get a set time.")
}

if v, ok := db.GetForTime("test", setTime); ok {
if v != "testValue" {
t.Error("Failed, returned the wrong value.")
}
}
}

func TestInMemDB_GetForTime_noValue(t *testing.T) {
db := getTestDatabase()
if v, ok := db.GetForTime("foo", time.Now()); ok && v != nil {
t.Error("this should be nil")
}
}

func TestInMemDB_Print(t *testing.T) {

Check warning on line 100 in interview/db/db_test.go

View workflow job for this annotation

GitHub Actions / Go 1.20 test

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 100 in interview/db/db_test.go

View workflow job for this annotation

GitHub Actions / Go 1.21 test

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
db := getTestDatabase()
db.Print()
}

0 comments on commit 4386b20

Please sign in to comment.