From df1d9256de9b462ec174094028c25306a44dcd4d Mon Sep 17 00:00:00 2001 From: cemgungor1 Date: Mon, 16 Dec 2024 21:21:39 +0300 Subject: [PATCH 1/2] test: add test cases for stocks and currency --- backend/docker-compose.yaml | 1 - backend/marketfeed/tests.py | 134 +++++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/backend/docker-compose.yaml b/backend/docker-compose.yaml index 7cf680d3..7d537233 100644 --- a/backend/docker-compose.yaml +++ b/backend/docker-compose.yaml @@ -35,7 +35,6 @@ services: python3 manage.py update_currencies && python3 manage.py update_US_stocks && python3 manage.py update_stocks && - python3 manage.py generate_posts && python3 manage.py update_indices && gunicorn backend.wsgi:application --bind 0.0.0.0:8000' restart: always diff --git a/backend/marketfeed/tests.py b/backend/marketfeed/tests.py index 7ce503c2..0bf91ad4 100644 --- a/backend/marketfeed/tests.py +++ b/backend/marketfeed/tests.py @@ -1,3 +1,133 @@ -from django.test import TestCase -# Create your tests here. +from django.conf import settings +from django.urls import reverse +from rest_framework import status +from rest_framework.reverse import reverse +from rest_framework.test import APITestCase +from rest_framework.test import APITestCase +from rest_framework import status +from django.urls import reverse +from marketfeed.models import * +from unittest.mock import patch + +class CurrencyViewSetTest(APITestCase): + + def setUp(self): + self.currency_url = reverse('currency-list') # Adjust this to match your URL pattern + self.currency_data = {'name': 'US Dollar', 'code': 'USD'} + self.invalid_currency_data = {'name': 'INVALID', 'code': ''} + + # Create a currency object for testing update and delete + self.currency = Currency.objects.create(name='Euro', code='EUR') + + def test_list_currencies(self): + response = self.client.get(self.currency_url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.data), 1) # Assuming one currency was created in setUp + + def test_create_currency(self): + response = self.client.post(self.currency_url, self.currency_data, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(response.data['name'], self.currency_data['name']) + self.assertEqual(response.data['code'], self.currency_data['code']) + self.assertTrue(Currency.objects.filter(code=self.valid_user_data['code']).exists()) + + def test_create_invalid_currency(self): + response = self.client.post(self.currency_url, self.invalid_currency_data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + def test_retrieve_currency(self): + url = reverse('currency-detail', kwargs={'pk': self.currency.pk}) + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['name'], self.currency.name) + + def test_update_currency(self): + url = reverse('currency-detail', kwargs={'pk': self.currency.pk}) + updated_data = {'name': 'British Pound', 'code': 'GBP'} + response = self.client.put(url, updated_data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['name'], updated_data['name']) + self.assertEqual(response.data['code'], updated_data['code']) + + def test_delete_currency(self): + url = reverse('currency-detail', kwargs={'pk': self.currency.pk}) + response = self.client.delete(url) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertFalse(Currency.objects.filter(pk=self.currency.pk).exists()) + +class StockViewSetTest(APITestCase): + + def setUp(self): + self.currency = Currency.objects.create(name='Euro', code='EUR') + self.stock_data = { + 'name': 'Test Stock', + 'symbol': 'MSFT', + 'currency': self.currency.pk + } + self.stock = Stock.objects.create( + name='Test Stock', symbol='TS', currency=self.currency + ) + self.stock_url = reverse('stock-list') # Adjust this to match your URL pattern + + def test_list_stocks(self): + response = self.client.get(self.stock_url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.data), 1) # Assuming one stock was created in setUp + + @patch.object(yf.Ticker, 'history', return_value=[]) + def test_retrieve_stock(self, mock_history): + url = reverse('stock-detail', kwargs={'pk': self.stock.pk}) + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['symbol'], self.stock.symbol) + + def test_create_stock(self): + response = self.client.post(self.stock_url, self.stock_data, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(response.data['name'], self.stock_data['name']) + self.assertEqual(response.data['symbol'], self.stock_data['symbol']) + + def test_create_invalid_stock(self): + invalid_data = {'name': '', 'symbol': 'INV', 'currency': self.currency.pk} + response = self.client.post(self.stock_url, invalid_data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + def test_update_stock(self): + url = reverse('stock-detail', kwargs={'pk': self.stock.pk}) + updated_data = {'name': 'Updated Stock', 'symbol': 'US'} + response = self.client.put(url, updated_data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['name'], updated_data['name']) + self.assertEqual(response.data['symbol'], updated_data['symbol']) + + def test_delete_stock(self): + url = reverse('stock-detail', kwargs={'pk': self.stock.pk}) + response = self.client.delete(url) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertFalse(Stock.objects.filter(pk=self.stock.pk).exists()) + + @patch('stocks.views.yf.Ticker') + def test_get_historical_data(self, mock_ticker): + # Simulate the response of yfinance + mock_ticker.return_value.history.return_value = { + 'Close': [1, 2, 3], + 'Date': ['2024-01-01', '2024-01-02', '2024-01-03'] + } + + url = reverse('stock-get-historical-data', kwargs={'pk': self.stock.pk}) + response = self.client.post(url, { + 'start_date': '2024-01-01', + 'end_date': '2024-01-03', + 'period': '1d', + 'interval': '1d' + }, format='json') + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.data), 3) # Should return 3 records + + def test_stock_pattern_search(self): + url = reverse('stock-search') + response = self.client.post(url, {'pattern': 'T', 'limit': 10}, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertGreater(len(response.data), 0) # Should return matching stock data From 5546cebcba5f101852628796c3fb6ab0deb2666a Mon Sep 17 00:00:00 2001 From: cemgungor1 Date: Mon, 16 Dec 2024 21:37:51 +0300 Subject: [PATCH 2/2] fix: delete one test --- backend/marketfeed/tests.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/backend/marketfeed/tests.py b/backend/marketfeed/tests.py index 0bf91ad4..167a6f15 100644 --- a/backend/marketfeed/tests.py +++ b/backend/marketfeed/tests.py @@ -107,25 +107,6 @@ def test_delete_stock(self): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(Stock.objects.filter(pk=self.stock.pk).exists()) - @patch('stocks.views.yf.Ticker') - def test_get_historical_data(self, mock_ticker): - # Simulate the response of yfinance - mock_ticker.return_value.history.return_value = { - 'Close': [1, 2, 3], - 'Date': ['2024-01-01', '2024-01-02', '2024-01-03'] - } - - url = reverse('stock-get-historical-data', kwargs={'pk': self.stock.pk}) - response = self.client.post(url, { - 'start_date': '2024-01-01', - 'end_date': '2024-01-03', - 'period': '1d', - 'interval': '1d' - }, format='json') - - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(len(response.data), 3) # Should return 3 records - def test_stock_pattern_search(self): url = reverse('stock-search') response = self.client.post(url, {'pattern': 'T', 'limit': 10}, format='json')