Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added progress tracking for test cases. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions forecast.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"
)

var progress Level

const (
//replace with your personal information as desired
key = "32772f4b37c5a08eb4488a2ce79155bd"
Expand Down Expand Up @@ -112,6 +114,17 @@ func output(fc Forecast, forecast bool, log *logger.Logger) {
}
}

// SetMessage sets a formatted string depending on test case progress.
func (l *Level) SetMessage() {
l.Msg = "You passed level %v!"
l.MsgValues = []interface{}{l.Current}
for i := 0; i < l.Current; i++ {
l.Msg += " %c"
l.MsgValues = append(l.MsgValues, 128640)
}
l.Msg += "\n"
}

// Forecast is just the parts of the response we care about. Current and Daily
type Forecast struct {
Currently CurrentConditions
Expand Down Expand Up @@ -141,3 +154,10 @@ type WeatherDaily struct {
WindBearing float32
}
}

// Level tracks the progress of tests being passed.
type Level struct {
Current int
Msg string
MsgValues []interface{}
}
76 changes: 59 additions & 17 deletions forecast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"bytes"
assertions "github.com/stretchr/testify/assert"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"testing"

assertions "github.com/stretchr/testify/assert"
)

// Welcome to the October 2019 A2GO Workshop!
Expand Down Expand Up @@ -42,6 +44,9 @@ import (
// - Write more integration tests (but keep them separate from unit tests)
//
// If you complete any of the stretch goals, be sure to add tests!!!
func init() {
progress = Level{Current: 1}
}

func TestGenerateURL(t *testing.T) {
assert := assertions.New(t)
Expand All @@ -60,14 +65,20 @@ func TestGenerateURL(t *testing.T) {
got := GenerateURL(key, lat, long)

// Assert
assert.Equal(want, got, "Expected URL to be concatenated")
if assert.Equal(want, got, "Expected URL to be concatenated") {
progress.SetMessage()
fmt.Printf(progress.Msg, progress.MsgValues...)
progress.Current++
}
})
}

func TestBuildRequest(t *testing.T) {
assert := assertions.New(t)
t.Run("Build Request from url", func(t *testing.T) {
t.Skip("Delete this when ready to go next")
if progress.Current < 2 {
t.Skip("Insufficient progress")
}

// Docs -- https://golang.org/pkg/net/http
// Hint #1 -- https://gist.github.com/BrianGenisio/a3ee7551c2b6bf0ca89ff3cd09b3c5c5
Expand All @@ -92,18 +103,25 @@ func TestBuildRequest(t *testing.T) {
gotContentType := got.Header.Get("Content-Type")

// Assert
assert.Equal(wantURL, gotURL)
assert.Equal(wantMethod, gotMethod)
assert.Equal(wantJSONMIMEType, gotContentType, "Expected `Content-Type` Header to be application/json")
assert.Equal(wantJSONMIMEType, gotAccept, "Expected `Accept` Header to be application/json")
if assert.Equal(wantURL, gotURL) &&
assert.Equal(wantMethod, gotMethod) &&
assert.Equal(wantJSONMIMEType, gotContentType, "Expected `Content-Type` Header to be application/json") &&
assert.Equal(wantJSONMIMEType, gotAccept, "Expected `Accept` Header to be application/json") {

progress.SetMessage()
fmt.Printf(progress.Msg, progress.MsgValues...)
progress.Current++
}
}
})
}

func TestGetBody(t *testing.T) {
assert := assertions.New(t)
t.Run("Test Get Body from Response", func(t *testing.T) {
t.Skip("Delete this when ready to go next")
if progress.Current < 3 {
t.Skip("Insufficient progress")
}

// Docs -- https://golang.org/pkg/io
// Hint #1 -- https://gist.github.com/BrianGenisio/cf696a9e29883a5089f8ddd725e20651
Expand All @@ -124,15 +142,21 @@ func TestGetBody(t *testing.T) {
want := "OK"

// Assert
assert.Equal(want, got)
if assert.Equal(want, got) {
progress.SetMessage()
fmt.Printf(progress.Msg, progress.MsgValues...)
progress.Current++
}
})
}

func TestParseWeatherResponse(t *testing.T) {
assert := assertions.New(t)

t.Run("empty response returns empty result", func(t *testing.T) {
t.Skip("Delete this when ready to go next")
if progress.Current < 4 {
t.Skip("Insufficient progress")
}

// Hint #1 -- https://gist.github.com/StevenACoffman/f688e1d6b20b218443ffbdce6ec773be

Expand All @@ -144,12 +168,19 @@ func TestParseWeatherResponse(t *testing.T) {
got, err := ParseWeatherResponse(input)

// Assert
assert.NoError(err, "Expected no error from ParseWeatherResponse")
assert.Equal(got, want, "Response did not match what we expected")
if assert.NoError(err, "Expected no error from ParseWeatherResponse") &&
assert.Equal(got, want, "Response did not match what we expected") {

progress.SetMessage()
fmt.Printf(progress.Msg, progress.MsgValues...)
progress.Current++
}
})

t.Run("good response populates result", func(t *testing.T) {
t.Skip("Delete this when ready to go next")
if progress.Current < 5 {
t.Skip("Insufficient progress")
}

// Docs -- https://golang.org/pkg/encoding/json
// Hint #2 -- https://gist.github.com/StevenACoffman/d93ccb05b9a3016ff242f1622edc93ad
Expand All @@ -169,12 +200,19 @@ func TestParseWeatherResponse(t *testing.T) {
got, err := ParseWeatherResponse(input)

// Assert
assert.NoError(err, "Expected no error from ParseWeatherResponse")
assert.Equal(got, want, "Response did not match what we expected")
if assert.NoError(err, "Expected no error from ParseWeatherResponse") &&
assert.Equal(got, want, "Response did not match what we expected") {

progress.SetMessage()
fmt.Printf(progress.Msg, progress.MsgValues...)
progress.Current++
}
})

t.Run("bad response returns an error", func(t *testing.T) {
t.Skip("Delete this when ready to go next")
if progress.Current < 6 {
t.Skip("Insufficient progress")
}

// Hint #4 -- https://gist.github.com/StevenACoffman/d49defdf4147fbf9fdc9c8fcdc09f528

Expand All @@ -185,7 +223,11 @@ func TestParseWeatherResponse(t *testing.T) {
_, err := ParseWeatherResponse(input)

// Assert
assert.Error(err, "Expected an error from ParseWeatherResponse")
if assert.Error(err, "Expected an error from ParseWeatherResponse") {
progress.SetMessage()
fmt.Printf(progress.Msg, progress.MsgValues...)
progress.Current++
}
})
}

Expand Down