Skip to content

Populate a mock table with test data #31

@NickLarsenNZ

Description

@NickLarsenNZ

Hi,

I've seen the example for setting up the mock client, but I'd like to reuse the client for other tests. I wondered if a mock table could be populated with some test data.

So far I have tried iterating through test data:

func init() {
	mock_dynamodb, mock = dynamock.New()
	test_table = "example"

	test_data = map[string]struct {
		etag        string
		lastversion string
	}{
		"aws": {etag: "33a64df551425fcc55e4d42a148795d9f25f89d4", lastversion: "v1.6.5"},
		"gcp": {etag: "51425fcc55e4d42a148795d9f4df5f89d42533a6", lastversion: "v2.3.1"},
	}

	m := mock.ExpectGetItem().ToTable(test_table)
	for key, value := range test_data {
		m.WithKeys(map[string]*dynamodb.AttributeValue{
			"Cloud": {
				S: aws.String(key),
			},
		}).WillReturns(dynamodb.GetItemOutput{
			Item: map[string]*dynamodb.AttributeValue{
				"Cloud": {S: aws.String(key)},
				"ETag":     {S: aws.String(value.etag)},
				"Version":  {S: aws.String(value.lastversion)},
			},
		})
	}
}

But I receive the following error in one of my tests (expecting to lookup the second test record, but the responses contain the first):

Expect key map[Provider:{
  S: "gcp"
}] but found key map[Provider:{
  S: "aws"
}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions