fix #46
This commit is contained in:
parent
6b77b35213
commit
da4c580c18
@ -18,6 +18,7 @@
|
||||
|
||||
|
||||
from json import loads
|
||||
from json.decoder import JSONDecodeError
|
||||
|
||||
from .models import Country, Coin
|
||||
|
||||
@ -59,5 +60,9 @@ def coin_sum_of_(country):
|
||||
|
||||
def get_test_response_status(test_response):
|
||||
''' test helper to return response status '''
|
||||
# if test_response =
|
||||
try:
|
||||
test_status = loads(test_response.content)
|
||||
return test_status['status']
|
||||
except JSONDecodeError:
|
||||
return 1
|
||||
|
@ -22,7 +22,7 @@ from copy import deepcopy
|
||||
from django.http import HttpResponse
|
||||
from django.test import TestCase, Client
|
||||
|
||||
from coinc.models import Coin, Country, Stamp
|
||||
from coinc.models import Coin, Country, Stamp, User
|
||||
from coinc.views import add_coin
|
||||
from coinc.helper import get_test_response_status
|
||||
|
||||
@ -36,7 +36,7 @@ class TestCoin(TestCase):
|
||||
'year': 2050,
|
||||
'country': 'fo',
|
||||
'stamp': '',
|
||||
'exists': 'false',
|
||||
'exists': 'true',
|
||||
'name': '',
|
||||
'found_by': '',
|
||||
'found_on': '03.05.2040',
|
||||
@ -55,9 +55,12 @@ class TestCoin(TestCase):
|
||||
name_short = '10 letters',
|
||||
name = 'test name'
|
||||
)
|
||||
User.objects.create(
|
||||
name = 'test_user'
|
||||
)
|
||||
|
||||
|
||||
def _return_status_wrong_field(self, field, data=None):
|
||||
def _add_coin(self, field, data=None):
|
||||
''' shortcut: return response status of post request to /coinc/add/coin '''
|
||||
|
||||
test_data = deepcopy(self.test_post_data)
|
||||
@ -68,61 +71,90 @@ class TestCoin(TestCase):
|
||||
test_data.pop(field)
|
||||
except KeyError:
|
||||
pass
|
||||
test_response = self.client.post(
|
||||
f"/coinc/add/coin/{test_data['country']}/{test_data['year']}/{test_data['value']}",
|
||||
test_data)
|
||||
request_uri = f"/coinc/add/coin/{test_data['country']}/{test_data['year']}/{test_data['value']}"
|
||||
test_response = self.client.post(request_uri, test_data)
|
||||
return get_test_response_status(test_response)
|
||||
|
||||
|
||||
def test_view_add_coin(self):
|
||||
|
||||
## value
|
||||
# Note: These test do not need to run as a missing value will get caught by
|
||||
# urls.py regex
|
||||
# value missing
|
||||
# self.assertEqual(self._return_status_wrong_field('value', None), 1)
|
||||
# self.assertEqual(self._add_coin('value', None), 1)
|
||||
# value not a number
|
||||
# self.assertEqual(self._return_status_wrong_field('value', ''), 1)
|
||||
# self.assertEqual(self._return_status_wrong_field('value', 'some string'), 1)
|
||||
# self.assertEqual(self._add_coin('value', ''), 1)
|
||||
self.assertEqual(self._add_coin('value', 'some string'), 1)
|
||||
# value wrong number
|
||||
# self.assertEqual(self._return_status_wrong_field('value', 3), 1)
|
||||
# self.assertEqual(self._return_status_wrong_field('value', 0.02), 1)
|
||||
# self.assertEqual(self._return_status_wrong_field('value', -3), 1)
|
||||
self.assertEqual(self._add_coin('value', 3), 1)
|
||||
self.assertEqual(self._add_coin('value', 0.02), 1)
|
||||
self.assertEqual(self._add_coin('value', -3), 1)
|
||||
# value ok
|
||||
for value in [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]:
|
||||
self.assertEqual(self._return_status_wrong_field('value', value), 0)
|
||||
self.assertEqual(self._add_coin('value', value), 0)
|
||||
|
||||
## year
|
||||
# Note: These test do not need to run as a missing year will get caught by
|
||||
# urls.py regex
|
||||
# year missing
|
||||
# self.assertEqual(self._return_status_wrong_field('year', None), 1)
|
||||
# year not a number self.assertEqual(self._return_status_wrong_field('year', ''), 1)
|
||||
# self.assertEqual(self._add_coin('year', None), 1)
|
||||
# year empty
|
||||
# self.assertEqual(self._add_coin('year', ''), 1)
|
||||
# year lower limit
|
||||
# self.assertEqual(self._return_status_wrong_field('year', 1998), 1)
|
||||
self.assertEqual(self._add_coin('year', 1998), 1)
|
||||
# year upper limit
|
||||
# self.assertEqual(self._return_status_wrong_field('year', 2099), 1)
|
||||
self.assertEqual(self._add_coin('year', 2099), 1)
|
||||
# year ok
|
||||
for year in range(1999, 2098):
|
||||
self.assertEqual(self._return_status_wrong_field('year', year), 0)
|
||||
self.assertEqual(self._add_coin('year', year), 0)
|
||||
|
||||
## country
|
||||
# Note: These test do not need to run as a missing country will get caught by
|
||||
# urls.py regex
|
||||
# country missing
|
||||
# self.assertEqual(self._return_status_wrong_field('country', None), 1)
|
||||
# self.assertEqual(self._add_coin('country', None), 1)
|
||||
# country empty
|
||||
# self.assertEqual(self._add_coin('country', ''), 1)
|
||||
# country not valid iso format
|
||||
# self.assertEqual(self._return_status_wrong_field('country', 12235235), 1)
|
||||
# self.assertEqual(self._return_status_wrong_field('country', 'toolong'), 1)
|
||||
self.assertEqual(self._add_coin('country', 12235235), 1)
|
||||
self.assertEqual(self._add_coin('country', 'toolong'), 1)
|
||||
# country does not exist
|
||||
self.assertEqual(self._return_status_wrong_field('country', 'xx'), 1)
|
||||
self.assertEqual(self._add_coin('country', 'xx'), 1)
|
||||
# country ok
|
||||
self.assertEqual(self._return_status_wrong_field('country', 'fo'), 0)
|
||||
self.assertEqual(self._add_coin('country', 'fo'), 0)
|
||||
|
||||
|
||||
## stamp
|
||||
# stamp missing
|
||||
self.assertEqual(self._return_status_wrong_field('stamp', None), 0)
|
||||
self.assertEqual(self._add_coin('stamp', None), 0)
|
||||
# stamp title length
|
||||
self.assertEqual(self._return_status_wrong_field('stamp', '10 letters'), 0)
|
||||
self.assertEqual(self._return_status_wrong_field('stamp', '11 letters '), 1)
|
||||
self.assertEqual(self._add_coin('stamp', '10 letters'), 0)
|
||||
self.assertEqual(self._add_coin('stamp', '11 letters '), 1)
|
||||
# stamp does not exist
|
||||
self.assertEqual(self._return_status_wrong_field('stamp', 'xx'), 1)
|
||||
self.assertEqual(self._add_coin('stamp', 'xx'), 1)
|
||||
|
||||
## found_by
|
||||
self.assertEqual(self._add_coin('found_by', 'test_user'), 0)
|
||||
self.assertEqual(self._add_coin('found_by', ''), 0)
|
||||
self.assertEqual(self._add_coin('found_by', 'not existing user'), 1)
|
||||
|
||||
## found_on
|
||||
self.assertEqual(self._add_coin('found_on', '01.01.2087'), 0)
|
||||
# not a date
|
||||
self.assertEqual(self._add_coin('found_on', '99.01.2087'), 1)
|
||||
self.assertEqual(self._add_coin('found_on', '23'), 1)
|
||||
self.assertEqual(self._add_coin('found_on', 'asdf'), 1)
|
||||
|
||||
## circulation
|
||||
self.assertEqual(self._add_coin('circulation', ''), 0)
|
||||
# not a number
|
||||
self.assertEqual(self._add_coin('circulation', ' some string'), 1)
|
||||
# negative
|
||||
self.assertEqual(self._add_coin('circulation', -1), 1)
|
||||
# unrealistic
|
||||
self.assertEqual(self._add_coin('circulation', 1000000001), 1)
|
||||
|
||||
## exists
|
||||
# missing exists
|
||||
self.assertEqual(self._return_status_wrong_field('exists', None), 0)
|
||||
self.assertEqual(self._add_coin('exists', None), 0)
|
||||
|
@ -319,11 +319,11 @@ def add_coin(request, country, year, value):
|
||||
found_on = found_on.strftime('%Y-%m-%d')
|
||||
except ValueError:
|
||||
return response(1, f"Das Datum '{found_on}' entspricht nicht dem Format 'DD.MM.YYYY'.")
|
||||
else:
|
||||
found_on = date.today().strftime('%Y-%m-%d')
|
||||
|
||||
circulation = request.POST.get('circulation', 0)
|
||||
circulation = 0 if circulation == '' else circulation
|
||||
circulation = request.POST.get('circulation', '0')
|
||||
# Note: dict.get('foo', default) will return default, when it is empty,
|
||||
# therefore we manually catch the case that circulation = ''
|
||||
circulation = '0' if circulation == '' else circulation
|
||||
try:
|
||||
circulation = int(circulation.replace('.', ''))
|
||||
except ValueError:
|
||||
@ -345,6 +345,7 @@ def add_coin(request, country, year, value):
|
||||
stamp=stamp)
|
||||
coin.name = name
|
||||
coin.found_by = found_by
|
||||
if found_on:
|
||||
coin.found_on = found_on
|
||||
coin.circulation = circulation
|
||||
coin.buy_only = buy_only
|
||||
@ -354,6 +355,8 @@ def add_coin(request, country, year, value):
|
||||
coin.save(force_update=True)
|
||||
|
||||
except Coin.DoesNotExist:
|
||||
if not found_on:
|
||||
found_on = date.today().strftime('%Y-%m-%d')
|
||||
Coin.objects.create(
|
||||
value = value,
|
||||
year = year,
|
||||
|
Loading…
x
Reference in New Issue
Block a user