The unit test

Here are the units tests that can be found in the middle of serverless-microservice-data-api/test/test_dynamo_get.py:

    def test_validparameters_parseparameters_pass(self):
parameters = lambda_query_dynamo.HttpUtils.parse_parameters(
self.validJsonDataStartDate)
assert parameters['parsedParams']['startDate'] == u'20171013'
assert parameters['parsedParams']['resource_id'] == u'324'

def test_emptybody_parsebody_nonebody(self):
body = lambda_query_dynamo.HttpUtils.parse_body(
self.validJsonDataStartDate)
assert body['body'] is None

def test_invalidjson_getrecord_notfound404(self):
result = lambda_query_dynamo.Controller.get_dynamodb_records(
self.invalidJsonData)
assert result['statusCode'] == '404'

def test_invaliduserid_getrecord_invalididerror(self):
result = lambda_query_dynamo.Controller.get_dynamodb_records(
self.invalidJsonUserIdData)
assert result['statusCode'] == '404'
assert json.loads(result['body'])['message'] ==
"resource_id not a number"

I'm using a prefix of test so Python test suites can automatically detect them as unit tests, and I'm using the triplet unit test naming convention for the test methods: the method name, the state under test, and the expected behavior. The test methods are as follows:

  • test_validparameters_parseparameters_pass(): Checks that the parameters are parsed correctly.
  • test_emptybody_parsebody_nonebody(): We are not using a body in the GET method, so we want to make sure that it still works if none is provided.
  • test_invalidjson_getrecord_notfound404(): Check how the Lambda will react with an invalid JSON payload.
  • test_invaliduserid_getrecord_invalididerror(): Check how the Lambda will react to an invalid non-number userId.

The preceding does not query DynamoDB for the records. If we want to do so, we should have DynamoDB running, use the new DynamoDB Local (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html), or we can mock the DynamoDB calls, which is what we will look at next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset