Mercurial > public > sg101
view contests/tests/test_models.py @ 943:cf9918328c64
Haystack tweaks for Django 1.7.7.
I had to upgrade to Haystack 2.3.1 to get it to work with Django
1.7.7. I also had to update the Xapian backend. But I ran into
problems.
On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms
are greater than 245 chars (or something) when indexing. So I created
a custom field that would simply omit terms greater than 64 chars and
used this field everywhere I previously used a CharField.
Secondly, the custom search form was broken now. Something changed in
the Xapian backend and exact searches stopped working. Fortunately the
auto_query (which I was using originally and broke during an upgrade)
started working again. So I cut the search form back over to doing an
auto_query. I kept the form the same (3 fields) because I didn't want
to change the form and I think it's better that way.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 13 May 2015 20:25:07 -0500 |
parents | 5977b43499f7 |
children |
line wrap: on
line source
""" Model tests for the contests application. """ import datetime from django.test import TestCase from django.contrib.auth.models import User from contests.models import Contest class ContestTestCase(TestCase): def test_creation_date(self): c = Contest(title='test', slug='test', description='test', is_public=False, end_date=datetime.datetime.now() + datetime.timedelta(days=30)) c.save() self.assertTrue(c.creation_date) self.assertTrue(datetime.datetime.now() - c.creation_date < datetime.timedelta(seconds=1)) def test_is_active(self): now = datetime.datetime.now() start = now + datetime.timedelta(days=7) end = start + datetime.timedelta(days=30) c = Contest(title='test', slug='test', description='test', is_public=False, creation_date=start, end_date=end) self.assertFalse(c.is_active()) start = now - datetime.timedelta(days=7) end = start + datetime.timedelta(days=30) c = Contest(title='test', slug='test', description='test', is_public=True, creation_date=start, end_date=end) self.assertTrue(c.is_active()) start = now - datetime.timedelta(days=7) end = start - datetime.timedelta(days=3) c = Contest(title='test', slug='test', description='test', is_public=True, creation_date=start, end_date=end) self.assertFalse(c.is_active()) def test_can_enter(self): now = datetime.datetime.now() start = now + datetime.timedelta(days=7) end = start + datetime.timedelta(days=30) c = Contest(title='test', slug='test', description='test', is_public=False, creation_date=start, end_date=end) self.assertFalse(c.can_enter()) start = now - datetime.timedelta(days=7) end = start + datetime.timedelta(days=30) c = Contest(title='test', slug='test', description='test', is_public=True, creation_date=start, end_date=end) self.assertTrue(c.can_enter()) start = now - datetime.timedelta(days=7) end = start - datetime.timedelta(days=3) c = Contest(title='test', slug='test', description='test', is_public=True, creation_date=start, end_date=end) self.assertFalse(c.can_enter()) start = now - datetime.timedelta(days=7) end = start + datetime.timedelta(days=30) user = User.objects.create_user('test_user', '', 'password') user.save() c = Contest(title='test', slug='test', description='test', is_public=True, creation_date=start, end_date=end, win_date=now) c.save() c.contestants.add(user) c.winners.add(user) self.assertFalse(c.can_enter()) start = now - datetime.timedelta(days=7) end = start - datetime.timedelta(days=3) c = Contest(title='test 2', slug='test-2', description='test 2', is_public=True, creation_date=start, end_date=end, win_date=end + datetime.timedelta(days=1)) c.save() c.contestants.add(user) c.winners.add(user) self.assertFalse(c.can_enter()) def test_pick_winners(self): now = datetime.datetime.now() start = now - datetime.timedelta(days=7) end = start - datetime.timedelta(days=3) c = Contest(title='test', slug='test', description='test', is_public=False, creation_date=start, end_date=end, num_winners=1) c.save() user1 = User.objects.create_user('test_user1', '', 'password') user1.save() user2 = User.objects.create_user('test_user2', '', 'password') user2.save() user3 = User.objects.create_user('test_user3', '', 'password') user3.save() c.contestants.add(user1, user2, user3) c.pick_winners() self.assertTrue(datetime.datetime.now() - c.win_date < datetime.timedelta(seconds=1)) winners = list(c.winners.all()) self.assertEqual(len(winners), 1) self.assertTrue(winners[0].id in [user1.id, user2.id, user3.id]) def test_pick_winners2(self): now = datetime.datetime.now() start = now - datetime.timedelta(days=7) end = start - datetime.timedelta(days=3) c = Contest(title='test', slug='test', description='test', is_public=False, creation_date=start, end_date=end, num_winners=2) c.save() user1 = User.objects.create_user('test_user1', '', 'password') user1.save() user2 = User.objects.create_user('test_user2', '', 'password') user2.save() user3 = User.objects.create_user('test_user3', '', 'password') user3.save() c.contestants.add(user1, user2, user3) c.pick_winners() self.assertTrue(datetime.datetime.now() - c.win_date < datetime.timedelta(seconds=1)) winners = list(c.winners.all()) self.assertEqual(len(winners), 2) user_ids = set([user1.id, user2.id, user3.id]) self.assertTrue(winners[0].id in user_ids) self.assertTrue(winners[1].id in user_ids) self.assertTrue(winners[0].id != winners[1].id)