コード例 #1
0
	def test_followers(self):
		#testing db is empty
		assert User.query.count() is 0

        	u1 = User('testuser1', 'testpassword1')
        	u2 = User('testuser2', 'testpassword2')
        	ua1 = User('testadmin1', 'testpassword1', True)
        	ua2 = User('testadmin2', 'testpassword2', True)

		db.session.add(u1)
        	db.session.add(u2)
		db.session.add(ua1)
		db.session.add(ua2)
        	db.session.commit()
		assert User.query.count() is 4

		#ensure there is currently no one following each other
        	assert u1.unfollow(u2) is None
        	assert u2.unfollow(u1) is None
		assert ua1.unfollow(ua2) is None
		assert ua2.unfollow(ua1) is None
		assert ua1.unfollow(u1) is None
		assert ua2.unfollow(u2) is None
		#make users follow each other and check success (follow returns self on success)
		assert u1.follow(u2) is not None
		assert u1.check_following(u2)
		assert u1.followed.count() is 1
		assert u2 in u1.get_followers()
		assert u1 in u2.get_followed()

		assert ua1.follow(ua2) is not None
		assert ua1.check_following(ua2)
		assert ua1.followed.count() is 1
		assert ua2 in ua1.get_followers()
		assert ua1 in ua2.get_followed()
		
		assert ua1.follow(u1) is not None
		assert ua1.check_following(u1)
		assert ua1.followed.count() == 2
		assert u1 in ua1.get_followers()
		assert ua1 in u1.get_followed()

		#check doing same follows does not succeed (follow returns None on failure)
		assert u1.follow(u2) is None
		assert ua1.follow(ua2) is None
		assert ua1.follow(u1) is None
		#test unfollow
		assert u1.unfollow(u2) is not None
		assert ua1.unfollow(ua2) is not None
		assert ua1.unfollow(u1) is not None
		assert not u1.check_following(u2)
		assert not ua1.check_following(ua2)
		assert not ua1.check_following(u2)
コード例 #2
0
	def test_posts(self):
		#testing db is empty
		assert User.query.count() is 0
		assert Post.query.count() is 0

        	u1 = User('testuser1', 'testpassword1')
        	u2 = User('testuser2', 'testpassword2')
        	ua1 = User('testadmin1', 'testpassword1', True)
        	ua2 = User('testadmin2', 'testpassword2', True)

		db.session.add(u1)
        	db.session.add(u2)
		db.session.add(ua1)
		db.session.add(ua2)
        	db.session.commit()
		assert User.query.count() is 4

		#need to first create followers for future posts testing
		#ensure there is currently no one following each other
        	assert u1.unfollow(u2) is None
        	assert u2.unfollow(u1) is None
		assert ua1.unfollow(ua2) is None
		assert ua2.unfollow(ua1) is None
		assert ua1.unfollow(u1) is None
		assert ua2.unfollow(u2) is None
		#make users follow each other and check success (follow returns self on success)
		assert u1.follow(u2) is not None
		assert u1.check_following(u2)
		assert u1.followed.count() is 1
		assert u2 in u1.get_followers()
		assert u1 in u2.get_followed()

		assert ua1.follow(ua2) is not None
		assert ua1.check_following(ua2)
		assert ua1.followed.count() is 1
		assert ua2 in ua1.get_followers()
		assert ua1 in ua2.get_followed()
		
		assert ua1.follow(u1) is not None
		assert ua1.check_following(u1)
		assert ua1.followed.count() == 2
		assert u1 in ua1.get_followers()
		assert ua1 in u1.get_followed()

		#test creating posts
		assert u1.create_post("This is user1's post!") is not None
		assert len(u1.get_posts()) is 1
		assert (u1.get_posts()[0].message == "This is user1's post!")
		assert u1.get_posts()[0].owner == u1.username

		assert u2.create_post("This is user2's post!") is not None
		assert len(u2.get_posts()) is 1
		assert (u2.get_posts()[0].message == "This is user2's post!")
		assert u2.get_posts()[0].owner == u2.username
		
		assert ua1.create_post("This is admin1's post!") is not None
		assert len(ua1.get_posts()) is 1
		assert (ua1.get_posts()[0].message == "This is admin1's post!")
		assert ua1.get_posts()[0].owner == ua1.username

		#test fetching followers posts
		assert len(u1.get_followers_posts()) is 1
		assert (u1.get_followers_posts()[0].message == "This is admin1's post!")
		assert u1.get_followers_posts()[0].owner == ua1.username
		assert len(u1.get_following_posts()) is 1
		assert (u1.get_following_posts()[0].message == "This is user2's post!")
		assert u1.get_following_posts()[0].owner == u2.username
		assert len(u2.get_followers_posts()) is 1
		assert (u2.get_followers_posts()[0].message == "This is user1's post!")
		assert u2.get_followers_posts()[0].owner == u1.username
		assert len(u2.get_following_posts()) is 0
		assert len(ua1.get_followers_posts()) is 0
		assert len(ua1.get_following_posts()) is 1
		assert (ua1.get_following_posts()[0].message == "This is user1's post!")
		assert ua1.get_following_posts()[0].owner == u1.username

		#unfollow
		assert u1.unfollow(u2) is not None
		assert ua1.unfollow(ua2) is not None
		assert ua1.unfollow(u1) is not None
		assert not u1.check_following(u2)
		assert not ua1.check_following(ua2)
		assert not ua1.check_following(u2)		
		
		#test fetching followers posts
		assert len(u1.get_following_posts()) is 0
		assert len(u1.get_followers_posts()) is 0
		assert len(u2.get_following_posts()) is 0
		assert len(u2.get_followers_posts()) is 0
		assert len(ua1.get_following_posts()) is 0
		assert len(ua1.get_followers_posts()) is 0