cloudanti

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

cloudanti: go-cloudant interface

cloudanti provides a convenience interface wrapper and mock functions for go-cloudant. The interface wraps and mocks all main library functions except for database's Bulk, Changes and NewFollower.

The package can be used directly (see basic or api examples) or copied and customized as required.

Getting started

Use cloudanti instead of cloudant to set up the client.

Refer to the full example for a working implementation.

main.go
import "github.com/barshociaj/go-cloudant/interface"

// Create Cloudant Client
client, err := cloudanti.NewClient("user123", "pa55w0rd01", "https://user123.cloudant.com")
main_test.go
// Create Cloudant Mock Client
client, _ := cloudanti.NewMockClient(cloudanti.CloudantContent{})

Mock content

Define mock content with mock docs and/or the list of document IDs to return in a view.

Views are tricky to mock, please be aware of the severe limitations:

  • View string must match the library call exactly, including the order of any provided ViewQuery parameters
  • Defined views are static, only the document contents will be updated
  • Undefined views return all docs in the mock database
content := cloudanti.CloudantContent{
    Databases: map[string]cloudanti.DatabaseContent{
        "my_db_name": {
            Docs: map[string][]byte{
                "my_doc_id1": []byte(`{"_id":"my_doc_id1","_rev":"34-23412341324","foo":"bar"}`),
                "my_doc_id2": []byte(`{"_id":"my_doc_id2","_rev":"1-12374912709","foo":"baz"}`),
            },
            Views: map[string][]string{
                "/_design/my_design_name/_view/my_view_name?descending=true": []string("my_doc_id2", "my_doc_id1"),
            },
        },
    },
}

client, _ := cloudanti.NewMockClient(content)

Refer to the api example for a working implementation.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(s interface{}, elem interface{}) bool

Contains finds if item exists in an array.

Types

type Client

type Client interface {
	Use(string) (Database, error)
	UseOrCreate(string) (Database, error)
	Destroy(string) error
	Exists(databaseName string) (bool, error)
	Info(databaseName string) (*cloudant.Info, error)
}

Client is the main interface for Cloudant instance operations

func NewClient

func NewClient(username, password, rootStrURL string, options ...cloudant.ClientOption) (Client, error)

NewClient returns a new Cloudanti client.

func NewMockClient

func NewMockClient(content CloudantContent) (Client, error)

NewMockClient mocks the creation of a new Cloudant instance.

type CloudantContent

type CloudantContent struct {
	Databases map[string]DatabaseContent
}

CloudantContent holds content for mock Cloudant instance.

type Database

type Database interface {
	Get(string, *cloudant.DocQuery, interface{}) error
	Insert(interface{}) (*cloudant.DocumentMeta, error)
	InsertEscaped(document interface{}) (*cloudant.DocumentMeta, error)
	InsertRaw(jsonDocument []byte) (*cloudant.DocumentMeta, error)
	Destroy(string, string) error
	List(q *cloudant.ViewQuery) (<-chan []byte, error)
	View(designName, viewName string, q *cloudant.ViewQuery) (<-chan []byte, error)
	ViewRaw(string, string, *cloudant.ViewQuery) ([]byte, error)
	Index(designName, indexName string, q *cloudant.IndexQuery) (<-chan []byte, error)
	IndexRaw(string, string, *cloudant.IndexQuery) ([]byte, error)
}

Database is an interface for Cloudant database operations

type DatabaseContent

type DatabaseContent struct {
	Info    cloudant.Info
	Docs    map[string][]byte
	Views   map[string][]string // map design URL, e.g. /_design/search~view/_view/versions?descending=true&include_docs=true&key=PLAN-abcd&limit=1 to list of doc IDs from docs
	Indexes map[string][]string // map design URL, e.g. /_design/search~view/_search/full?q=*:* to list of doc IDs from docs
}

DatabaseContent holds mock content for mock database.

Jump to

Keyboard shortcuts

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