def setUp(self): try: self._my_class = Register() except Exception as excp: pass try: self._my_app = webtest.TestApp(application) # print self._my_app except Exception as excp: # print excp sys.exit(0)
def setUp(self): try: self._my_class=Register() except Exception as excp: pass try: self._my_app=webtest.TestApp(application) #print self._my_app except Exception as excp: #print excp sys.exit(0)
class Test_Register(unittest.TestCase ): def setUp(self): try: self._my_class=Register() except Exception as excp: pass try: self._my_app=webtest.TestApp(application) #print self._my_app except Exception as excp: #print excp sys.exit(0) def test_register_class_instantiates(self): self.assertIsInstance(self._my_class, Register, "Register class did not instiate") @unittest.expectedFailure def test_build_cart_unit(self): code, error=tidylib.tidy_document(self._my_class.build_cart(), options={'show-errors':1,'show-warnings':0}) self.assertFalse(error, "method build_cart does not return valid html page") def test_build_cart_functional(self): response=self._my_app.get('/register/build_cart') code, error=tidylib.tidy_document(response.body, options={'show-errors':1, 'show-warnings':0}) self.assertFalse(error, '/register/build_cart did not return functional html') def test_get_cart_empty(self): reply=self._my_app.get('/register/get_cart') self.assertNotEqual(reply.json, [], "couldn't get empty cart") @unittest.expectedFailure def test_add_item_to_cart_unit(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] args={'item':{"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice}} result=self._my_class.add_item_to_cart(**args) self.assertTrue(result, '/register/add_item_to_cart returned error in unittest') def test_add_item_to_cart_functional(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] item={"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice} result=self._my_app.post('/register/add_item_to_cart', {'item':json.dumps(item)}) confirm=self._my_app.get('/register/get_cart') print "confirm is", confirm print "test_add_inventoried", confirm.json[0]['items'][0] confirm=confirm.json[0]['items'][0] self.assertEqual(item, confirm, '/register/add_item_to_cart returned error in function test') def test_remove_item_from_cart_functional(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] item={"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice} self._my_app.post('/register/add_item_to_cart', {'item':json.dumps(item)}) self._my_app.post('/register/remove_item_from_cart', {'index':0}) confirm=self._my_app.get('/register/get_cart').json[0]['items'] self.assertEqual(confirm, [], "/register/remove_item_from_cart failed.") def test_void_cart(self): random_item_list=random.sample(list(Book.selectBy(status='STOCK')), 3) for random_item in random_item_list: item={"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice} self._my_app.post('/register/add_item_to_cart', {'item':json.dumps(item)}) self._my_app.post('/register/void_cart') confirm=self._my_app.get('/register/get_cart').json[0] self.assertEqual(confirm, {}, '/register/void_cart failed to destroy cart') def test_check_out_zeros_cart(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] item={"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice} self._my_app.post('/register/add_item_to_cart', {'item':json.dumps(item)}) self._my_app.post('/register/check_out') confirm=self._my_app.get('/register/get_cart').json[0] self.assertEqual(confirm, {}, '/register/checkout failed to destroy cart') def test_check_out_sells_book(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] item={"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice} self._my_app.post('/register/add_item_to_cart', {'item':json.dumps(item)}) self._my_app.post('/register/check_out') confirm=random_item.status self.assertEqual(confirm, 'SOLD', '/register/checkout failed mark book \'SOLD\'') def test_check_out_records_transaction(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] item={"department":"Book","isInventoried":"True","isTaxable":"True","booktitle":random_item.title.booktitle,"isbn":random_item.title.isbn,"bookID":random_item.id,"titleID":random_item.titleID,"ourprice":random_item.ourprice} self._my_app.post('/register/add_item_to_cart', {'item':json.dumps(item)}) cart_id=self._my_app.get('/register/get_cart').json[0]['uuid'] self._my_app.post('/register/check_out') transaction=Transaction.selectBy(cartID=cart_id) #print transaction self.assertEqual('SOLD', 'SOLD', '/register/checkout failed mark book \'SOLD\'') def test_select_item_search(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] code, error=tidylib.tidy_document(self._my_class.select_item_search(title=random_item.title.booktitle), options={'show-errors':1,'show-warnings':0}) self.assertFalse(error, "/register/select_item_search does not return valid html page") def test_get_item_by_isbn_in_stock(self): random_item=random.sample(list(Book.selectBy(status='STOCK')), 1)[0] result=self._my_class.get_item_by_isbn(**{'isbn':random_item.title.isbn}) #print "isbn_stock", random_item, result self.assertTrue(result, "/register/get_item_by_isbn does not return item when it should") def test_get_item_by_isbn_out_of_stock(self): query_string=''' SELECT * FROM title t2 JOIN book b2 ON b2.title_id=t2.id JOIN (SELECT t1.isbn FROM title t1 JOIN book b1 ON t1.id=b1.title_id GROUP BY t1.isbn HAVING COUNT(CASE WHEN b1.status='STOCK' THEN 1 END) = 0 ORDER BY t1.booktitle) as subq1 ON t2.isbn=subq1.isbn''' results= run_sql_select( query_string ) random_item= random.sample( results, 1 )[0] self.assertFalse(self._my_class.get_item_by_isbn(**{'isbn':random_item['isbn']}), "/register/get_item_by_isbn returns item when it shouldn't")
from inventoryserver.server import SpecialOrders # cherrypy.config.update({'environment': 'embedded'}) if cherrypy.__version__.startswith("3.0") and cherrypy.engine.state == 0: cherrypy.engine.start(blocking=False) atexit.register(cherrypy.engine.stop) class Root(object): def index(self): return "Hello World!" index.exposed = True root = InventoryServer() root.admin = Admin() root.staffing = Staffing() root.notes = Noteboard() root.register = Register() root.specialorder = SpecialOrders() cherrypy_local_config_file = configuration.get("cherrypy_local_config_file") application = cherrypy.Application( root, script_name=None, config=cherrypy_local_config_file ) if __name__ == "__main__": cherrypy.quickstart(root, "/", cherrypy_local_config_file)
class Test_Register(unittest.TestCase): def setUp(self): try: self._my_class = Register() except Exception as excp: pass try: self._my_app = webtest.TestApp(application) # print self._my_app except Exception as excp: # print excp sys.exit(0) def test_register_class_instantiates(self): self.assertIsInstance(self._my_class, Register, "Register class did not instiate") @unittest.expectedFailure def test_build_cart_unit(self): code, error = tidylib.tidy_document(self._my_class.build_cart(), options={ "show-errors": 1, "show-warnings": 0 }) self.assertFalse(error, "method build_cart does not return valid html page") def test_build_cart_functional(self): response = self._my_app.get("/register/build_cart") code, error = tidylib.tidy_document(response.body, options={ "show-errors": 1, "show-warnings": 0 }) self.assertFalse( error, "/register/build_cart did not return functional html") def test_get_cart_empty(self): reply = self._my_app.get("/register/get_cart") self.assertNotEqual(reply.json, [], "couldn't get empty cart") @unittest.expectedFailure def test_add_item_to_cart_unit(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] args = { "item": { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } } result = self._my_class.add_item_to_cart(**args) self.assertTrue( result, "/register/add_item_to_cart returned error in unittest") def test_add_item_to_cart_functional(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] item = { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } result = self._my_app.post("/register/add_item_to_cart", {"item": json.dumps(item)}) confirm = self._my_app.get("/register/get_cart") print(("confirm is", confirm)) print(("test_add_inventoried", confirm.json[0]["items"][0])) confirm = confirm.json[0]["items"][0] self.assertEqual( item, confirm, "/register/add_item_to_cart returned error in function test") def test_remove_item_from_cart_functional(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] item = { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } self._my_app.post("/register/add_item_to_cart", {"item": json.dumps(item)}) self._my_app.post("/register/remove_item_from_cart", {"index": 0}) confirm = self._my_app.get("/register/get_cart").json[0]["items"] self.assertEqual(confirm, [], "/register/remove_item_from_cart failed.") def test_void_cart(self): random_item_list = random.sample(list(Book.selectBy(status="STOCK")), 3) for random_item in random_item_list: item = { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } self._my_app.post("/register/add_item_to_cart", {"item": json.dumps(item)}) self._my_app.post("/register/void_cart") confirm = self._my_app.get("/register/get_cart").json[0] self.assertEqual(confirm, {}, "/register/void_cart failed to destroy cart") def test_check_out_zeros_cart(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] item = { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } self._my_app.post("/register/add_item_to_cart", {"item": json.dumps(item)}) self._my_app.post("/register/check_out") confirm = self._my_app.get("/register/get_cart").json[0] self.assertEqual(confirm, {}, "/register/checkout failed to destroy cart") def test_check_out_sells_book(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] item = { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } self._my_app.post("/register/add_item_to_cart", {"item": json.dumps(item)}) self._my_app.post("/register/check_out") confirm = random_item.status self.assertEqual(confirm, "SOLD", "/register/checkout failed mark book 'SOLD'") def test_check_out_records_transaction(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] item = { "department": "Book", "isInventoried": "True", "isTaxable": "True", "booktitle": random_item.title.booktitle, "isbn": random_item.title.isbn, "bookID": random_item.id, "titleID": random_item.titleID, "ourprice": random_item.ourprice, } self._my_app.post("/register/add_item_to_cart", {"item": json.dumps(item)}) cart_id = self._my_app.get("/register/get_cart").json[0]["uuid"] self._my_app.post("/register/check_out") transaction = Transaction.selectBy(cartID=cart_id) # print transaction self.assertEqual("SOLD", "SOLD", "/register/checkout failed mark book 'SOLD'") def test_select_item_search(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] code, error = tidylib.tidy_document( self._my_class.select_item_search( title=random_item.title.booktitle), options={ "show-errors": 1, "show-warnings": 0 }, ) self.assertFalse( error, "/register/select_item_search does not return valid html page") def test_get_item_by_isbn_in_stock(self): random_item = random.sample(list(Book.selectBy(status="STOCK")), 1)[0] result = self._my_class.get_item_by_isbn( **{"isbn": random_item.title.isbn}) # print "isbn_stock", random_item, result self.assertTrue( result, "/register/get_item_by_isbn does not return item when it should") def test_get_item_by_isbn_out_of_stock(self): # lookign for a title where none of its copies are in stock query_string = """ SELECT * FROM title t2 JOIN book b2 ON b2.title_id=t2.id JOIN (SELECT t1.isbn FROM title t1 JOIN book b1 ON t1.id=b1.title_id GROUP BY t1.isbn HAVING COUNT( CASE WHEN b1.status='STOCK' THEN 1 END) = 0 ORDER BY t1.isbn) as subq1 ON t2.isbn=subq1.isbn""" results = run_sql_select(query_string) random_item = random.sample(results, 1)[0] self.assertFalse( self._my_class.get_item_by_isbn(**{"isbn": random_item["isbn"]}), "/register/get_item_by_isbn returns item when it shouldn't", )