fixtures

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GameScores = Fixture{
	Table: *gameScoresTable,
	Create: &dynamodb.CreateTableInput{
		TableName: gameScoresTable,
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("UserId"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("GameTitle"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("Wins"),
				AttributeType: aws.String("N"),
			},
			{
				AttributeName: aws.String("TopScore"),
				AttributeType: aws.String("N"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("UserId"),
				KeyType:       aws.String(dynamodb.KeyTypeHash),
			},
			{
				AttributeName: aws.String("GameTitle"),
				KeyType:       aws.String(dynamodb.KeyTypeRange),
			},
		},
		LocalSecondaryIndexes: []*dynamodb.LocalSecondaryIndex{
			{
				IndexName: aws.String("UserWinsIndex"),
				KeySchema: []*dynamodb.KeySchemaElement{
					{
						KeyType:       aws.String(dynamodb.KeyTypeHash),
						AttributeName: aws.String("UserId"),
					},
					{
						KeyType:       aws.String(dynamodb.KeyTypeRange),
						AttributeName: aws.String("Wins"),
					},
				},
				Projection: &dynamodb.Projection{
					ProjectionType: aws.String(dynamodb.ProjectionTypeKeysOnly),
				},
			},
		},
		GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{
			{
				IndexName: aws.String("GameTitleIndex"),
				KeySchema: []*dynamodb.KeySchemaElement{
					{
						KeyType:       aws.String(dynamodb.KeyTypeHash),
						AttributeName: aws.String("GameTitle"),
					},
					{
						KeyType:       aws.String(dynamodb.KeyTypeRange),
						AttributeName: aws.String("TopScore"),
					},
				},
				Projection: &dynamodb.Projection{
					ProjectionType: aws.String(dynamodb.ProjectionTypeKeysOnly),
				},
			},
		},
		BillingMode: aws.String(dynamodb.BillingModePayPerRequest),
	},
	Data: func(t *testing.T, client *dynamodb.DynamoDB) {
		for _, s := range gameScoresData {
			_, err := client.PutItem(&dynamodb.PutItemInput{
				TableName: gameScoresTable,
				Item: map[string]*dynamodb.AttributeValue{
					"UserId":    {S: &s.UserID},
					"GameTitle": {S: &s.GameTitle},
					"TopScore":  {N: aws.String(strconv.Itoa(s.TopScore))},
					"Wins":      {N: aws.String(strconv.Itoa(s.Wins))},
					"Losses":    {N: aws.String(strconv.Itoa(s.Losses))},
				},
			})
			require.NoError(t, err)
		}
	},
}

GameScores is a fixture with a video game high score table

View Source
var Movies = Fixture{
	Table: *moviesTable,
	Create: &dynamodb.CreateTableInput{
		TableName: moviesTable,
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("title"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("year"),
				AttributeType: aws.String("N"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("title"),
				KeyType:       aws.String(dynamodb.KeyTypeHash),
			},
			{
				AttributeName: aws.String("year"),
				KeyType:       aws.String(dynamodb.KeyTypeRange),
			},
		},
		BillingMode: aws.String(dynamodb.BillingModePayPerRequest),
	},
	Data: func(t *testing.T, client *dynamodb.DynamoDB) {
		var movies []Movie
		err := json.Unmarshal([]byte(movieData), &movies)
		require.NoError(t, err)
		for _, m := range movies {
			_, err := client.PutItem(&dynamodb.PutItemInput{
				TableName: moviesTable,
				Item: map[string]*dynamodb.AttributeValue{
					"title": {S: aws.String(m.Title)},
					"year":  {N: aws.String(strconv.Itoa(m.Year))},
					"info": {
						M: map[string]*dynamodb.AttributeValue{
							"actors": {
								SS: m.Info.Actors,
							},
							"directors": {
								SS: m.Info.Directors,
							},
							"genres": {
								SS: m.Info.Genres,
							},
							"imageURL": {
								S: aws.String(m.Info.ImageURL),
							},
							"plot": {
								S: aws.String(m.Info.Plot),
							},
							"rank": {
								N: aws.String(strconv.Itoa(m.Info.Rank)),
							},
							"rating": {
								N: aws.String(strconv.FormatFloat(m.Info.Rating, 'g', -1, 64)),
							},
							"release_date": {
								S: aws.String(m.Info.ReleaseDate),
							},
							"running_time_secs": {
								S: aws.String(strconv.Itoa(m.Info.RunningTimeSecs)),
							},
						},
					},
				},
			})
			require.NoError(t, err)
		}
	},
}

Movies is a fixture with a movies table and a few top movies.

Functions

func SetUp

func SetUp(t *testing.T, fixtures ...Fixture) *session.Session

SetUp creates and tears down a dynamodb table fixture.

Types

type Fixture

type Fixture struct {
	Table  string
	Create *dynamodb.CreateTableInput
	Data   func(t *testing.T, client *dynamodb.DynamoDB)
}

Fixture defines a set of DynamoDB tables and data for testing.

type GameScore

type GameScore struct {
	UserID    string
	GameTitle string
	TopScore  int
	Wins      int
	Losses    int
}

type Movie

type Movie struct {
	Title string    `json:"title"`
	Year  int       `json:"year"`
	Info  MovieInfo `json:"info"`
}

Movie is a container for movie data

type MovieInfo added in v0.0.2

type MovieInfo struct {
	Actors          []*string `json:"actors"`
	Directors       []*string `json:"directors"`
	Genres          []*string `json:"genres"`
	ImageURL        string    `json:"image_url"`
	Plot            string    `json:"plot"`
	Rank            int       `json:"rank"`
	Rating          float64   `json:"rating"`
	ReleaseDate     string    `json:"release_date"`
	RunningTimeSecs int       `json:"running_time_secs"`
}

Jump to

Keyboard shortcuts

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