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

Commit

Permalink
Add unit tests to the "commentAnalysis.py" (#17)
Browse files Browse the repository at this point in the history
* Add unit tests to the "commentAnalysis.py"

1.'unittest' framework of python is used to develop unit tests
2. adds the __init__.py to have standard packaging template

* Add documentation to run unit tests

update the README.md with the commands to run unit tests
  • Loading branch information
pramodrj07 authored Jan 20, 2022
1 parent 69d04d4 commit 5262fea
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
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)

0 comments on commit 5262fea

Please sign in to comment.