diff --git a/annotations_project/annotations/tests.py b/annotations_project/annotations/tests.py index 7ce503c2..7d511757 100644 --- a/annotations_project/annotations/tests.py +++ b/annotations_project/annotations/tests.py @@ -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.") \ No newline at end of file diff --git a/backend/marketfeed/tests.py b/backend/marketfeed/tests.py index 167a6f15..bf7da2c2 100644 --- a/backend/marketfeed/tests.py +++ b/backend/marketfeed/tests.py @@ -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 @@ -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):