Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
wbcurrency / currency / tests.py
Size: Mime:
import pytest
from django.core.management import call_command
from django.test import TestCase
from rest_framework.test import APIRequestFactory

from currency.models import Currency
from currency.serializers import CurrencyModelSerializer
from currency.views.api.currency import CurrencyViewSet


@pytest.mark.django_db
class TestCurrency:
    def setup(self):
        call_command('loaddata', 'currency/fixtures/currency.yaml')

    def test_to_str(self):
        currency = Currency.objects.first()
        assert str(currency) == f'{currency.symbol} ({currency.title})'

    def test_convert(self):
        currency = Currency.objects.first()
        other_currency = Currency.objects.last()

        value = currency.convert(other_currency)

        assert value is not None


@pytest.mark.django_db
class TestCurrencyViewSet:
    def setup(self):
        call_command('loaddata', 'currency/fixtures/currency.yaml')

    def test_detail(self):
        currency = Currency.objects.first()
        request = APIRequestFactory().get('')
        currency_detail = CurrencyViewSet.as_view({'get': 'retrieve'})

        response = currency_detail(request, pk=currency.pk)
        assert response.status_code == 200
        assert response.data['id'] == currency.id

    def test_list(self):
        currencies = Currency.objects.all()
        request = APIRequestFactory().get('')
        currency_list = CurrencyViewSet.as_view({'get': 'list'})

        response = currency_list(request)
        assert response.status_code == 200
        assert len(response.data) == currencies.count()


@pytest.mark.django_db
class TestCurrencySerializer:
    def setup(self):
        call_command('loaddata', 'currency/fixtures/currency.yaml')
        

    def test_contains_expected_fields(self):
        currency = Currency.objects.first()
        serializer = CurrencyModelSerializer(currency)

        assert set(serializer.data.keys()) == set(['id', 'title', 'symbol', 'key'])