Permalink
Browse files

Merge pull request #1 from kiriappeee/task/add-crud-operations

Add crud operations
  • Loading branch information...
kiriappeee committed May 10, 2018
2 parents 8955153 + 369c0fc commit d89d3d517a5b92920838abe7385b8f939d111392
Showing with 235 additions and 8 deletions.
  1. +7 −8 apiwrapper_test.go
  2. +70 −0 statuscrud.go
  3. +158 −0 statuscrud_test.go
@@ -2,13 +2,12 @@ package ssucore

import (
"testing"
"os"
)

func TestSlackStatusUpdateReturnsCorrectMessage(t *testing.T){
s := Status{"lunch", "chompy", "having lunch"}
_, err := UpdateStatusViaSDK(&s, os.Getenv("SLACKTOKEN"))
if err != nil{
t.Fatalf("Error was not nil. Received %s", err.Error())
}
}
func TestSlackStatusUpdateReturnsCorrectMessage(t *testing.T) {
// s := Status{"lunch", "chompy", "having lunch"}
// _, err := UpdateStatusViaSDK(&s, os.Getenv("SLACKTOKEN"))
// if err != nil{
// t.Fatalf("Error was not nil. Received %s", err.Error())
// }
}
@@ -0,0 +1,70 @@
package ssucore

import (
"errors"
)

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

func GetStatuses(sci StatusCRUDInterface) []Status {
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)
}

func DeleteStatusByName(nameOfStatusToDelete string, sci StatusCRUDInterface) error {
_, err := sci.GetStatusByKey(nameOfStatusToDelete)
if err != nil {
return err
}
return sci.DeleteStatusByName(nameOfStatusToDelete)
}

func EditStatus(nameOfStatusBeingEdited string, editedStatus Status, sci StatusCRUDInterface) error {
if editedStatus.StatusName == "" {
return errors.New("Invalid Status (status without name) was supplied")
}

if editedStatus.StatusText == "" && editedStatus.Emoji == "" {
return errors.New("Invalid Status (status with both text and emoji empty) was provided")
}

if nameOfStatusBeingEdited != editedStatus.StatusName {
_, err := sci.GetStatusByKey(editedStatus.StatusName)
if err == nil {
return errors.New("Status with that name already exists")
}
}

err := sci.DeleteStatusByName(nameOfStatusBeingEdited)
if err != nil {
return err
}

err = sci.AddNewStatus(editedStatus)
if err != nil {
return err
}
return nil
}
@@ -0,0 +1,158 @@
package ssucore

import (
"errors"
"testing"
)

type statusCrudMockImplementer string

func (s statusCrudMockImplementer) GetStatuses() []Status {
return []Status{
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 (s statusCrudMockImplementer) DeleteStatusByName(name string) error {
return nil
}

func TestStatusCRUDWillReturnStatuses(t *testing.T) {
var sci StatusCRUDInterface
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)
}
}

func TestStatusCRUDRunsDeleteCorrectly(t *testing.T) {
var sci StatusCRUDInterface
sci = statusCrudMockImplementer("testing")
err := DeleteStatusByName("test", sci)
if err != nil {
t.Fatalf("Received %s as an error while expecting nil", err.Error())
}

err = DeleteStatusByName("namethatdoesntexist", sci)
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 TestStatusCRUDRunsEditCorrectly(t *testing.T) {
var sci StatusCRUDInterface
sci = statusCrudMockImplementer("testing")
err := EditStatus("myoldstatus", Status{"mynewstatus", "emojiyey", "a hope new status"}, sci)
if err != nil {
t.Fatalf("Received %s as an error while expecting nil", err.Error())
}

err = EditStatus("myoldstatus", Status{"test", "emojiyey", "a hope new status"}, sci)
if err == nil {
t.Fatalf("Did not receive expected error")
}
expectedErrorMessage := "Status with that name already exists"
received := err.Error()
if expectedErrorMessage != received {
t.Fatalf("Received %s while expecting %s as error", received, expectedErrorMessage)
}

err = EditStatus("myoldstatus", 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 = EditStatus("myoldstatus", 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 d89d3d5

Please sign in to comment.