dstest

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package dstest implements a mock Dark Sky server for testing.

Index

Examples

Constants

View Source
const DefaultKey = "key"

DefaultKey is the default key.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Server)

An Option sets an option on a Server.

func WithDefaultForecasts

func WithDefaultForecasts() Option

WithDefaultForecasts returns an option that adds all default forecasts to a Server.

func WithForecast

func WithForecast(request Request, forecastStr string) Option

WithForecast returns an option that adds a forecastStr as a response to request on a Server.

type Request

type Request struct {
	Latitude  float64
	Longitude float64
	Time      darksky.Time
	Exclude   string // A string, not a []darksky.Block so we can use Request as a map key.
	Extend    darksky.Extend
	Lang      darksky.Lang
	Units     darksky.Units
}

A Request contains parameters for a request.

func (*Request) Options

func (r *Request) Options() *darksky.ForecastOptions

Options returns a new darksky.ForecastOptions constructed from r.

type Server

type Server struct {
	*httptest.Server
	chi.Router
	Key       string
	Forecasts map[Request]string
}

A Server is a mock server.

func NewServer

func NewServer(options ...Option) *Server

NewServer returns a new Server.

Example
package main

import (
	"context"
	"fmt"
	"time"

	darksky "github.com/twpayne/go-darksky"
	"github.com/twpayne/go-darksky/dstest"
)

func main() {
	ctx := context.Background()

	s := dstest.NewServer(
		dstest.WithDefaultForecasts(),
	)
	c, err := s.NewClient()
	if err != nil {
		fmt.Println(err)
		return
	}

	f, err := c.Forecast(ctx, 34.0219, -118.4814, &darksky.Time{Time: time.Date(2019, 5, 1, 0, 0, 0, 0, time.UTC)}, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(f.Currently.Icon)

}
Output:

partly-cloudy-day

func (*Server) NewClient

func (s *Server) NewClient(options ...darksky.ClientOption) (*darksky.Client, error)

NewClient returns a new darksky.Client that connects to s.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL