Skip to content

Commit

Permalink
added TestOpenFileStoreUnmarshallsExistingFile; t.Fatal unexpected er…
Browse files Browse the repository at this point in the history
…rors
  • Loading branch information
crmejia committed Jun 17, 2022
1 parent 0abfebc commit cc8a5ba
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
12 changes: 6 additions & 6 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestNewController(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}

if controller.Store == nil {
Expand All @@ -33,7 +33,7 @@ func TestController_HandleReturnsErrorOnNilHabit(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}
_, err = controller.Handle(nil)
if err == nil {
Expand All @@ -46,7 +46,7 @@ func TestController_HandleReturnsErrorOnEmptyHabitName(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}
h := habit.Habit{Name: ""}
_, err = controller.Handle(&h)
Expand All @@ -65,7 +65,7 @@ func TestController_HandleUpdatesStreaksDueDateCorrectly(t *testing.T) {
}
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}
testCases := []struct {
name string
Expand All @@ -90,7 +90,7 @@ func TestController_HandleUpdatesStreaksDueDateCorrectly(t *testing.T) {

h, err := controller.Handle(&inputHabit)
if err != nil {
t.Error(err)
t.Fatal(err)
}

if h.Name != "piano" {
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestController_HandleCreatesHabit(t *testing.T) {
}

if store.Habits["piano"] == nil {
t.Error("want new habit to be inserted into store")
t.Errorf("want new habit to be inserted into store")
}
}

Expand Down
20 changes: 10 additions & 10 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func TestHandleIndexWithGibberishReturns400(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}

server, err := habit.NewServer(&controller, localHostAddress)
if err != nil {
t.Error(err)
t.Fatal(err)
}
handler := server.HandleIndex()
handler(recorder, req)
Expand All @@ -112,7 +112,7 @@ func TestHandleIndexWithGibberishReturns400(t *testing.T) {
}
err = res.Body.Close()
if err != nil {
t.Error(err)
t.Fatal(err)
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ func TestRouting(t *testing.T) {
}
err = res.Body.Close()
if err != nil {
t.Error(err)
t.Fatal(err)
} //no defer at it might leak ;)
}
}
Expand All @@ -218,7 +218,7 @@ func TestServer_RunReturnsBadRequest(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}

freePort, err := freeport.GetFreePort()
Expand All @@ -228,7 +228,7 @@ func TestServer_RunReturnsBadRequest(t *testing.T) {
address := fmt.Sprintf("%s:%d", localHostAddress, freePort)
server, err := habit.NewServer(&controller, address)
if err != nil {
t.Error(err)
t.Fatal(err)
}
go server.Run()
resp, err := http.Get("http://" + address)
Expand All @@ -240,7 +240,7 @@ func TestServer_RunReturnsBadRequest(t *testing.T) {
t.Fatal(err)
}
if err != nil {
t.Error(err)
t.Fatal(err)
}
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Want Status %d, got: %d", http.StatusNotFound, resp.StatusCode)
Expand All @@ -260,7 +260,7 @@ func TestServer_RunReturnsHabit(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}

freePort, err := freeport.GetFreePort()
Expand All @@ -270,7 +270,7 @@ func TestServer_RunReturnsHabit(t *testing.T) {
address := fmt.Sprintf("%s:%d", localHostAddress, freePort)
server, err := habit.NewServer(&controller, address)
if err != nil {
t.Error(err)
t.Fatal(err)
}
go server.Run()
resp, err := http.Get("http://" + address + "?habit=piano")
Expand All @@ -282,7 +282,7 @@ func TestServer_RunReturnsHabit(t *testing.T) {
t.Fatal(err)
}
if err != nil {
t.Error(err)
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Want Status %d, got: %d", http.StatusNotFound, resp.StatusCode)
Expand Down
41 changes: 29 additions & 12 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestMemoryStore_GetReturnsNilOnNoHabit(t *testing.T) {
store := habit.OpenMemoryStore()
got, err := store.Get("piano")
if err != nil {
t.Error(err)
t.Fatal(err)
}

if got != nil {
Expand All @@ -27,7 +27,7 @@ func TestMemoryStore_GetReturnsExistingHabit(t *testing.T) {

h, err := store.Get("piano")
if err != nil {
t.Error(err)
t.Fatal(err)
}
if h == nil {
t.Fatal()
Expand All @@ -47,7 +47,7 @@ func TestMemoryStore_CreateNewHabit(t *testing.T) {

err := store.Create(&h)
if err != nil {
t.Error(err)
t.Fatal(err)
}

if _, ok := store.Habits["piano"]; !ok {
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestMemoryStore_UpdateHabit(t *testing.T) {
updateHabit := &habit.Habit{Name: "piano"}
err := store.Update(updateHabit)
if err != nil {
t.Error(err)
t.Fatal(err)
}

if oldHabit == store.Habits["piano"] {
Expand Down Expand Up @@ -163,13 +163,13 @@ func TestController_HandleSetsMessageCorrectlyForNewHabit(t *testing.T) {
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}
h := &habit.Habit{Name: "piano",
Frequency: habit.DailyInterval}
_, err = controller.Handle(h)
if err != nil {
t.Error(err)
t.Fatal(err)
}

want := "Good luck with your new habit 'piano'! Don't forget to do it again tomorrow."
Expand All @@ -191,14 +191,14 @@ func TestTracker_FetchHabitSetsMessageCorrectlyForStreakBrokenStreak(t *testing.
store := habit.OpenMemoryStore()
controller, err := habit.NewController(&store)
if err != nil {
t.Error(err)
t.Fatal(err)
}
for _, tc := range testCases {

store.Habits[tc.habit.Name] = tc.habit
_, err := controller.Handle(tc.habit)
if err != nil {
t.Error(err)
t.Fatal(err)
}

got := tc.habit.String()
Expand All @@ -221,7 +221,7 @@ func TestDBStore_CreateUpdateNilHabitFails(t *testing.T) {
dbSource := t.TempDir() + "test.db"
store, err := habit.OpenDBStore(dbSource)
if err != nil {
t.Error(err)
t.Fatal(err)
}

err = store.Create(nil)
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestDBStore_AllHabits(t *testing.T) {
for _, h := range habits {
err := dbStore.Create(h)
if err != nil {
t.Error(err)
t.Fatal(err)
}
}

Expand Down Expand Up @@ -356,12 +356,29 @@ func TestOpenFileStoreErrorsOnEmptyFilename(t *testing.T) {
t.Error("Want error on empty string db source")
}
}

func TestOpenFileStoreUnmarshallsExistingFile(t *testing.T) {
t.Parallel()
fileName := "testdata/.habitTracker"
store, err := habit.OpenFileStore(fileName)

if err != nil {
t.Fatal(err)
}
habits := store.GetAllHabits()
if err != nil {
t.Fatal(err)
}
if len(habits) == 0 {
t.Error("want OpenStore to load testdata/.habitTracker")
}
}
func TestFileStore_CreateUpdateNilHabitFails(t *testing.T) {
t.Parallel()
filename := t.TempDir() + ".habitTracker"
store, err := habit.OpenFileStore(filename)
if err != nil {
t.Error(err)
t.Fatal(err)
}
err = store.Create(nil)
if err == nil {
Expand Down Expand Up @@ -481,7 +498,7 @@ func TestFileStore_AllHabitsReturnsSliceOfHabits(t *testing.T) {
for _, h := range habits {
err := fileStore.Create(h)
if err != nil {
t.Error(err)
t.Fatal(err)
}
}

Expand Down

0 comments on commit cc8a5ba

Please sign in to comment.