def test_submissionsentiment_model(session): s = Submission.create(session, **MOCK_SUBMISSION) c1 = Comment.create(session, **MOCK_COMMENT1) c1_sentiment = comment_sentiment(c1) c1s = CommentSentiment.create(session, **c1_sentiment) c2 = Comment.create(session, **MOCK_COMMENT2) c2_sentiment = comment_sentiment(c2) c2s = CommentSentiment.create(session, **c2_sentiment) comments = session.query(Comment).options(joinedload("sentiment")).all() comment_sentiments = [] for c in comments: comment_sentiments.append({"polarity": c.sentiment.polarity, "subjectivity": c.sentiment.subjectivity}) submission_sentiment = comment_sentiment_avg(comment_sentiments) submission_sentiment.update({"submission_id": s.id}) ss = SubmissionSentiment.create(session, **submission_sentiment) submission_sentiments = session.query(SubmissionSentiment).all() ss1 = submission_sentiments[0] # test object form assert ss1.id == 1 assert ss1.submission_id == s.id for k in submission_sentiment.keys(): assert getattr(ss1, k) == submission_sentiment[k] # test values assert ss1.polarity < 0.5 and ss1.polarity > -0.5 assert ss1.subjectivity > 0.8
def test_comment_sentiment_avg(session): s = Submission.create(session, **const.MOCK_SUBMISSION) c1 = Comment.create(session, **const.MOCK_COMMENT1) c1_sentiment = sentiment.comment_sentiment(c1) c1s = CommentSentiment.create(session, **c1_sentiment) c2 = Comment.create(session, **const.MOCK_COMMENT2) c2_sentiment = sentiment.comment_sentiment(c2) c2s = CommentSentiment.create(session, **c2_sentiment) comments = session.query(Comment).\ options(joinedload('sentiment')).\ all() comment_sentiments = [] for c in comments: comment_sentiments.append({ "polarity": c.sentiment.polarity, "subjectivity": c.sentiment.subjectivity }) csa = sentiment.comment_sentiment_avg(comment_sentiments) csa.update({'submission_id': s.id}) expected_keys = ['submission_id', 'polarity', 'subjectivity'] assert sorted(csa.keys()) == sorted(expected_keys) assert isinstance(csa['polarity'], float) assert isinstance(csa['subjectivity'], float)
def test_commentsentiment_model(session): s = Submission.create(session, **MOCK_SUBMISSION) c1 = Comment.create(session, **MOCK_COMMENT1) c1_sentiment = comment_sentiment(c1) c1s = CommentSentiment.create(session, **c1_sentiment) c2 = Comment.create(session, **MOCK_COMMENT2) c2_sentiment = comment_sentiment(c2) c2s = CommentSentiment.create(session, **c2_sentiment) # test object form for k in c1_sentiment.keys(): assert getattr(c1s, k) == c1_sentiment[k] # test relationship assert c1.sentiment == c1s # test values assert c1s.id == 1 assert c1s.polarity > 0.5 assert c1s.subjectivity > 0.8 assert c2s.id == 2 assert c2s.polarity < -0.5 assert c2s.subjectivity > 0.8