#1 begin add_coin tests

This commit is contained in:
moritz münch 2021-07-14 23:58:59 +02:00
parent 563adb2615
commit 447d93ea63
4 changed files with 116 additions and 13 deletions

View File

@ -1,6 +1,6 @@
# encoding: utf-8
#
# Copyright (C) 2020 willipink.eu
# Copyright (C) 2020-1 willipink.eu
# Author Moritz Münch moritzmuench@mailbox.org
#
# This program is free software: you can redistribute it and/or modify
@ -17,8 +17,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from json import loads
from .models import Country, Coin
def total_coin_sum():
''' add each coin_sum from each country and return the result
>>> total_coin_sum()
@ -52,3 +55,9 @@ def coin_sum_of_(country):
coin_count['total'] += value * coin_count[value]
return coin_count['total'] / 100
def get_test_response_status(test_response):
''' test helper to return response status '''
test_status = loads(test_response.content)
return test_status['status']

View File

@ -0,0 +1,92 @@
# encoding: utf-8
#
# Copyright (C) 2021 willipink.eu
# Author Moritz Münch moritzmuench@mailbox.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.http import HttpResponse
from django.test import TestCase, Client
from coinc.models import Coin, Country
from coinc.views import add_coin
from coinc.helper import get_test_response_status
class TestCoin(TestCase):
''' all tests for the coin model '''
def setUp(self):
self.test_post_data = {
'value': 1,
'year': 2050,
'country': 'fo',
'stamp': '',
'exists': 'false',
'name': '',
'found_by': '',
'found_on': '03.05.2040',
# 'date-added' '':,
# 'date-modified':,
'circulation': 0,
'bux_only': 'false',
'checked': 'false',
'ec': 'false'
}
Country.objects.create(
name_iso = 'fo',
name = 'bar'
)
def _return_status_wrong_field(self, field, data):
''' shortcut: return response status of post request to /coinc/add/coin '''
test_data = self.test_post_data
test_data[field] = data
test_response = self.client.post(f'/coinc/add/coin', test_data)
return get_test_response_status(test_response)
def test_view_add_coin_missing_post_values(self):
# all values right
self.assertEqual(self._return_status_wrong_field('value', '1'), 0)
# value not a number
self.assertEqual(self._return_status_wrong_field('value', ''), 1)
# value not the right number
self.assertEqual(self._return_status_wrong_field('value', 3), 1)
# year not a number
self.assertEqual(self._return_status_wrong_field('year', ''), 1)
# year lower limit
self.assertEqual(self._return_status_wrong_field('year', 1998), 1)
# year upper limit
self.assertEqual(self._return_status_wrong_field('year', 2099), 1)
# country not valid format xx
self.assertEqual(self._return_status_wrong_field('country', 12235235), 1)
self.assertEqual(self._return_status_wrong_field('country', 'toolong'), 1)
# country does not exist
self.assertEqual(self._return_status_wrong_field('country', 'xx'), 1)
#stamp does not exist
self.assertEqual(self._return_status_wrong_field('stamp', 'xx'), 1)
self.assertEqual(self._return_status_wrong_field('stamp', 'longer than 10 characters'), 1)
self.assertEqual(self._return_status_wrong_field('stamp', 'xx'), 1)
def test_views_add_without_year(self):
pass

View File

@ -23,13 +23,8 @@ from django.http import HttpResponse
from django.test import TestCase, Client
from coinc.models import User
from coinc.views import response, add_user
from coinc.views import add_user
CLIENT = Client()
SESSION = CLIENT.session
# session['backend'] = 'facebook'
# session['kwargs'] = {'username':'Chuck Norris','response':{'id':1}}
SESSION.save()
class TestUser(TestCase):

View File

@ -266,29 +266,36 @@ def add_coin(request):
'''
# mandatory fields value, year, country
value = request.POST['value']
try:
value = int(request.POST['value'])
value = int(value)
except ValueError:
return response(1, f"Der Münzwert '{value}' ist keine gültige Zahl.")
if value not in [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]:
return response(1, f"Der Münzwert '{value}' muss eine Zahl aus der Menge [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203] sein.")
year = request.POST['year']
try:
year = int(request.POST['year'])
year = int(year)
except ValueError:
return response(1, f"Das Jahr '{year}' ist keine gültige Zahl.")
return response(1, f"Das Jahr ist keine gültige Zahl, Format: 1234")
if year < 1999:
return response(1, f"Das Jahr '{year}' ist kleiner als 1999.")
return response(1, f"Das Jahr '{year}' ist kleiner als 1999")
if year > 2098:
return response(1, f"Bitte den 2100-Jahrhundert-Datumsbug fixen. :-)")
country = request.POST['country']
if not country.isalpha() or len(country) != 2:
return response(1, f"Das Land ist kein gültiges ISO-Format: XX")
try:
country = Country.objects.get(name_iso=request.POST['country'])
country = Country.objects.get(name_iso=country)
except Country.DoesNotExist:
return response(1, f"Das Land mit ISO-Kürzel '{name_iso}' existiert nicht.")
return response(1, f"Das Land mit ISO-Kürzel '{country}' existiert nicht")
# optional fields stamp, exists, name, found_by, found_on, circulation, buy_only, ec, checked
stamp = request.POST['stamp']
if len(stamp > 10):
return response(1, f'Die Prägerei darf max. 10 Zeichen lang sein'
try:
stamp = Stamp.objects.get(name_short=stamp)
except Stamp.DoesNotExist: