Skip to content

Commit

Permalink
Merge pull request #550 from bounswe/feature/BE-annotation-tests
Browse files Browse the repository at this point in the history
feat: Add tests for annotation and posts
  • Loading branch information
rukiyeaslan authored Dec 16, 2024
2 parents 7610dba + f7212b3 commit 6f3f7d7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
79 changes: 78 additions & 1 deletion annotations_project/annotations/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
from django.test import TestCase
from unittest.mock import patch, MagicMock
from rest_framework.test import APIRequestFactory, APITestCase, force_authenticate
from rest_framework import status
from .models import *
from .serializers import AnnotationSerializer
from .views import AnnotationViewSet
from django.contrib.auth.models import User
from django.urls import reverse
from django.conf import settings

# Create your tests here.
class AnnotationViewSetIntegrationTests(APITestCase):
def setUp(self):
self.user = User.objects.create_user(username='testuser', password='testpassword')

# Create related instances for Annotation
self.body = Body.objects.create(
type="TextualBody",
value="This info is misleading imo",
format="text/plain",
language="en"
)
self.target = Target.objects.create(
type="TextPositionSelector",
start=10,
end=50,
source="http://example.com/posts/1"
)
self.creator = Creator.objects.create(
type="Person",
name="john"
)
self.annotation = Annotation.objects.create(
body=self.body,
target=self.target,
creator=self.creator
)
self.client.login(username='testuser', password='testpassword')

def test_create_annotation(self):
"""Test creating a new annotation."""
url = reverse('annotation-list')
data = {
"body": {
"type": "TextualBody",
"value": "This info is misleading imo",
"format": "text/plain",
"language": "en"
},
"target": {
"type": "TextPositionSelector",
"start": 10,
"end": 50,
"source": "http://example.com/posts/1"
},
"creator": {
"type": "Person",
"name": "john"
}
}
response = self.client.post(url, data, format='json')

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data["target"]["source"], data["target"]["source"])

def test_get_annotations_by_source(self):
"""Test retrieving annotations by source."""
url = reverse('annotation-get-by-posts', kwargs={"source": 1})
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data[0]["target"]["source"], self.target.source)

def test_get_annotations_by_source_not_found(self):
"""Test retrieving annotations by source when none are found."""
url = reverse('annotation-get-by-posts', kwargs={"source": 999})
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data['message'], "No annotations found for the specified source.")
44 changes: 44 additions & 0 deletions backend/marketfeed/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@

from django.test import TestCase
from rest_framework.test import APITestCase, APIClient
from rest_framework import status
from unittest.mock import patch, MagicMock
from django.urls import reverse
from django.contrib.auth.models import User
from .models import *
from .serializers import *
import requests
from django.conf import settings
from django.urls import reverse
from rest_framework import status
Expand All @@ -10,6 +19,41 @@
from marketfeed.models import *
from unittest.mock import patch


class PortfolioViewSetTests(APITestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="testpassword")
self.portfolio = Portfolio.objects.create(name="Tech Portfolio", user_id=self.user)
self.client.login(username="testuser", password="testpassword")

def test_list_portfolios(self):
url = reverse('portfolio-list')
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)


class PostViewSetTests(APITestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="testpassword")
self.post = Post.objects.create(title="Test Post", content="Test content", author=self.user)
self.client.login(username="testuser", password="testpassword")

def test_list_posts(self):
url = reverse('post-list')
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_retrieve_post(self):
url = reverse('post-detail', args=[self.post.id])
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_delete_post(self):
url = reverse('post-detail', args=[self.post.id])
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)


class CurrencyViewSetTest(APITestCase):

def setUp(self):
Expand Down

0 comments on commit 6f3f7d7

Please sign in to comment.