import datetime from django.test import TestCase from django.db import models from lostplaces_app.models import Place from lostplaces_app.tests.models import TestSubmittable from lostplaces_app.tests import mock_user from lostplaces_app.tests.models import TestModel from taggit.managers import TaggableManager def mock_place(): place = Place.objects.create( name='Im a place', submitted_when=datetime.datetime.now(), submitted_by=mock_user().explorer, location='Testtown', latitude=50.5, longitude=7.0, description='This is just a test, do not worry' ) place.tags.add('I a tag', 'testlocation') return place class PlaceTestCase(TestSubmittable, TestCase): model_name = 'Place' related_name = 'places' nullable = True def setUp(self): self.place = mock_place() self.object = self.place def test_name_field(self): self._test_char_field( field_name='name', min_length=10, max_length=100 ) def test_location(self): self._test_char_field( field_name='location', min_length=10, max_length=100 ) def test_latitude(self): self._test_float_field( field_name='latitude', min_value=-90, max_value=90 ) def test_longitude(self): self._test_float_field( field_name='longitude', min_value=-180, max_value=180 ) def test_decsription(self): self._test_field('description', models.TextField) def test_tags(self): self._test_field('tags', TaggableManager) def test_average_latlon(self): ''' Tests the average latitude/longitude calculation of a list of 10 places ''' place_list = [] for i in range(10): place = mock_place() place.latitude = i+1 place.longitude = i+10 place_list.append(place) avg_latlon = Place.average_latlon(place_list) self.assertEqual(avg_latlon[0], 5.5, msg='%s: average latitude missmatch' % ( self.model_name ) ) self.assertEqual(avg_latlon[1], 14.5, msg='%s: average longitude missmatch' % ( self.model_name ) ) def test_average_latlon_one_place(self): ''' Tests the average latitude/longitude calculation of a list of one place ''' place = mock_place() avg_latlon = Place.average_latlon([place]) self.assertEqual(avg_latlon[0], place.latitude, msg='%s:(one place) average latitude missmatch' % ( self.model_name ) ) self.assertEqual(avg_latlon[1], place.longitude, msg='%s: (one place) average longitude missmatch' % ( self.model_name ) ) def test_average_latlon_no_places(self): ''' Tests the average latitude/longitude calculation of an empty list ''' avg_latlon = Place.average_latlon([]) self.assertEqual(avg_latlon[0], 0, msg='%s: (no places) average latitude missmatch' % ( self.model_name ) ) self.assertEqual(avg_latlon[1], 0, msg='%s: a(no places) verage longitude missmatch' % ( self.model_name ) ) def test_str(self): place = mock_place() self.assertEqual(str(place), place.name, msg='%s __str__ should return the name' % ( self.model_name ) )