Permalink
Browse files

Refactored and added tests for adding new statuses

Due to a previous misunderstanding about how functions could be defined
I had named my functions poorly. I refactored these names and then
added tests to test that statuscrud.go handles adding a new status correctly.

There's some logic that's going to be duplicated in editing a status.
Once I have that logic fully written out I'll refactor it into a common
unexported function
  • Loading branch information...
kiriappeee committed May 8, 2018
1 parent a2035a0 commit db243fc67fc347d0c5714f33acd2db41acc8c48d
Showing with 109 additions and 9 deletions.
  1. +26 −2 statuscrud.go
  2. +83 −7 statuscrud_test.go
@@ -1,9 +1,33 @@
package ssucore

import (
"errors"
)

type StatusCRUDInterface interface {
List() []Status
GetStatuses() []Status
GetStatusByKey(k string) (Status, error)
AddNewStatus(status Status) error
}

func GetStatuses(sci StatusCRUDInterface) []Status {
return sci.List()
return sci.GetStatuses()
}

func GetStatusByKey(key string, sci StatusCRUDInterface) (Status, error) {
return sci.GetStatusByKey(key)
}

func AddNewStatus(status Status, sci StatusCRUDInterface) error {
if status.StatusName == "" {
return errors.New("Invalid Status (status without name) was supplied")
}
if status.StatusText == "" && status.Emoji == "" {
return errors.New("Invalid Status (status with both text and emoji empty) was provided")
}
_, err := sci.GetStatusByKey(status.StatusName)
if err == nil {
return errors.New("Status already exists")
}
return sci.AddNewStatus(status)
}
@@ -1,24 +1,100 @@
package ssucore

import (
"errors"
"testing"
)

type StatusCrudMockImplementer string
type statusCrudMockImplementer string

func (s StatusCrudMockImplementer) List() []Status {
func (s statusCrudMockImplementer) GetStatuses() []Status {
return []Status{
Status{"test", "me", "now"},
Status{"test", "me", "now"},
Status{"test", "me", "now"},
Status{"test", "me", "now"},
Status{"test", "emojiA", "Status One"},
Status{"test1", "emojiB", "Status Two"},
Status{"test2", "emojiC", "Status Three"},
Status{"test3", "emojiD", "Status Four"},
}
}

func (s statusCrudMockImplementer) GetStatusByKey(key string) (Status, error) {
if key == "test" {
return Status{"test", "emojiA", "Status One"}, nil
} else if key == "statusalreadyexisting" {
return Status{"statusalreadyexisting", "emojiExisting", "An existing status"}, nil
}
return Status{}, errors.New("Could not find status with that key")
}

func (s statusCrudMockImplementer) AddNewStatus(status Status) error {
if status.StatusName == "mynewstatus" {
return nil
}
return nil
}

func TestStatusCRUDWillReturnStatuses(t *testing.T) {
var sci StatusCRUDInterface
sci = StatusCrudMockImplementer("testing")
sci = statusCrudMockImplementer("testing")
statusList := GetStatuses(sci)
if len(statusList) != 4 {
t.Fatalf("Length of returned array was not %d. Expected 4", len(statusList))
}
}

func TestStatusCRUDRunsSelectByKey(t *testing.T) {
var sci StatusCRUDInterface
sci = statusCrudMockImplementer("testing")
status, err := GetStatusByKey("test", sci)
if err != nil {
t.Fatalf("Received an error when nil was expected")
}
if status.StatusName != "test" {
t.Fatalf("Returned status did not match expected status name. Got %s, Expected %s", status.StatusName, "test")
}
status, err = GetStatusByKey("invalidkey", sci)
if status.StatusName != "" {
t.Fatalf("Received a status while expecting nil")
}
if err == nil {
t.Fatalf("Did not receive expected error")
}
if err.Error() != "Could not find status with that key" {
t.Fatalf("Received %s as error message. Expected %s", err.Error(), "Could not find status with that key")
}
}

func TestStatusCRUDRunsAddStatusCorrectly(t *testing.T) {
var sci StatusCRUDInterface
sci = statusCrudMockImplementer("testing")
err := AddNewStatus(Status{"mynewstatus", "emojiyey", "a hope new status"}, sci)
if err != nil {
t.Fatalf("Received %s as an error while expecting nil", err.Error())
}

err = AddNewStatus(Status{"statusalreadyexisting", "emojiyey", "a hope new status"}, sci)
expectedErrorMessage := "Status already exists"
if err == nil {
t.Fatalf("Did not receive an error while expecting one")
}
if err.Error() != expectedErrorMessage {
t.Fatalf("Received %s as error while expecting %s", err.Error(), expectedErrorMessage)
}

err = AddNewStatus(Status{"", "anemoji", ""}, sci)
expectedErrorMessage = "Invalid Status (status without name) was supplied"
if err == nil {
t.Fatalf("Did not receive an error while expecting one")
}
if err.Error() != expectedErrorMessage {
t.Fatalf("Received %s as error while expecting %s", err.Error(), expectedErrorMessage)
}

err = AddNewStatus(Status{"mynewstatus", "", ""}, sci)
expectedErrorMessage = "Invalid Status (status with both text and emoji empty) was provided"
if err == nil {
t.Fatalf("Did not receive an error while expecting one")
}
if err.Error() != expectedErrorMessage {
t.Fatalf("Received %s as error while expecting %s", err.Error(), expectedErrorMessage)
}
}

0 comments on commit db243fc

Please sign in to comment.