-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made a database package and improved tests
- Loading branch information
1 parent
16358e3
commit 2fb4b20
Showing
11 changed files
with
135 additions
and
54 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
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,89 @@ | ||
package database | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// NewTempDir will return a new temp directory for testing | ||
func NewTempDir(t testing.TB) string { | ||
tempDir, err := ioutil.TempDir("", "") | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
t.FailNow() | ||
} | ||
|
||
t.Cleanup(func() { | ||
os.RemoveAll(tempDir) | ||
}) | ||
|
||
return tempDir | ||
} | ||
|
||
func TestOpenDatabase(t *testing.T) { | ||
t.Run("Simple", func(t *testing.T) { | ||
tempDir := NewTempDir(t) | ||
db, err := OpenDatabase(filepath.Join(tempDir, "test.db")) | ||
require.NoError(t, err) | ||
require.NotNil(t, db) | ||
}) | ||
|
||
t.Run("NoFile", func(t *testing.T) { | ||
db, err := OpenDatabase("") | ||
require.NoError(t, err) | ||
require.NotNil(t, db) | ||
require.IsType(t, &StubDatabase{}, db) | ||
}) | ||
|
||
t.Run("NonexistantPathIsCreated", func(t *testing.T) { | ||
tempDir := NewTempDir(t) | ||
db, err := OpenDatabase(filepath.Join(tempDir, "nonexistdir", "test.db")) | ||
require.NoError(t, err) | ||
require.NotNil(t, db) | ||
}) | ||
|
||
t.Run("BadPermissions", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("Windows does not have the same kind of file permissions") | ||
} | ||
tempDir := NewTempDir(t) | ||
err := os.MkdirAll(filepath.Join(tempDir, "badperms"), 0666) | ||
require.NoError(t, err) | ||
db, err := OpenDatabase(filepath.Join(tempDir, "badperms", "nonexistdir", "test.db")) | ||
require.Error(t, err) | ||
require.Nil(t, db) | ||
}) | ||
|
||
t.Run("ExecuteOnlyPermissions", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("Windows does not have the same kind of file permissions") | ||
} | ||
tempDir := NewTempDir(t) | ||
err := os.MkdirAll(filepath.Join(tempDir, "badperms"), 0111) | ||
require.NoError(t, err) | ||
db, err := OpenDatabase(filepath.Join(tempDir, "badperms", "nonexistdir", "test.db")) | ||
require.Error(t, err) | ||
require.Nil(t, db) | ||
}) | ||
|
||
} | ||
|
||
func TestStubDatabase(t *testing.T) { | ||
stubDatabase := NewStubDatabase() | ||
err := stubDatabase.Close() | ||
require.NoError(t, err) | ||
|
||
err = stubDatabase.Sync() | ||
require.NoError(t, err) | ||
|
||
err = stubDatabase.Update(nil) | ||
require.NoError(t, err) | ||
|
||
err = stubDatabase.View(nil) | ||
require.NoError(t, err) | ||
} |
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,29 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func resetVersion() { | ||
Version = "" | ||
GitHash = "" | ||
} | ||
|
||
func TestGetVersionWithVersion(t *testing.T) { | ||
Version = "0.1.1" | ||
defer resetVersion() | ||
require.Equal(t, Version, GetVersion()) | ||
} | ||
|
||
func TestGetVersionWithGitHash(t *testing.T) { | ||
GitHash = "git hash" | ||
defer resetVersion() | ||
require.Equal(t, GitHash, GetVersion()) | ||
} | ||
|
||
func TestGetVersionWithUnknownVersion(t *testing.T) { | ||
defer resetVersion() | ||
require.Equal(t, "unknown", GetVersion()) | ||
} |
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 was deleted.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.