Created
September 18, 2013 11:56
-
-
Save dusual/6608127 to your computer and use it in GitHub Desktop.
A basic pony test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from datetime import timedelta | |
from pony.orm import * | |
db = Database("sqlite", "data.sqlite", create_db=True) | |
class User(db.Entity): | |
#Answer 3 : Correcting User db | |
domain = Optional('Domain') | |
username = Required(unicode) | |
password = Required(unicode) | |
class Page(db.Entity): | |
url = Required(unicode) | |
# I think scape_time should be time | |
scrape_time = Required(datetime) | |
html = Required(LongUnicode) | |
domain = Required('Domain') | |
class Domain(db.Entity): | |
url = Required(unicode) | |
pages = Set('Page') | |
user = Required('User') | |
sql_debug(True) | |
db.generate_mapping(create_tables=True) | |
for i in range(1,11): | |
with db_session: | |
user = User(username = 'user%s' %(i), password = 'password%s' %(i)) | |
commit() | |
domain = Domain(url='http://example%s.com' %(i), user=user) | |
commit() | |
pages = [] | |
for i in range(1, 6): | |
page = Page(url = '%s/%s%s' % (domain.url, 'document', i), | |
scrape_time= datetime.now() + timedelta(seconds=1), | |
html='test', | |
domain=domain | |
) | |
commit() | |
# Answer 1: | |
with db_session: | |
domains = select(d for d in Domain) | |
for domain in domains: | |
print domain.url | |
print domain.user.username | |
#Answer 2: | |
with db_session: | |
pages = select(d.pages for d in Domain if d.id == 8) | |
for page in pages: | |
print page.url | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment