Skip to content

Commit

Permalink
test: Added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniCodeMonkey committed Jan 15, 2022
1 parent 7ab98f9 commit 51543f4
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
geocodio
app
.idea

dist/
geocodio
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_name: geocodio-cli
project_name: geocodio
before:
hooks:
- go mod tidy
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ Command line application to create and manage spreadsheet uploads on Geocodio
## Usage

```
$ ./geocodio
$ ./geocodio
NAME:
Geocodio - Geocode lists using the Geocodio API
USAGE:
geocodio [global options] command [command options] [arguments...]
VERSION:
dev
COMMANDS:
create Geocode a new spreadsheet
list List existing geocoding jobs
status Query the status for a specific geocoding job
remove, rm Delete an existing geocoding job
download Download output data for a specific geocoding job
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--hostname value, -n value Geocodio hostname to use, change this for Geocodio+HIPAA or on-premise environments (default: "api.geocod.io") [$GEOCODIO_HOSTNAME]
--apikey value, -k value Geocodio API Key to use. Generate a new one in the Geocodio Dashboard [$GEOCODIO_API_KEY]
--apikey value, -k value Geocodio API Key to use. Generate a new one in the Geocodio Dashboard (default: "84a871a4ed113cc616ae328ae731a371c6dad14") [$GEOCODIO_API_KEY]
--help, -h show help (default: false)
--version, -v print the version (default: false)
```
12 changes: 7 additions & 5 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var apiVersion string = "v1.7"

func Request(method string, path string, c *cli.Context) ([]byte, error) {
func Request(method string, path string, c *cli.Context) ([]byte, bool, error) {
hostname := c.String("hostname")
apiKey := c.String("apikey")
url := fmt.Sprintf("https://%s/%s/%s?api_key=%s", hostname, apiVersion, path, url.QueryEscape(apiKey))
Expand All @@ -28,26 +28,28 @@ func Request(method string, path string, c *cli.Context) ([]byte, error) {

req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
return nil, false, err
}

req.Header.Set("User-Agent", buildUserAgent())

res, getErr := client.Do(req)
if getErr != nil {
return nil, getErr
return nil, false, getErr
}

if res.Body != nil {
defer res.Body.Close()
}

isJson := res.Header.Get("Content-Type") == "application/json"

body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
return nil, isJson, readErr
}

return body, nil
return body, isJson, nil
}

