-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_catalog.py
66 lines (55 loc) · 1.8 KB
/
test_catalog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Category, Item
#from flask.ext.sqlalchemy import SQLAlchemy
from random import randint
import datetime
import random
engine = create_engine('sqlite:///catalog.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
def myTests():
'''Checks if the cascade worked fine'''
# getting first item from first category
item = session.query(Item).first()
print "Testing READ 1 - "
print item.id
print item.name
print item.category_id
print ""
# deleting first category, items should get deleted too
print "Deleting category 1..."
categoryToDelete = session.query(Category).filter_by(id=1).one()
session.delete(categoryToDelete)
session.commit()
print "If cascade worked, items should be deleted along-with the category"
print ""
# should return first item of second category
item = session.query(Item).first()
print "Testing READ 2 - "
print item.id
print item.name
print item.category_id
print ""
# deleting first item of second category
itemToDelete = session.query(Item).filter_by(category_id=2).first()
print "Deleting first item from category 2 - "
session.delete(itemToDelete)
session.commit()
print ""
# deleting item should not delete category
print "Showing category 2..."
category = session.query(Category).filter_by(id=2).one()
print category.id
print category.name
print "If cascade worked, category should show"
print ""
# show second item which became first
item = session.query(Item).filter_by(category_id=2).first()
print "Testing READ 2 - "
print item.id
print item.name
print item.category_id
print ""
myTests()