def test_create(): db = DB() name = random_string(10) UserAction.create(db, name) user = UserLoader.find_by_name(db, name) assert user.name == name
def test_delete(): db = DB() entry = create_entry() EntryAction.delete(db, entry.id) found_entry = EntryLoader.find_by_id(db, entry.id) assert found_entry is None
def test_find_by_owner_id(): db = DB() blog = create_blog() found_blog = BlogLoader.find_by_owner_id(db, blog.owner_id) assert found_blog.id == blog.id assert BlogLoader.find_by_owner_id(db, -1) is None
def test_find_by_id(): db = DB() user = create_user() blog = create_blog(user=user) found_blog = BlogLoader.find_by_id(db, blog.id) assert found_blog.id == blog.id assert BlogLoader.find_by_id(db, -1) is None
def test_find_by_name(): db = DB() user = create_user() found_user = UserLoader.find_by_name(db, user.name) assert isinstance(found_user, User) assert found_user.name == user.name not_found_user = UserLoader.find_by_name(db, random_string(10)) assert not_found_user is None
def test_find_by_id(): "EntryLoader.find_by_id() can find a entry by id" db = DB() entry = create_entry() found_entry = EntryLoader.find_by_id(db, entry.id) assert isinstance(found_entry, Entry) assert found_entry.id == entry.id not_found_entry = EntryLoader.find_by_id(db, 10) assert not_found_entry is None
def test_entry(): db = DB() with global_user(random_string(5)): blog = BlogAction.ensure_global_blog_created(db) entry = create_entry(blog=blog) res1 = web_client().get('/entry/' + str(entry.id)) assert res1.status == '200 OK' d = pq(res1.data) res2 = web_client().get('/entry/0') assert res2.status == '404 NOT FOUND'
def create_blog(user=None): db = DB() if user is None: user = create_user() new_blog_id = BlogAction.create( db, owner_id=user.id, title=random_string(10), ) return BlogLoader.find_by_id(db, new_blog_id)
def test_ensure_global_user_created(): db = DB() with global_user(random_string(10)) as global_user_name: assert UserLoader.find_by_name(db, global_user_name) is None user = UserAction.ensure_global_user_created(db) assert user.name == global_user_name # Check no exceptions raises user_again = UserAction.ensure_global_user_created(db) assert user_again.id == user.id
def create_entry(blog=None): db = DB() if blog is None: blog = create_blog() new_entry_id = EntryAction.post( db, blog_id=blog.id, title=random_string(30), body=random_string(100), ) return EntryLoader.find_by_id(db, new_entry_id)
def test_ensure_global_blog_created(): db = DB() with global_user(random_string(10)) as global_user_name: assert UserLoader.find_by_name(db, global_user_name) is None blog = BlogAction.ensure_global_blog_created(db) found_user = UserLoader.find_by_name(db, global_user_name) assert blog.owner_id == found_user.id # Check no exeception raises blog_again = BlogAction.ensure_global_blog_created(db) assert blog_again.id == blog.id
def test_create(): db = DB() user = create_user() title = random_string(10) blog_id = BlogAction.create( db, owner_id=user.id, title=title, ) found_blog = BlogLoader.find_by_id(db, blog_id) assert found_blog.id == blog_id assert found_blog.owner_id == user.id assert found_blog.title == title
def test_post(): db = DB() blog = create_blog() entry_id = EntryAction.post( db, blog_id=blog.id, title='タイトル', body='こんにちは', ) found_entry = EntryLoader.find_by_id(db, entry_id) assert found_entry.blog_id == blog.id assert found_entry.title == 'タイトル' assert found_entry.body == 'こんにちは'
def test_post_create_entry(): db = DB() with global_user(random_string(5)), web_client() as wc: blog = BlogAction.ensure_global_blog_created(db) res = wc.post('/-/post', data=dict( title='はろー', body='こんにちは', blog_id=blog.id, )) assert res.status == '302 FOUND' assert res.headers['Location'] == url_for('index', _external=True) entry = EntryLoader.find_entries(db, blog.id, limit=1)[0] assert entry.title == 'はろー' assert entry.body == 'こんにちは'
def test_index_with_entries(): db = DB() with global_user(random_string(5)): blog = BlogAction.ensure_global_blog_created(db) entries = [] with freeze_time('2017-01-13 12:00:02'): entries.append(create_entry(blog=blog)) with freeze_time('2017-01-13 12:00:01'): entries.append(create_entry(blog=blog)) with freeze_time('2017-01-13 12:00:00'): entries.append(create_entry(blog=blog)) res = web_client().get('/') assert res.status == '200 OK' d = pq(res.data) assert [int(d(a).attr('data-entry-id')) for a in d('.entry')] == [e.id for e in entries]
def test_find_entries(): "EntryLoader.find_entries can find entries for the specified blog" db = DB() blog = create_blog() entries = [] with freeze_time('2017-01-13 12:00:02'): entries.append(create_entry(blog=blog)) with freeze_time('2017-01-13 12:00:01'): entries.append(create_entry(blog=blog)) with freeze_time('2017-01-13 12:00:00'): entries.append(create_entry(blog=blog)) found_entries = EntryLoader.find_entries(db, blog.id) assert len(found_entries) == len(entries) assert [e.id for e in found_entries] == [e.id for e in entries] found_entries_with_limit = EntryLoader.find_entries(db, blog.id, limit=2) assert len(found_entries_with_limit) == len(entries[0:2]) assert [e.id for e in found_entries_with_limit] == [e.id for e in entries[0:2]]
def test_edit(): db = DB() blog = create_blog() entry_id = EntryAction.post( db, blog_id=blog.id, title='タイトルbefore', body='こんにちはbefore', ) EntryAction.edit( db, entry_id=entry_id, title='タイトルafter', body='こんにちはafter', ) found_entry = EntryLoader.find_by_id(db, entry_id) assert found_entry.blog_id == blog.id assert found_entry.title == 'タイトルafter' assert found_entry.body == 'こんにちはafter'
def create_user(): db = DB() name = random_string(10) UserAction.create(db, name) return UserLoader.find_by_name(db, name)
def get_db(): db = getattr(flask_g, "_database", None) if db is None: db = flask_g._database = DB() return db