func Upload(file *os.File, direction string, format string, fields string, c *cli.Context) ([]byte, error) {
Expand Down
6 changes: 4 additions & 2 deletions api/data.go → api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package api
import "time"

type FileItem struct {
EstimatedRowsCount int `json:"estimated_rows_count"`
Filename string `json:"filename"`
EstimatedRowsCount int `json:"estimated_rows_count"`
Filename string `json:"filename"`
Headers []string `json:"headers,omitempty"`
}

type StatusItem struct {
Expand All @@ -23,4 +24,5 @@ type SpreadsheetJob struct {
DownloadUrl string `json:"download_url"`
ExpiresAt time.Time `json:"expires_at"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
15 changes: 15 additions & 0 deletions api/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import (
"github.com/geocodio/geocodio-cli/output"
"github.com/urfave/cli/v2"
"strconv"
)

func ValidateSpreadsheetJobId(c *cli.Context) (int, error) {
spreadsheetJobId, err := strconv.Atoi(c.Args().First())
if err != nil || spreadsheetJobId <= 0 {
return 0, output.ErrorStringAndExit("Invalid spreadsheet job id specified")
}
return spreadsheetJobId, nil
}
49 changes: 18 additions & 31 deletions create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/geocodio/geocodio-cli/api"
"github.com/geocodio/geocodio-cli/output"
"github.com/urfave/cli/v2"
"io"
"os"
"strings"
)
Expand Down Expand Up @@ -34,6 +35,8 @@ func RegisterCommand() *cli.Command {

func geocode(c *cli.Context) error {
direction, format, file, err := validateInput(c)
defer file.Close()

if err != nil {
return err
}
Expand All @@ -54,7 +57,7 @@ func geocode(c *cli.Context) error {
return err
}

outputJob(job)
outputJob(c.App.Writer, job)

return nil
}
Expand All @@ -81,7 +84,6 @@ func validateInput(c *cli.Context) (string, string, *os.File, error) {
if err != nil {
return "", "", nil, output.ErrorStringAndExit(fmt.Sprintf("Could not open %s", filename))
}
defer file.Close()

return direction, format, file, nil
}
Expand All @@ -91,38 +93,23 @@ func validateResponse(job api.SpreadsheetJob) error {
return output.ErrorStringAndExit(job.Message)
}

if job.Error != "" {
return output.ErrorStringAndExit(job.Error)
}

return nil
}

func outputJob(job api.SpreadsheetJob) {
output.Success("Spreadsheet job created")
fmt.Println("--------------------------")

fmt.Println("Id:")
fmt.Printf("\t%d\n\n", job.Id)
func outputJob(w io.Writer, job api.SpreadsheetJob) {
output.Success(w, "Spreadsheet job created")
fmt.Fprint(w, "\n")

fmt.Println("Filename:")
fmt.Printf("\t%s\n\n", job.File.Filename)
fmt.Fprintf(w, "Id: %d\n", job.Id)
fmt.Fprintf(w, "Filename: %s\n", job.File.Filename)
fmt.Fprintf(w, "Headers: %s\n", strings.Join(job.File.Headers, " | "))
fmt.Fprintf(w, "Rows: %s\n", humanize.Comma(int64(job.File.EstimatedRowsCount)))

fmt.Println("Rows:")
fmt.Printf("\t%s\n\n", humanize.Comma(int64(job.File.EstimatedRowsCount)))

fmt.Println("State:")
fmt.Printf("\t%s\n\n", job.Status.State)

fmt.Println("Progress:")
fmt.Printf("\t%.0f%%\n\n", job.Status.Progress)

fmt.Println("Message:")
fmt.Printf("\t%s\n\n", job.Status.Message)

fmt.Println("Time left:")
timeLeft := job.Status.TimeLeftDescription
if len(timeLeft) <= 0 {
timeLeft = "-"
}
fmt.Printf("\t%s\n\n", timeLeft)

fmt.Println("Expires:")
fmt.Printf("\t%s\n\n", job.ExpiresAt.Format("Mon Jan _2 15:04:05 2006"))
}
fmt.Fprint(w, "\n\tTo see job status\n")
fmt.Fprintf(w, "\t$ %s status %d", os.Args[0], job.Id)
}
50 changes: 50 additions & 0 deletions download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package download

import (
"fmt"
"github.com/geocodio/geocodio-cli/api"
"github.com/geocodio/geocodio-cli/output"
"github.com/urfave/cli/v2"
"net/http"
)

type downloadResponse struct {
Message string `json:"message"`
Success bool `json:"success"`
}

func RegisterCommand() *cli.Command {
var command *cli.Command
command = new(cli.Command)
command.Name = "download"
command.Usage = "Download output data for a specific geocoding job"
command.Action = download
command.ArgsUsage = "id"

return command
}

func download(c *cli.Context) error {
spreadsheetJobId, err := api.ValidateSpreadsheetJobId(c)
if err != nil {
return err
}

body, isJson, err := api.Request(http.MethodGet, fmt.Sprintf("lists/%d/download", spreadsheetJobId), c)
if err != nil {
return output.ErrorAndExit(err)
}

if isJson {
job := downloadResponse{}
if err = api.ParseJson(body, &job); err != nil {
return err
}

return output.ErrorStringAndExit(job.Message)
} else {
fmt.Fprintf(c.App.Writer, string(body))
}

return nil
}
41 changes: 5 additions & 36 deletions geocodio.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,17 @@
package main

import (
"github.com/geocodio/geocodio-cli/create"
"github.com/geocodio/geocodio-cli/list"
"github.com/geocodio/geocodio-cli/release"
"github.com/geocodio/geocodio-cli/remove"
"github.com/geocodio/geocodio-cli/status"
"github.com/urfave/cli/v2"
"log"
cli "github.com/geocodio/geocodio-cli/app"
"github.com/geocodio/geocodio-cli/output"
"os"
)

func main() {
app := cli.NewApp()
app.Name = "Geocodio"
app.Usage = "Geocode lists using the Geocodio API"
app.Version = release.Version()
app.EnableBashCompletion = true
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "hostname",
Aliases: []string{"n"},
Value: "api.geocod.io",
Usage: "Geocodio hostname to use, change this for Geocodio+HIPAA or on-premise environments",
EnvVars: []string{"GEOCODIO_HOSTNAME"},
},
&cli.StringFlag{
Name: "apikey",
Aliases: []string{"k"},
Value: "",
Usage: "Geocodio API Key to use. Generate a new one in the Geocodio Dashboard",
EnvVars: []string{"GEOCODIO_API_KEY"},
Required: true,
},
}
app.Commands = []*cli.Command{
create.RegisterCommand(),
list.RegisterCommand(),
status.RegisterCommand(),
remove.RegisterCommand(),
}
app := cli.BuildApp()

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
output.ErrorAndExit(err)
}
}

4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.13.0
github.com/olekukonko/tablewriter v0.0.5
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
Expand All @@ -20,11 +22,17 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 51543f4

Please sign in to comment.