testhelpers

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package testhelpers provides common testing helpers and utilities that are used across packages in this project. Only non-trivial helper logic used in multiple places in this library should be added here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckErrorNDJSONFile

func CheckErrorNDJSONFile(t *testing.T, dir string, wantErrors []ErrorNDJSONLine)

CheckErrorNDJSONFile is a helper to test that the contents of the error ndjson file written by fhirstore.Uploader are correct.

func FHIRStoreServer

func FHIRStoreServer(t *testing.T, expectedResources []FHIRStoreTestResource, projectID, location, datasetID, fhirStoreID string) string

FHIRStoreServer creates a test FHIR store server that expects the provided expectedResources uploaded as single resources. If it receives valid upload requests that do not include elements from expectedResources, it will call t.Errorf with an error. If not all of the resources in expectedResources are uploaded by the end of the test errors are thrown. The test server's URL is returned by this function, and is auto-closed at the end of the test.

func FHIRStoreServerBatch

func FHIRStoreServerBatch(t *testing.T, expectedFHIRResources [][]byte, expectedFullBatchSize int, projectID, location, datasetID, fhirStoreID string) string

FHIRStoreServerBatch sets up a test FHIR Store Server for batch executeBundle requests. It ensures proper executeBundle requests are sent, and that the bundles are in batch mode and contain the expectedFHIRResources. It is okay to upload the full set of expectedFHIRResources over multiple executeBundle batch calls to this server. At the end of the test, the teardown of this server checks and ensures that all expectedFHIRResources were uploaded at least once.

func GCPLogServer

func GCPLogServer() (*GRPCServer, *LoggingHandler, error)

GCPLogServer creates an in-memory fake server implementing the GCP logging service and starts it. Call close on the server when done. GetLogsAfterClose can be used to verify logs were written.

func NormalizeJSON

func NormalizeJSON(t *testing.T, jsonIn []byte) []byte

NormalizeJSON normalizes the input json bytes to look like how it would look as if marshaled from a json.Marshal. In particular, this may reorder some fields (e.g. json object keys are sorted alphabetically), but the json should be equivalent.

func NormalizeJSONString

func NormalizeJSONString(t *testing.T, jsonIn string) string

NormalizeJSONString normalizes the input json to look how it would look if marshaled from a json.Marshal.

func ReadAllFHIRJSON

func ReadAllFHIRJSON(t *testing.T, outputDir string, normalize bool) [][]byte

ReadAllFHIRJSON reads all ndjsons in the output directory, extracts out the FHIR json for each resource, and adds it to the output [][]byte. If normalize=true, then NormalizeJSON is applied to the json bytes before being added to the output.

func ReadAllGCSFHIRJSON

func ReadAllGCSFHIRJSON(t *testing.T, gcsServer *GCSServer, normalize bool) [][]byte

ReadAllGCSFHIRJSON reads ALL files in the gcsServer, attempets to extract the FHIR json for each resource, and adds it to the output [][]byte. If normalize=true, then NormalizeJSON is applied to the json bytes before being added to the output.

Types

type ErrorNDJSONLine

type ErrorNDJSONLine struct {
	Err          string `json:"err"`
	FHIRResource string `json:"fhir_resource"`
}

ErrorNDJSONLine represents one line of an error NDJSON file produced by fhirstore.Uploader.

type FHIRStoreTestResource

type FHIRStoreTestResource struct {
	ResourceID       string
	ResourceTypeCode cpb.ResourceTypeCode_Value
	Data             []byte
}

FHIRStoreTestResource represents a test FHIR resource to be uploaded to FHIR store.

type GCSObjectEntry

type GCSObjectEntry struct {
	Data        []byte
	ContentType string
}

GCSObjectEntry holds the contents and content type of stored objects.

type GCSServer

type GCSServer struct {
	// contains filtered or unexported fields
}

GCSServer provides a minimal implementation of the GCS API for use in tests.

func NewGCSServer

func NewGCSServer(t *testing.T) *GCSServer

NewGCSServer creates a new GCS Server for use in tests.

func (*GCSServer) AddObject

func (gs *GCSServer) AddObject(bucket, name string, obj GCSObjectEntry)

AddObject adds an object to be served by the GCS server.

func (*GCSServer) GetAllObjects

func (gs *GCSServer) GetAllObjects() []GCSObjectEntry

GetAllObjects returns all objects uploaded to this test server across all buckets. Use this only if needed for your test, otherwise prefer GetObject.

func (*GCSServer) GetAllPaths

func (gs *GCSServer) GetAllPaths() []string

GetAllPaths returns a slice representing the upload path of all items that have been uploaded to the test server in the form gs://bucket/path.

func (*GCSServer) GetObject

func (gs *GCSServer) GetObject(bucket, name string) (GCSObjectEntry, bool)

GetObject retrieves an object which has been uploaded to the server.

func (*GCSServer) URL

func (gs *GCSServer) URL() string

URL returns the URL of the GCS server to be passed to the client library.

type GRPCServer

type GRPCServer struct {
	// contains filtered or unexported fields
}

GRPCServer is an in-process gRPC server, listening on the local loopback interface. GRPCServers are for testing only and are not intended to be used in production code.

func (*GRPCServer) Addr

func (s *GRPCServer) Addr() string

Addr returns the address of the server.

func (*GRPCServer) Close

func (s *GRPCServer) Close()

Close shuts down the server.

type LoggingHandler

type LoggingHandler struct {
	logpb.LoggingServiceV2Server
	// contains filtered or unexported fields
}

LoggingHandler implements the log rpc server and holds an in memory map of logs.

func (*LoggingHandler) GetLogsAfterClose

func (h *LoggingHandler) GetLogsAfterClose() map[string][]*logpb.LogEntry

GetLogsAfterClose returns the in-memory map of written logs. It should be called after the Log Client is closed or flushed.

func (*LoggingHandler) WriteLogEntries

WriteLogEntries usually writes log entries to Cloud Logging. In this fake it saves the logs in an in-memory map. This function must be exported for the grpc server to work but should not be called directly in tests.

Jump to

Keyboard shortcuts

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