def setup_data(mock_dynamodb): created_wheel = json.loads( wheel.create_wheel({'body': { 'name': 'Test Wheel' }})['body']) create_participant_events = [{ 'pathParameters': { 'wheel_id': created_wheel['id'] }, 'body': { 'name': name, 'url': 'https://amazon.com' } } for name in ['Dan', 'Alexa', 'Jeff']] created_participants = [ json.loads(wheel_participant.create_participant(event)['body']) for event in create_participant_events ] # Reloads the wheel with updated participant count return { 'wheel': json.loads( wheel.get_wheel({ 'body': {}, 'pathParameters': { 'wheel_id': created_wheel['id'] } })['body']), 'participants': created_participants }
def test_invalid_create_participant(mock_dynamodb): response = wheel_participant.create_participant({ 'body': { 'name': '', 'url': '' }, 'pathParameters': { 'wheel_id': WHEEL_ID} }) assert response['statusCode'] == 400 assert 'Participants require a name and url which must be at least 1 character in length' in response['body']
def test_create_participant(mock_dynamodb, mock_participant_table): event = { 'pathParameters': { 'wheel_id': WHEEL_ID }, 'body': { 'name': 'Dan', 'url': 'https://amazon.com' } } response = wheel_participant.create_participant(event) created_participant = json.loads(response['body']) assert response['statusCode'] == 200 assert created_participant['name'] == event['body']['name'] assert created_participant['url'] == event['body']['url'] assert mock_participant_table.get_existing_item(Key={'id': created_participant['id'], 'wheel_id': WHEEL_ID})
def test_fix_incorrect_participant_count(mock_dynamodb, setup_data, mock_wheel_table): out_of_whack = 999 wheel = setup_data['wheel'] wheel_id = wheel['id'] proper_participant_count = wheel['participant_count'] # # # # We will first test this on a select_participant operation. # Throw the participant count way out of whack. mock_wheel_table.update_item(Key={'id': wheel['id']}, **to_update_kwargs( {'participant_count': out_of_whack})) participant_count = mock_wheel_table.query(KeyConditionExpression=Key( 'id').eq(wheel['id']))['Items'][0].get('participant_count') # # Ensure it's out of whack. assert abs(out_of_whack - participant_count) < epsilon # Select a participant to cause correction of participant count. wheel = Wheel.get_existing_item(Key={'id': wheel_id}) choice_algorithm.select_participant(wheel, setup_data['participants'][0]) # ...and ensure it's back into whack. participant_count = mock_wheel_table.query(KeyConditionExpression=Key( 'id').eq(wheel['id']))['Items'][0].get('participant_count') assert abs(Decimal(proper_participant_count) - participant_count) < epsilon # # # # We will next test this on a delete_participant operation. # Throw the participant count way out of whack. mock_wheel_table.update_item(Key={'id': wheel['id']}, **to_update_kwargs( {'participant_count': out_of_whack})) participant_count = mock_wheel_table.query(KeyConditionExpression=Key( 'id').eq(wheel['id']))['Items'][0].get('participant_count') # # Ensure it's out of whack. assert abs(out_of_whack - participant_count) < epsilon # Delete a participant to cause correction of participant count. event = { 'body': {}, 'pathParameters': { 'wheel_id': wheel_id, 'participant_id': setup_data['participants'][0]['id'] } } wheel_participant.delete_participant(event) # # ...and ensure it's back into whack. participant_count = mock_wheel_table.query(KeyConditionExpression=Key( 'id').eq(wheel['id']))['Items'][0].get('participant_count') assert abs((Decimal(proper_participant_count) - 1) - participant_count) < epsilon # # # # We will next test this on a create_participant operation. # Throw the participant count way out of whack. mock_wheel_table.update_item(Key={'id': wheel['id']}, **to_update_kwargs( {'participant_count': out_of_whack})) participant_count = mock_wheel_table.query(KeyConditionExpression=Key( 'id').eq(wheel['id']))['Items'][0].get('participant_count') # # Ensure it's out of whack. assert abs(out_of_whack - participant_count) < epsilon # Add a participant to cause correction of participant count. event = { 'pathParameters': { 'wheel_id': wheel_id }, 'body': { 'name': 'Ishmael-on-the-Sea', 'url': 'https://amazon.com' } } wheel_participant.create_participant(event) # # ...and ensure it's back into whack. participant_count = mock_wheel_table.query(KeyConditionExpression=Key( 'id').eq(wheel['id']))['Items'][0].get('participant_count') assert abs((Decimal(proper_participant_count)) - participant_count) < epsilon