Permalink
Browse files

Added new crud method implementations

The following CRUD method implementations were added:

* GetStatusByKey
* AddNewStatus
* DeleteStatusByName

I have to add a test in the next commit to ensure that Editing statuses
uses the above implementations correctly, but with this commit, the work
to implement the interface seems done.
  • Loading branch information...
kiriappeee committed May 14, 2018
1 parent aaa476b commit 5166102ef3d7d3874f0b9ea36d0b45050eb0e36d
Showing with 105 additions and 4 deletions.
  1. +44 −4 statuscrud_file.go
  2. +61 −0 statuscrud_file_test.go
@@ -1,7 +1,11 @@
package main

import (
"errors"
"github.com/kiriappeee/slack-status-updater-core"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
)

type statusCRUDFileImplementation string
@@ -15,16 +19,52 @@ func (s statusCRUDFileImplementation) GetStatuses() []ssucore.Status {
}

func (s statusCRUDFileImplementation) GetStatusByKey(name string) (ssucore.Status, error) {
//stub
return ssucore.Status{}, nil
if name != "" {
statuses := s.GetStatuses()
for i := 0; i < len(statuses); i++ {
if statuses[i].StatusName == name {
return statuses[i], nil
}
}
return ssucore.Status{}, errors.New("Could not find the requested status")
}
return ssucore.Status{}, errors.New("Name of status to search for cannot be empty")
}

func (s statusCRUDFileImplementation) AddNewStatus(status ssucore.Status) error {
//stub
if s!= "" {
statuses := s.GetStatuses()
statuses = append(statuses, status)
log.Println(statuses)
data , err := yaml.Marshal(statuses)
if err != nil {
return err
}
err = ioutil.WriteFile(string(s), data, 0644)
if err != nil {
return err
}
}
return nil
}

func (s statusCRUDFileImplementation) DeleteStatusByName(name string) error {
//stub
if s!= "" {
statuses := s.GetStatuses()
var newStatusList []ssucore.Status
for _, status := range(statuses) {
if status.StatusName != name {
newStatusList = append(newStatusList, status)
}
}
data , err := yaml.Marshal(newStatusList)
if err != nil {
return err
}
err = ioutil.WriteFile(string(s), data, 0644)
if err != nil {
return err
}
}
return nil
}
@@ -16,4 +16,65 @@ func TestStatusesCanBeListed(t *testing.T) {
if len(statuses) != 4 {
t.Fatalf("Did not receive the expected statuses")
}
fileTearDown()
}

func TestStatusCanBeRetrievedByKey(t *testing.T) {
fileSetUp()
var sci ssucore.StatusCRUDInterface
homeDirectory := os.Getenv("HOME")
sci = statusCRUDFileImplementation(homeDirectory + "/.config/ssuclitest/statuses.yaml")
status, err := ssucore.GetStatusByKey("resting", sci)
if err != nil {
t.Fatalf("Received an error where non was expected. Received error was %s", err.Error())
}
if status.StatusName != "resting"{
t.Fatalf("Did not received the expected status. Received %s", status)
}

status, err = ssucore.GetStatusByKey("notresting", sci)
if err == nil {
t.Fatalf("Did not receive an error when expecting one")
}
expectedError := "Could not find the requested status"
receivedError := err.Error()

if expectedError != receivedError {
t.Fatalf("Did not receive the expected error. Received %s. Expected %s", receivedError, expectedError)
}
fileTearDown()
}

func TestStatusCanBeAdded(t *testing.T) {
fileSetUp()
var sci ssucore.StatusCRUDInterface
homeDirectory := os.Getenv("HOME")
sci = statusCRUDFileImplementation(homeDirectory + "/.config/ssuclitest/statuses.yaml")
goodStatusToAdd := ssucore.Status{"mynewstatus", "emoji", "Status text"}
err := ssucore.AddNewStatus(goodStatusToAdd, sci)
if err != nil {
t.Fatalf("Received an error where non was expected. Received error was: %s", err.Error())
}
status, _ := ssucore.GetStatusByKey("mynewstatus", sci)
if status.StatusName != "mynewstatus" && status.Emoji != "emoji" && status.StatusText != "Status text" {
t.Fatalf("Status retrieved after adding did not match expected values. Received %s", status)
}
fileTearDown()
}

func TestStatusCanBeDeleted(t *testing.T) {
fileSetUp()
var sci ssucore.StatusCRUDInterface
homeDirectory := os.Getenv("HOME")
sci = statusCRUDFileImplementation(homeDirectory + "/.config/ssuclitest/statuses.yaml")
err := ssucore.DeleteStatusByName("resting", sci)
if err != nil {
t.Fatalf("Received an error where non was expected. Received error was: %s", err.Error())
}

_, err = ssucore.GetStatusByKey("resting", sci)
if err == nil {
t.Fatalf("Received no error. Was expecting: Could not find the requested status")
}
fileTearDown()
}

0 comments on commit 5166102

Please sign in to comment.