This commit is contained in:
Moritz Münch 2022-01-17 01:28:01 +01:00
parent 6b77b35213
commit da4c580c18
3 changed files with 75 additions and 35 deletions

View File

@ -18,6 +18,7 @@
from json import loads from json import loads
from json.decoder import JSONDecodeError
from .models import Country, Coin from .models import Country, Coin
@ -59,5 +60,9 @@ def coin_sum_of_(country):
def get_test_response_status(test_response): def get_test_response_status(test_response):
''' test helper to return response status ''' ''' test helper to return response status '''
test_status = loads(test_response.content) # if test_response =
return test_status['status'] try:
test_status = loads(test_response.content)
return test_status['status']
except JSONDecodeError:
return 1

View File

@ -22,7 +22,7 @@ from copy import deepcopy
from django.http import HttpResponse from django.http import HttpResponse
from django.test import TestCase, Client 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.views import add_coin
from coinc.helper import get_test_response_status from coinc.helper import get_test_response_status
@ -36,7 +36,7 @@ class TestCoin(TestCase):
'year': 2050, 'year': 2050,
'country': 'fo', 'country': 'fo',
'stamp': '', 'stamp': '',
'exists': 'false', 'exists': 'true',
'name': '', 'name': '',
'found_by': '', 'found_by': '',
'found_on': '03.05.2040', 'found_on': '03.05.2040',
@ -55,9 +55,12 @@ class TestCoin(TestCase):
name_short = '10 letters', name_short = '10 letters',
name = 'test name' 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 ''' ''' shortcut: return response status of post request to /coinc/add/coin '''
test_data = deepcopy(self.test_post_data) test_data = deepcopy(self.test_post_data)
@ -68,61 +71,90 @@ class TestCoin(TestCase):
test_data.pop(field) test_data.pop(field)
except KeyError: except KeyError:
pass pass
test_response = self.client.post( request_uri = f"/coinc/add/coin/{test_data['country']}/{test_data['year']}/{test_data['value']}"
f"/coinc/add/coin/{test_data['country']}/{test_data['year']}/{test_data['value']}", test_response = self.client.post(request_uri, test_data)
test_data)
return get_test_response_status(test_response) return get_test_response_status(test_response)
def test_view_add_coin(self): def test_view_add_coin(self):
## value ## value
# Note: These test do not need to run as a missing value will get caught by
# urls.py regex
# value missing # value missing
# self.assertEqual(self._return_status_wrong_field('value', None), 1) # self.assertEqual(self._add_coin('value', None), 1)
# value not a number # value not a number
# self.assertEqual(self._return_status_wrong_field('value', ''), 1) # self.assertEqual(self._add_coin('value', ''), 1)
# self.assertEqual(self._return_status_wrong_field('value', 'some string'), 1) self.assertEqual(self._add_coin('value', 'some string'), 1)
# value wrong number # value wrong number
# self.assertEqual(self._return_status_wrong_field('value', 3), 1) self.assertEqual(self._add_coin('value', 3), 1)
# self.assertEqual(self._return_status_wrong_field('value', 0.02), 1) self.assertEqual(self._add_coin('value', 0.02), 1)
# self.assertEqual(self._return_status_wrong_field('value', -3), 1) self.assertEqual(self._add_coin('value', -3), 1)
# value ok # value ok
for value in [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]: 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 ## year
# Note: These test do not need to run as a missing year will get caught by
# urls.py regex
# year missing # year missing
# self.assertEqual(self._return_status_wrong_field('year', None), 1) # self.assertEqual(self._add_coin('year', None), 1)
# year not a number self.assertEqual(self._return_status_wrong_field('year', ''), 1) # year empty
# self.assertEqual(self._add_coin('year', ''), 1)
# year lower limit # year lower limit
# self.assertEqual(self._return_status_wrong_field('year', 1998), 1) self.assertEqual(self._add_coin('year', 1998), 1)
# year upper limit # year upper limit
# self.assertEqual(self._return_status_wrong_field('year', 2099), 1) self.assertEqual(self._add_coin('year', 2099), 1)
# year ok # year ok
for year in range(1999, 2098): 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 ## country
# Note: These test do not need to run as a missing country will get caught by
# urls.py regex
# country missing # 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 # country not valid iso format
# self.assertEqual(self._return_status_wrong_field('country', 12235235), 1) self.assertEqual(self._add_coin('country', 12235235), 1)
# self.assertEqual(self._return_status_wrong_field('country', 'toolong'), 1) self.assertEqual(self._add_coin('country', 'toolong'), 1)
# country does not exist # country does not exist
self.assertEqual(self._return_status_wrong_field('country', 'xx'), 1) self.assertEqual(self._add_coin('country', 'xx'), 1)
# country ok # country ok
self.assertEqual(self._return_status_wrong_field('country', 'fo'), 0) self.assertEqual(self._add_coin('country', 'fo'), 0)
## stamp ## stamp
# stamp missing # stamp missing
self.assertEqual(self._return_status_wrong_field('stamp', None), 0) self.assertEqual(self._add_coin('stamp', None), 0)
# stamp title length # stamp title length
self.assertEqual(self._return_status_wrong_field('stamp', '10 letters'), 0) self.assertEqual(self._add_coin('stamp', '10 letters'), 0)
self.assertEqual(self._return_status_wrong_field('stamp', '11 letters '), 1) self.assertEqual(self._add_coin('stamp', '11 letters '), 1)
# stamp does not exist # 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 ## exists
# missing exists # missing exists
self.assertEqual(self._return_status_wrong_field('exists', None), 0) self.assertEqual(self._add_coin('exists', None), 0)

View File

@ -319,11 +319,11 @@ def add_coin(request, country, year, value):
found_on = found_on.strftime('%Y-%m-%d') found_on = found_on.strftime('%Y-%m-%d')
except ValueError: except ValueError:
return response(1, f"Das Datum '{found_on}' entspricht nicht dem Format 'DD.MM.YYYY'.") 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 = request.POST.get('circulation', '0')
circulation = 0 if circulation == '' else circulation # 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: try:
circulation = int(circulation.replace('.', '')) circulation = int(circulation.replace('.', ''))
except ValueError: except ValueError:
@ -345,7 +345,8 @@ def add_coin(request, country, year, value):
stamp=stamp) stamp=stamp)
coin.name = name coin.name = name
coin.found_by = found_by coin.found_by = found_by
coin.found_on = found_on if found_on:
coin.found_on = found_on
coin.circulation = circulation coin.circulation = circulation
coin.buy_only = buy_only coin.buy_only = buy_only
coin.checked = checked coin.checked = checked
@ -354,6 +355,8 @@ def add_coin(request, country, year, value):
coin.save(force_update=True) coin.save(force_update=True)
except Coin.DoesNotExist: except Coin.DoesNotExist:
if not found_on:
found_on = date.today().strftime('%Y-%m-%d')
Coin.objects.create( Coin.objects.create(
value = value, value = value,
year = year, year = year,