Originally hosted at : https://github.com/erlepereira/thrift-experimentation (now deleted) Have pulled in inside here, owing to recent acquisition rumors ;) Date: 4 June 2018.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.8 KiB

  1. #!/usr/bin/env python
  2. import sys, unittest
  3. sys.path.append('../gen-py')
  4. from atmservice import atmService
  5. from atmservice.ttypes import *
  6. from thrift.transport import TSocket
  7. from thrift.transport import TTransport
  8. from thrift.protocol import TBinaryProtocol
  9. from thrift.server import TServer
  10. import socket
  11. if len(sys.argv) <= 2 or sys.argv[1] == '--help':
  12. print ''
  13. print 'Usage: ' + sys.argv[0] + ' <number of 20$ notes> <number of 50$ notes>'
  14. print ''
  15. sys.exit(0)
  16. amt20 = int(sys.argv[1])
  17. amt50 = int(sys.argv[2])
  18. class atmHandler:
  19. def __init__(self):
  20. self.denom = [ 20, 50 ];
  21. self.denom_amts = [ amt20, amt50 ];
  22. self.vault = self.denom[0] * self.denom_amts[0] + self.denom[1] * self.denom_amts[1]
  23. self.note20 = 0
  24. self.note50 = 0
  25. self.log = {}
  26. def active(self):
  27. print "active()"
  28. def amountquery(self):
  29. return self.vault
  30. def statusquery(self):
  31. return str(self.vault) + " - " + str(self.denom_amts[0]) + " in 20$ bills," + str(self.denom_amts[1]) + " in 50$ bills"
  32. def atmloader(self, loadamount):
  33. self.vault += loadamount
  34. return "active: amount reserve is $" + str(loadamount)
  35. def atmserve(self, number):
  36. if self.validcombo(number) != None:
  37. print "withdraw " + str(number)
  38. self.vault -= number
  39. print "left is " + str(self.vault)
  40. else:
  41. return "Invalid Amount"
  42. return str(number) + " - " + str(self.note20) + " in 20$ bills," + str(self.note50) + " in 50$ bills"
  43. return str(number)
  44. def commandfailure(self, ouch):
  45. ouch = "Something bad happended, amount not loaded"
  46. return ouch
  47. def transactionfailure (self, uhoh):
  48. uhoh = "Sorry, we cannot process this amount"
  49. return uhoh
  50. def validcombo(self, number):
  51. self.note50 = 0
  52. self.note20 = 0
  53. if number > 0 and number <= self.vault and len(str(number)) <= 3:
  54. print str(number) +" requested"
  55. while number >= 50:
  56. self.note50 += 1
  57. number -= 50
  58. #print number
  59. while number >= 20:
  60. self.note20 += 1
  61. number -= 20
  62. #print number
  63. if number != 0:
  64. return None
  65. self.denom_amts[0] -= self.note20
  66. self.denom_amts[1] -= self.note50
  67. return number
  68. else:
  69. return None
  70. def selftesting(atmtesting):
  71. case = unittest.TestLoader().loadTestsFromTestCase(atmtesting)
  72. result = unittest.TestResult()
  73. case(result)
  74. if result.wasSuccessful():
  75. return True
  76. else:
  77. print("Some tests failed!")
  78. for test, err in result.failures + result.errors:
  79. print(test)
  80. print(err)
  81. return False
  82. class selfTest(unittest.TestCase):
  83. def setUp(self):
  84. self.testhandler = atmHandler()
  85. def tearDown(self):
  86. self.testhandler = None
  87. def test_loader(self):
  88. print self.testhandler.statusquery()
  89. pass
  90. def test_statusquery(self):
  91. print "for Vault Amount :"
  92. self.assertEqual(self.testhandler.vault, self.testhandler.amountquery())
  93. print " ok "
  94. def test_withdraw_inlimit(self):
  95. print "for Withdrawl :"
  96. self.testhandler.atmserve(20)
  97. self.assertEqual((self.testhandler.vault ), self.testhandler.amountquery(), "First Withdrawal Failure")
  98. def test_withdraw_outlimit(self):
  99. print "for Exceeded Withdrawl Limit :"
  100. value = self.testhandler.vault
  101. self.testhandler.atmserve(self.testhandler.vault * 10)
  102. self.assertEqual(value, self.testhandler.amountquery(), "Withdrawl Failure: Amount too large")
  103. print " ok "
  104. def test_invalidamount(self):
  105. print "for Withdrawl Invalid Amount:"
  106. value = self.testhandler.vault
  107. print "value" + str(self.testhandler.vault)
  108. self.testhandler.atmserve(21)
  109. self.assertEqual(value, self.testhandler.amountquery(), "Wrong Amount Withdrawal Failure")
  110. print " ok "
  111. print "Running self test..."
  112. if not(selftesting(selfTest)):
  113. print "Init Tests Failed"
  114. sys.exit()
  115. print "Tests OK: resetting for use"
  116. handler = atmHandler()
  117. #re-init
  118. handler.denom_amts = [ amt20, amt50 ];
  119. handler.vault = handler.denom[0] * handler.denom_amts[0] + handler.denom[1] * handler.denom_amts[1]
  120. print handler.statusquery()
  121. processor = atmService.Processor(handler)
  122. transport = TSocket.TServerSocket(port=8082)
  123. tfactory = TTransport.TBufferedTransportFactory()
  124. pfactory = TBinaryProtocol.TBinaryProtocolFactory()
  125. server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
  126. print "Starting atmServer......"
  127. server.serve()
  128. print "done!"