Skip to content

Commit

Permalink
Add create for database where it doesn't exist (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiefdhurst authored Aug 28, 2024
2 parents 8e20c1a + 18a9302 commit cd787f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/adapter/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package json
import "testing"

type TestResponse struct {
ID int `json:"id"`
Slug string `json:"slug"`
Title string `json:"title"`
Date string `json:"date"`
Content int `json:"content"`
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}

const testURL = "https://journal.jamiehurst.co.uk/api/v1/post/welcome-back"
const testURL = "https://jsonplaceholder.typicode.com/todos/1"

func TestGet(t *testing.T) {
response := &TestResponse{}
Expand All @@ -19,7 +18,7 @@ func TestGet(t *testing.T) {
if err != nil {
t.Error("Expected no error from test API call")
}
if response.ID != 1 && response.Title != "Welcome Back" {
if response.ID != 1 && response.Title != "delectus aut autem" {
t.Error("Expected result from JSON decode was not achieved")
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"database/sql"
"os"

"github.com/jamiefdhurst/journal/pkg/database/rows"
_ "github.com/mattn/go-sqlite3" // SQLite 3 driver
Expand All @@ -28,6 +29,13 @@ func (s *Sqlite) Close() {

// Connect Connect/open the database
func (s *Sqlite) Connect(dbFile string) error {
if _, err := os.Stat(dbFile); err != nil {
file, err := os.Create(dbFile)
if err != nil {
return err
}
file.Close()
}
s.db, _ = sql.Open("sqlite3", dbFile)
return s.db.Ping()
}
Expand Down

0 comments on commit cd787f6

Please sign in to comment.