def test_create_tracker_error(self): self._create_test_user(self.TEST_USER, self.TEST_USER_PASSWORD) client = Client() logged_in = client.login(username=self.TEST_USER, password=self.TEST_USER_PASSWORD) data = { 'name': 'testName', 'unit': 'g', 'description': 'testDescription' } tracker = Track(name=data['name'], unit=data['unit'], description=data['description']) tracker.save() response = client.post(reverse('tracker:create_tracker'), data=data) content = str(response.content) trackers_header = "<h1>Trackers</h1>" trackers_list = '<ul id="trackers-list" class="list-group">' error_msg = "Tracking with name '%s' already exists" % ( data['name']) self.assertEqual(response.status_code, 200) self.assertEqual(trackers_header in content, True) self.assertEqual(trackers_list in content, True) self.assertEqual(error_msg in content, True)
def test_tracker_create_record_get(self): tracker = Track(name='testName', unit='g', description='testDescription') tracker.save() client = Client() response = client.get('/tracker/%s/create_record' % tracker.id) content = str(response.content) trackers_header = "<h2>%s</h2>" % tracker.name self.assertEqual(response.status_code, 200) self.assertEqual(trackers_header in content, True)
def test_tracker_create_record(self): tracker = Track(name='testName', unit='g', description='testDescription') tracker.save() client = Client() data = {'value': 15, 'datetime': '2050-01-01 12:30'} response = client.post('/tracker/%s/create_record' % tracker.id, data, follow=True) content = str(response.content) trackers_header = "<h2>%s</h2>" % tracker.name self.assertEqual(response.status_code, 200) self.assertEqual(trackers_header in content, True) self.assertEqual(str(data['value']) in content, True) self.assertEqual(data['datetime'] in content, True)
def test_tracker_create_record_error(self): self._create_test_user(self.TEST_USER, self.TEST_USER_PASSWORD) tracker = Track(name='testName', unit='g', description='testDescription') tracker.save() client = Client() logged_in = client.login(username=self.TEST_USER, password=self.TEST_USER_PASSWORD) data = {'value': 15, 'datetime': '2050'} response = client.post('/tracker/%s/create_record' % tracker.id, data, follow=True) content = str(response.content) trackers_header = "<h2>%s</h2>" % tracker.name error_msg = "Enter a valid date/time." self.assertEqual(response.status_code, 200) self.assertEqual(trackers_header in content, True) self.assertEqual(error_msg in content, True)
def test_create_record(self): track = Track(name='name', unit='unit', description='description') value = 11.1 date = datetime.datetime.now() record = Record(track=track, value=value, date=date) self.assertEqual(str(track), str(record.track)) self.assertEqual(value, record.value) self.assertEqual(date, record.date) expected_string = "Record{value='%s', date='%s'}" % ( value, date.strftime('%Y-%m-%d')) self.assertEqual(expected_string, str(record)) expected_repr = "<Record: Record{value='%s', date='%s'}>" % ( value, date.strftime('%Y-%m-%d')) self.assertEqual(expected_repr, repr(record))
def test_create_track(self): name = 'Test name' unit = 'Test unit' description = 'Test description' track = Track(name=name, unit=unit, description=description) self.assertEqual(name, track.name) self.assertEqual(unit, track.unit) self.assertEqual(description, track.description) expected_string = "Track{name='%s', unit='%s', description='%s'}" % ( name, unit, description) self.assertEqual(expected_string, str(track)) expected_repr = "<Track: Track{name='%s', unit='%s', description='%s'}>" % ( name, unit, description) self.assertEqual(expected_repr, repr(track))
def create_tracker(request): new_track_form = TrackerForm(request.POST) if request.method == 'POST' and new_track_form.is_valid(): tracker = Track() tracker.name = request.POST['name'] tracker.unit = request.POST['unit'] tracker.description = request.POST['description'] try: with transaction.atomic(): tracker.save() except IntegrityError: return render( request, 'tracker/trackers.html', { 'trackers_list': Track.objects.all(), 'form': TrackerForm(), 'form_error': "Tracking with name '%s' already exists" % (request.POST['name']), }) return HttpResponseRedirect(reverse('tracker:trackers')) else: fields = { 'trackers_list': Track.objects.all(), 'form': new_track_form, } if new_track_form.errors.get('name'): fields['name_error'] = new_track_form.errors.get('name')[0] if new_track_form.errors.get('unit'): fields['unit_error'] = new_track_form.errors.get('unit')[0] if new_track_form.errors.get('description'): fields['description_error'] = new_track_form.errors.get( 'description')[0] return render(request, 'tracker/trackers.html', fields)