Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Add unit tests to the "commentAnalysis.py" #17

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ inclusive pull requests to foster a healthier open source community.
1. [Environment Setup](#environment-setup)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Testing](#testing)
2. [Build Dataset](#build-dataset)
- [Extract Raw Data from GitHub](#extract-raw-data-from-github)
- [Annotate](#annotate)
Expand All @@ -36,6 +37,7 @@ inclusive pull requests to foster a healthier open source community.

- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Testing](#testing)

#### Prerequisites

Expand All @@ -53,7 +55,7 @@ create isolated Python environment is recommended for this project.

2. Set up ML Conversational Analytic Tool in a virtualenv
```python
python3 -m venv virtualenv-ml-conversational
python -m venv virtualenv-ml-conversational
```

3. Activate the virtualenv
Expand All @@ -70,6 +72,17 @@ create isolated Python environment is recommended for this project.
```python
pip install -r requirements.txt
```
#### Testing
6. Run all unit tests
```python
cd ml-conversational-analytic-tool
python -m unittest discover -s tests
```
7. Run an individual unit test
```python
cd ml-conversational-analytic-tool
python -m unittest tests/<file_name>
```

The libraries used within the project are available in the [requirements.txt](./requirements.txt).

Expand Down Expand Up @@ -160,7 +173,6 @@ Both `BaseCNN` and `BaseLSTM` also have prediction explanation mechanisms that c

If you have ideas on how to improve the framework to assess text conversation for constructive and inclusive
communication, we welcome your contributions!

## Documentation

Auto-generated API documentation can be found in
Expand Down
52 changes: 52 additions & 0 deletions ml-conversational-analytic-tool/tests/test_commentAnalysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest
import unittest.mock

import commentAnalysis

class TestCommentAnalysis(unittest.TestCase):

def setUp(self) -> None:
self.sentiment_analyzer = commentAnalysis.vader.SentimentIntensityAnalyzer()
self.sentiment_analyzer.polarity_scores = unittest.mock.MagicMock(return_value={"compound": 1.0})

def test_CommentAnalyzer_init(self):
analyzer = commentAnalysis.CommentAnalyzer(['test'])
self.assertEqual({'test': 0}, analyzer.word_count)

def test_analyzeComment(self):
analyzer = commentAnalysis.CommentAnalyzer(['test'])
analyzer.vader_sentiment = self.sentiment_analyzer
result = analyzer.analyzeComment("This is a test PR")
self.assertEqual({'test': 1, 'Sentiment': 1.0, 'Code Blocks': 0}, result)

def test_analyzeComment_with_empty_comment(self):
analyzer = commentAnalysis.CommentAnalyzer(['empty comment'])
analyzer.vader_sentiment = self.sentiment_analyzer
result = analyzer.analyzeComment("")
self.assertEqual({'empty comment': 0, 'Sentiment': 1.0, 'Code Blocks': 0}, result)

def test_analyzeComment_with_codeBlocks(self):
analyzer = commentAnalysis.CommentAnalyzer(['code block test'])
analyzer.vader_sentiment = self.sentiment_analyzer
result = analyzer.analyzeComment("```This patch has blocks```, ```This is second block```")
self.assertEqual({'code block test': 0, 'Sentiment': 1.0, 'Code Blocks': 2}, result)

def test_preProcess(self):
analyzer = commentAnalysis.CommentAnalyzer(['preProcess test'])
result = analyzer.preProcess("THIS is a TEST COMMENT")
self.assertEqual("this is a test comment", result)

def test_countWords(self):
analyzer = commentAnalysis.CommentAnalyzer(['count words test'])
result = analyzer.countWords("This is a test comment")
self.assertEqual({'count words test': 0}, result)

def test_getSentiment(self):
analyzer = commentAnalysis.CommentAnalyzer(['sentiment test'])
result = analyzer.getSentiment("This is a very good PR")
self.assertEqual(float, type(result))

def test_getCodeBlockCount(self):
analyzer = commentAnalysis.CommentAnalyzer(['test'])
result = analyzer.getCodeBlockCount("```This comment has code block``` ``` this is the 2nd code block```")
self.assertEqual(2, result)