Skip to content

Commit

Permalink
projects: added projects list feature description & test implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 15, 2019
1 parent d96608f commit e7606a9
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 93 deletions.
91 changes: 0 additions & 91 deletions tests/config_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions tests/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func Executor(buildDirectory string) *Config {
}
}

func (c *Config) Execute(args ...string) ([]byte, error) {
cmd := exec.Command(c.ExecFullPath, args...)
func (c *Config) Execute(args string) ([]byte, error) {
subCmd := strings.Split(args, " ")
cmd := exec.Command(c.ExecFullPath, subCmd...)
cmd.Dir = c.ExecDirectory
return cmd.Output()
}
Expand Down
150 changes: 150 additions & 0 deletions tests/feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package tests_test

import (
"errors"
"flag"
"fmt"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/colors"
"github.com/DATA-DOG/godog/gherkin"
"github.com/leanovate/mite-go/tests/executor"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)

const buildDirectory = "../"

func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
}

func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()

status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)

if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}

func FeatureContext(s *godog.Suite) {
c := cmdTest{
executor: executor.Executor(buildDirectory),
}

s.AfterScenario(c.reset)
s.AfterSuite(func() {
err := c.executor.Clean()
if err != nil {
panic(err)
}
})
s.Step(`^an empty config file called "([^"]*)"$`, c.anEmptyConfigFileCalled)
s.Step(`^I execute "([^"]*)"$`, c.iExecute)
s.Step(`^"([^"]*)" should return "([^"]*)"$`, c.shouldReturn)
s.Step(`^A local mock server is setup for the http method "([^"]*)" and path "([^"]*)" which returns:$`, c.aLocalMockServerIsSetupForTheHttpMethodAndPathWhichReturns)
s.Step(`^Mite is setup to connect to this mock server$`, c.miteIsSetupToConnectToThisMockServer)
s.Step(`^"([^"]*)" should return the following:$`, c.shouldReturnTheFollowing)
}

var opt = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
}

type cmdTest struct {
executor *executor.Config
mockServer *httptest.Server
}

func (c *cmdTest) reset(interface{}, error) {
err := c.executor.Clean()
if err != nil {
panic(err)
}
c.executor = executor.Executor(buildDirectory)

if c.mockServer != nil {
c.mockServer.Close()
}
}

func (c *cmdTest) anEmptyConfigFileCalled(arg1 string) error {
_, err := os.Stat(filepath.Join(c.executor.ExecDirectory, arg1))
if os.IsNotExist(err) {
return nil
}
return err
}

func (c *cmdTest) iExecute(subCommand string) error {
output, err := c.executor.Execute(subCommand)
fmt.Println(string(output))
return err
}

func (c *cmdTest) shouldReturn(subCommand, output string) error {
stdout, err := c.executor.Execute(subCommand)
if err != nil {
return err
}
outputWithoutSpace := strings.TrimSpace(string(stdout))
return assertEqual(output, outputWithoutSpace)
}

func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichReturns(arg1, arg2 string, arg3 *gherkin.DocString) error {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != arg1 {
w.WriteHeader(400)
}
if r.URL.Path != arg2 {
w.WriteHeader(400)
}

w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err := w.Write([]byte(arg3.Content))
if err != nil {
panic(err)
}
}
handlerFunc := http.HandlerFunc(handler)
c.mockServer = httptest.NewServer(handlerFunc)
return nil
}

func (c *cmdTest) miteIsSetupToConnectToThisMockServer() error {
err := c.iExecute("-c .mite.toml config api.key=foo")
if err != nil {
return err
}
err = c.iExecute(fmt.Sprintf("-c .mite.toml config api.url=%s", c.mockServer.URL))
if err != nil {
return err
}
return nil
}

func (c *cmdTest) shouldReturnTheFollowing(arg1 string, arg2 *gherkin.DocString) error {
actualOutput, err := c.executor.Execute(arg1)
if err != nil {
return err
}
return assertEqual(arg2.Content, string(actualOutput))
}

func assertEqual(expected, actual string) error {
if strings.Compare(expected, actual) != 0 {
return errors.New(fmt.Sprintf("expected: \n%s\n to equal: \n%s", expected, actual))
}
return nil
}
45 changes: 45 additions & 0 deletions tests/features/projects.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Feature: projects
In order to use mite in a sane manner
As a mite user
I need to be able to list projects

Scenario: list projects
Given A local mock server is setup for the http method "GET" and path "/projects.json" which returns:
"""
[
{
"project": {
"id": 643,
"name": "Open-Source",
"note": "valvat, memento et all.",
"customer_id": 291,
"customer_name": "Yolk",
"budget": 0,
"budget_type": "minutes",
"hourly_rate": 6000,
"archived": false,
"active_hourly_rate": "hourly_rate",
"hourly_rates_per_service": [
{
"service_id": 31272,
"hourly_rate": 4500
},
{
"service_id": 149228,
"hourly_rate": 5500
}
],
"created_at": "2011-08-17T12:06:57+02:00",
"updated_at": "2015-02-19T10:53:10+01:00"
}
}
]
"""
And Mite is setup to connect to this mock server
Then "-c .mite.toml projects" should return the following:
"""
id name notes
-- ---- -----
643 Open-Source valvat, memento et all.
"""

0 comments on commit e7606a9

Please sign in to comment.