def issue_voucher(self, request, voucher_pool): [request_id] = request.args['request_id'] [operator] = request.args['operator'] [denomination] = request.args['denomination'] conn = yield self.engine.connect() pool = VoucherPool(voucher_pool, conn) try: voucher = yield pool.issue_voucher(operator, denomination) except NoVoucherPool: request.setResponseCode(404) returnValue(json.dumps({ 'request_id': request_id, 'error': 'Voucher pool does not exist.', })) except NoVoucherAvailable: # This is a normal condition, so we still return a 200 OK. returnValue(json.dumps({ 'request_id': request_id, 'error': 'No voucher available.', })) finally: yield conn.close() request.setHeader('Content-Type', 'application/json') returnValue(json.dumps({ 'request_id': request_id, 'voucher': voucher['voucher'], }))
def test_issue_voucher(self): pool = VoucherPool('testpool', self.conn) self.populate_pool(pool, ['Tank'], ['red'], [0]) rows = self.successResultOf(pool.count_vouchers()) assert rows == [('Tank', 'red', False, 1)] voucher = self.successResultOf(pool.issue_voucher('Tank', 'red')) assert voucher['operator'] == 'Tank' assert voucher['denomination'] == 'red' assert voucher['voucher'] == 'Tank-red-0' rows = self.successResultOf(pool.count_vouchers()) assert rows == [('Tank', 'red', True, 1)] self.failureResultOf( pool.issue_voucher('Tank', 'red'), NoVoucherAvailable)