-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for create action, remove unused callback action
- Loading branch information
Showing
8 changed files
with
85 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import random | ||
from sentiment.models import Sentiment | ||
|
||
def create_sentiment(label, text, score=None, tags=None): | ||
if score is None: | ||
score = random.uniform(-1.0, 1.0) # Generate a random score between -1.0 and 1.0 | ||
if tags is None: | ||
tags = [] # Set tags to an empty list if not provided | ||
|
||
return Sentiment.objects.create( | ||
label=label, | ||
score=score, | ||
tags=tags, | ||
text=text | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from .callbackview import * | ||
from .createview import * | ||
from .detailview import * | ||
from .listview import * |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from django.test import RequestFactory | ||
from django.http import HttpResponse | ||
from sentiment.views import SentimentCreate | ||
|
||
class SentimentCreateTestCase(unittest.TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.view = SentimentCreate.as_view() | ||
|
||
@patch('sentiment.tasks.run_sentiment.s') | ||
@patch('sentiment.tasks.send_webhook.s') | ||
def test_post_with_callback_url(self, mock_send_webhook, mock_run_sentiment): | ||
|
||
request_body = json.dumps([ | ||
{'article_id': 1, 'tags': ['news'], 'text': 'Some content here'}, | ||
{'article_id': 2, 'tags': ['sports'], 'text': 'Another content'} | ||
]) | ||
request = self.factory.post('/sentiment/new?callback_url=http://example.com/callback', request_body, content_type='application/json') | ||
response = self.view(request) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
mock_run_sentiment.assert_called_once_with( | ||
[(1, ['news'], 'Some content here'), (2, ['sports'], 'Another content')] | ||
) | ||
mock_send_webhook.assert_called_once_with('http://example.com/callback') | ||
|
||
@patch('sentiment.tasks.run_sentiment.s') | ||
def test_post_without_callback_url(self, mock_run_sentiment): | ||
request_body = json.dumps([ | ||
{'article_id': 1, 'tags': ['news'], 'text': 'Some content here'}, | ||
{'article_id': 2, 'tags': ['sports'], 'text': 'Another content'} | ||
]) | ||
request = self.factory.post('/sentiment/new', request_body, content_type='application/json') | ||
response = self.view(request) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
mock_run_sentiment.assert_called_once_with( | ||
[(1, ['news'], 'Some content here'), (2, ['sports'], 'Another content')] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import json | ||
import pytest | ||
from django.test import Client | ||
from sentiment.models import Sentiment | ||
from sentiment.test_helpers import create_sentiment | ||
|
||
@pytest.mark.django_db | ||
def test_sentiment_detail_view(mocker): | ||
client = Client() | ||
sentiment = create_sentiment(label='test-label', text='test-text', tags=['test-tag']) | ||
|
||
response = client.get(f"/sentiment/{sentiment.id}/", HTTP_ACCEPT='application/json') | ||
|
||
assert response.status_code == 200 | ||
|
||
json_response = response.json() | ||
assert json_response['label'] == 'test-label' | ||
assert json_response['text'] == 'test-text' | ||
assert json_response['tags'] == ['test-tag'] | ||
assert json_response['score'] < 1.0 and json_response['score'] > -1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters