def test_get_user(self): user1 = User(username='******', email='*****@*****.**') user2 = User(username='******', email='*****@*****.**') db.session.add(user1) db.session.add(user2) db.session.commit() user = backend.get_user(user1.id) assert user['username'] == user1.username users = backend.get_user([user1.id, user2.id]) assert len(users) == 2
def test_get_user_following(self): user1 = User(username='******', email='*****@*****.**') user2 = User(username='******', email='*****@*****.**') user3 = User(username='******', email='*****@*****.**') db.session.add(user1) db.session.add(user2) db.session.add(user3) db.session.commit() backend.follow_user(user1.id, user2.id) backend.follow_user(user1.id, user3.id) users = backend.get_user_following(user1.id) assert len(users) == 2 count = backend.get_user_following_count(user1.id) assert count == 2
def test_is_following_user(self): user1 = User(username='******', email='*****@*****.**') user2 = User(username='******', email='*****@*****.**') user3 = User(username='******', email='*****@*****.**') db.session.add(user1) db.session.add(user2) db.session.add(user3) db.session.commit() backend.follow_user(user1.id, user2.id) ret = backend.is_following_user(user1.id, user2.id) assert ret == True ret = backend.is_following_user(user1.id, user3.id) assert ret == False
def test_set_user(self): user1 = User(username='******', email='*****@*****.**', password='******') db.session.add(user1) db.session.commit() user2 = backend.set_user(user1.id, {'username': '******'}) assert user2['username'] == 'user02'
def test_follow(self): user1 = User(username='******', email='*****@*****.**') user2 = User(username='******', email='*****@*****.**') db.session.add(user1) db.session.add(user2) db.session.commit() ret = backend.follow_user(user1.id, user2.id) assert ret > 0 asso = UserFollowAsso.query.get(ret) assert asso.user_id == user1.id assert asso.user_id_to == user2.id ret = backend.unfollow_user(user1.id, user2.id) assert ret == True asso = UserFollowAsso.query.filter(UserFollowAsso.user_id == user1.id).\ filter(UserFollowAsso.user_id_to == user2.id).first() print asso assert asso == None
def test_add_user(self): user1 = User(username='******', email='*****@*****.**', password='******') db.session.add(user1) db.session.commit() user = backend.add_user('user02', '*****@*****.**', 'pass02') assert user['username'] == 'user02' self.assertRaises(BackendError, backend.add_user, 'user02', '*****@*****.**', 'pass02')
def add_user(username, email, passstr): assert_error(type(email) == types.StringType, 'ParamError', '邮箱应该为字符串') assert_error(type(passstr) == types.StringType, 'ParamError', '密码应该为字符串') assert_error( type(username) == types.StringType, 'ParamError', '用户昵称应该为字符串') if _check_username(username): raise BackendError('UsernameError', '用户名已经存在') if _check_email(email): raise BackendError('EmailError', '邮箱已经存在') try: passstr = generate_password_hash(passstr) user = User(email=email, username=username, password=passstr) db.session.add(user) db.session.commit() except: db.session.rollback() raise BackendError('InternalError', traceback.format_exc()) return user.json