Beispiel #1
0
		def testCommentModel(self):
			with app.app_context():
				user = User(username="******", password="******", email="*****@*****.**", admin = True)
				db.session.add(user)
				db.session.commit()

				self.assertEqual(len(User.query.all()), 1)

				newCourse = Course(name='COMP 1010', alt_name='Computer science', professor = 1)
				db.session.add(newCourse)
				db.session.commit()

				self.assertEqual(len(Course.query.all()), 1)

				message = Message(message="Hello this is message", posttime=datetime.utcnow(), courseid=newCourse.courseid, userid = user.uid)
				db.session.add(message)
				db.session.commit()

				self.assertEqual(len(Message.query.all()), 1)

				# side loading of messages within comments is not 
				# post processed 
				with app.test_client() as c:
					resp = c.get('api/comments')
					data = json.loads(resp.data)
					self.assertEqual(len(data), 1)

				comment = Comment(comment="test comment", posttime=datetime.utcnow(), messageid=1, userid = 1)
				db.session.add(comment)
				db.session.commit()

				comment = Comment(comment="test comment2", posttime=datetime.utcnow(), messageid=1, userid = 1)
				db.session.add(comment)
				db.session.commit()

				self.assertEqual(len(Comment.query.all()), 2)

				with app.test_client() as c:
					resp = c.get('api/comments')
					data = json.loads(resp.data)
					self.assertEqual(len(data['comments']), 2)
					self.assertEqual(data['comments'][0]['id'], data['comments'][0]['commentid'])
					self.assertEqual(data['comments'][1]['id'], data['comments'][1]['commentid'])

				with app.test_client() as c:
					resp = c.get('api/comments/1')
					data = json.loads(resp.data)
					self.assertEqual(data['comment'], "test comment")
					self.assertEqual(data['messageid'], 1)
					self.assertEqual(data['userid'], 1)
					self.assertEqual(data['message']['userid'], data['userid'])
Beispiel #2
0
		def setUp(self):
				basedir = os.path.abspath(os.path.dirname(__file__))
				app.config['TESTING'] = True
				app.config['SQLALCHEMY_DATABASE_URI'] = TEST_DB
				self.app = app.test_client()
				with app.app_context():
						db.create_all()
Beispiel #3
0
		def testUsersModel(self):
			with app.app_context():
				newUser = User(username="******", password="******", email="*****@*****.**",admin = False)
				db.session.add(newUser)
				db.session.flush()
				self.assertEqual(db.session.query(User).get(1).username, 'Bob')

				with app.test_client() as c:
					resp = c.get('api/users/1')
					data = json.loads(resp.data)
					self.assertEqual(data['user']['username'], "Bob")
					self.assertEqual(data['user']['password'], "dinosaur")
					self.assertEqual(data['user']['email'], "*****@*****.**")
					self.assertEqual(len(data['courses']), 0)

				db.session.delete(newUser)
				db.session.flush()

				self.assertEqual(db.session.query(User).get(1), None)
Beispiel #4
0
		def testMessageModel(self):
			with app.app_context():
				user = User(username="******", password="******", email="*****@*****.**", admin = True)
				db.session.add(user)
				db.session.commit()

				self.assertEqual(len(User.query.all()), 1)

				newCourse = Course(name='COMP 1010', alt_name='Computer science', professor = 1)
				db.session.add(newCourse)
				db.session.commit()

				self.assertEqual(len(Course.query.all()), 1)

				message = Message(message="Hello this is message", posttime=datetime.utcnow(), courseid=newCourse.courseid, userid = user.uid)
				db.session.add(message)
				db.session.commit()

				self.assertEqual(len(Message.query.all()), 1)

				largeStr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
				message = Message(message=largeStr, posttime=datetime.utcnow(), courseid=newCourse.courseid, userid = user.uid)
				db.session.add(message)
				db.session.commit()

				self.assertEqual(len(Message.query.all()), 2)

				# testing side loading of comments
				with app.test_client() as c:
					resp = c.get('api/messages')
					data = json.loads(resp.data)
					self.assertEqual(len(data), 2)

				with app.test_client() as c:
					resp = c.get('api/messages/1')
					data = json.loads(resp.data)
					self.assertEqual(data['message'], "Hello this is message")
					self.assertEqual(data['courseid'], 1)
					self.assertEqual(data['userid'], 1)
					self.assertEqual(data['user'], data['userid'])
					self.assertEqual(data['course'], data['courseid'])
					self.assertEqual(len(data['comments']), 0)
					self.assertEqual(len(data['comment_ids']), 0)

				comment = Comment(comment="test comment", posttime=datetime.utcnow(), messageid=2, userid = 1)
				db.session.add(comment)
				db.session.commit()

				comment = Comment(comment="test comment2", posttime=datetime.utcnow(), messageid=2, userid = 1)
				db.session.add(comment)
				db.session.commit()

				self.assertEqual(len(Comment.query.all()), 2)

				with app.test_client() as c:
					resp = c.get('api/messages/2')
					data = json.loads(resp.data)
					self.assertEqual(data['courseid'], 1)
					self.assertEqual(data['userid'], 1)
					self.assertEqual(data['user'], data['userid'])
					self.assertEqual(data['course'], data['courseid'])
					self.assertEqual(len(data['comments']), 2)
					self.assertEqual(len(data['comment_ids']), 2)