def test_strict_chain(self): from dj.chain import chain class extra_list(list): def extra(self, *args, **kwargs): raise Exception("extra called") books = extra_list(self.books) default_media = chain( self.Video.objects.all(), self.Song.objects.all(), books, ) with self.assertRaises(Exception): default_media.extra(where=['id IN (2, 4, 6)']) strict_media = chain( self.Video.objects.all(), self.Song.objects.all(), books, strict=True, ) strict_media_extra = strict_media.extra(where=['id IN (2, 4, 6)']) self.assertEqual(strict_media_extra[0].title, 'Baby') self.assertEqual(strict_media_extra[1].title, 'Waka Waka') self.assertEqual(strict_media_extra[2].title, 'Clocks') self.assertEqual(strict_media_extra[3].title, 'Spectrum') self.assertEqual(strict_media_extra[4].title, 'A Tale of Two Cities') self.assertEqual(strict_media_extra[5].title, 'Don Quixote')
def test_chain_simple_copy(self): from dj.chain import chain c = chain((1, 2), [3, 4], "56") c.xform = lambda v: int(v) c.xfilter = lambda v: int(v) % 2 == 0 c2 = c.copy(*c.iterables) self.test_chain_simple(c2)
def test_xvalues(self): from dj.chain import chain media = chain(self.Video.objects.all(), self.books) media_values = media.values('title') self.assertEqual(media_values[0]['title'], 'Gangnam Style') self.assertEqual(media_values[1]['title'], 'Baby') self.assertEqual(media_values[2]['title'], 'Bad Romance') self.assertEqual(media_values[3]['title'], 'Waka Waka') self.assertEqual(media_values[4]['title'], 'A Tale of Two Cities') self.assertEqual(media_values[5]['title'], 'Don Quixote') media_values_list = media.values_list('title', 'author') self.assertEqual(media_values_list[0][0], 'Gangnam Style') self.assertEqual(media_values_list[0][1], 'Psy') self.assertEqual(media_values_list[1][0], 'Baby') self.assertEqual(media_values_list[1][1], 'Justin Bieber') self.assertEqual(media_values_list[2][0], 'Bad Romance') self.assertEqual(media_values_list[2][1], 'Lady Gaga') self.assertEqual(media_values_list[3][0], 'Waka Waka') self.assertEqual(media_values_list[3][1], 'Shakira') self.assertEqual(media_values_list[4][0], 'A Tale of Two Cities') self.assertEqual(media_values_list[4][1], 'Charles Dickens') self.assertEqual(media_values_list[5][0], 'Don Quixote') self.assertEqual(media_values_list[5][1], 'Miguel de Cervantes') media_values_list2 = media.values_list('author', flat=True) self.assertEqual(media_values_list2[0], 'Psy') self.assertEqual(media_values_list2[1], 'Justin Bieber') self.assertEqual(media_values_list2[2], 'Lady Gaga') self.assertEqual(media_values_list2[3], 'Shakira') self.assertEqual(media_values_list2[4], 'Charles Dickens') self.assertEqual(media_values_list2[5], 'Miguel de Cervantes')
def test_basic_chain(self): from dj.chain import chain media = chain(self.Video.objects.all(), self.Song.objects.all()) self.assertEqual(media.count(), 8) self.assertEqual( list(media.filter(duration__gt=250)), list( self.Video.objects.filter(title__in=( 'Gangnam Style', 'Bad Romance')).order_by('-title')) + list( self.Song.objects.filter( title__in=('Clocks', 'Madness')).order_by('title'))) self.assertEqual(media.filter(duration__gt=250).count(), 4) self.assertEqual( media.filter(duration__gt=250)[1], self.Video.objects.get(title='Bad Romance')) self.assertEqual(list( media[3:6]), [self.Video.objects.get(title='Waka Waka')] + list( self.Song.objects.filter(artist__in=( 'Gotye feat. Kimbra', 'Coldplay')).order_by('-artist'))) self.assertEqual(list(media[1::3]), [ self.Video.objects.get(title='Baby'), self.Song.objects.get(artist='Gotye feat. Kimbra'), self.Song.objects.get(title='Spectrum'), ]) self.assertEqual(list(media[:5:4]), [ self.Video.objects.get(title='Gangnam Style'), self.Song.objects.get(artist='Gotye feat. Kimbra'), ])
def test_basic_chain(self): from dj.chain import chain media = chain(self.Video.objects.all(), self.Song.objects.all()) self.assertEqual(media.count(), 8) self.assertEqual( list(media.filter(duration__gt=250)), list(self.Video.objects.filter(title__in=('Gangnam Style', 'Bad Romance')).order_by('-title')) + list(self.Song.objects.filter(title__in=('Clocks', 'Madness')).order_by('title')) ) self.assertEqual(media.filter(duration__gt=250).count(), 4) self.assertEqual( media.filter(duration__gt=250)[1], self.Video.objects.get(title='Bad Romance') ) self.assertEqual( list(media[3:6]), [self.Video.objects.get(title='Waka Waka')] + list(self.Song.objects.filter(artist__in=('Gotye feat. Kimbra', 'Coldplay')).order_by('-artist')) ) self.assertEqual(list(media[1::3]), [ self.Video.objects.get(title='Baby'), self.Song.objects.get(artist='Gotye feat. Kimbra'), self.Song.objects.get(title='Spectrum'), ]) self.assertEqual(list(media[:5:4]), [ self.Video.objects.get(title='Gangnam Style'), self.Song.objects.get(artist='Gotye feat. Kimbra'), ])
def test_chain_sorted_django_factory(self): from dj.chain import chain c = chain(("8", 1, 2, "8"), [8, 3, 4, 8], "8568") c.xform = lambda v: int(v) c.xkey = lambda v: -int(v) c.xfilter = lambda v: int(v) % 2 == 0 c2 = c._django_factory("__getitem__", slice(1, 3)) self.test_chain_sorted(c2)
def test_heterogenic_sort(self): from dj.chain import chain media = chain(self.Video.objects.all(), self.books) title_asc = media.order_by('title') self.assertEqual(title_asc[0].title, 'A Tale of Two Cities') self.assertEqual(title_asc[1].title, 'Baby') self.assertEqual(title_asc[2].title, 'Bad Romance') self.assertEqual(title_asc[3].title, 'Don Quixote') self.assertEqual(title_asc[4].title, 'Gangnam Style') self.assertEqual(title_asc[5].title, 'Waka Waka')
def test_chain_simple(self, c=None): from dj.chain import chain if not c: c = chain((1, 2), [3, 4], "56") c.xform = lambda v: int(v) c.xfilter = lambda v: int(v) % 2 == 0 self.assertEqual((2, 4, 6), tuple(c)) self.assertEqual(3, len(c)) self.assertEqual((4, 6), tuple(c[1:])) self.assertEqual(2, len(c[1:])) self.assertEqual((4, 6), tuple(c[1:3])) self.assertEqual(2, len(c[1:3])) self.assertEqual((2, 4), tuple(c[:2])) self.assertEqual(2, len(c[:2])) self.assertEqual((2, ), tuple(c[:2:2])) self.assertEqual(1, len(c[:2:2])) self.assertEqual((2, 6), tuple(c[::2])) self.assertEqual(2, len(c[::2])) self.assertEqual(6, c[2]) try: c[3] self.fail("Index error not raised.") except IndexError: pass c.xfilter = lambda v: True self.assertEqual((1, 2, 3, 4, 5, 6), tuple(c)) self.assertEqual(6, len(c)) self.assertEqual((5, 6), tuple(c[4:])) self.assertEqual(2, len(c[4:])) self.assertEqual((3, 4), tuple(c[2:4])) self.assertEqual(2, len(c[2:4])) self.assertEqual((1, 2, 3, 4), tuple(c[:4])) self.assertEqual(4, len(c[:4])) self.assertEqual((1, 3), tuple(c[:4:2])) self.assertEqual(2, len(c[:4:2])) self.assertEqual((1, 4), tuple(c[::3])) self.assertEqual(2, len(c[::3])) self.assertEqual(6, c[5]) try: c[6] self.fail("Index error not raised.") except IndexError: pass try: c[-1] self.fail("Value error not raised.") except ValueError: pass try: c["boo"] self.fail("Value error not raised.") except ValueError: pass
def test_chain_simple(self, c=None): from dj.chain import chain if not c: c = chain((1, 2), [3, 4], "56") c.xform = lambda v: int(v) c.xfilter = lambda v: int(v) % 2 == 0 self.assertEqual((2, 4, 6), tuple(c)) self.assertEqual(3, len(c)) self.assertEqual((4, 6), tuple(c[1:])) self.assertEqual(2, len(c[1:])) self.assertEqual((4, 6), tuple(c[1:3])) self.assertEqual(2, len(c[1:3])) self.assertEqual((2, 4), tuple(c[:2])) self.assertEqual(2, len(c[:2])) self.assertEqual((2,), tuple(c[:2:2])) self.assertEqual(1, len(c[:2:2])) self.assertEqual((2, 6), tuple(c[::2])) self.assertEqual(2, len(c[::2])) self.assertEqual(6, c[2]) try: c[3] self.fail("Index error not raised.") except IndexError: pass c.xfilter = lambda v: True self.assertEqual((1, 2, 3, 4, 5, 6), tuple(c)) self.assertEqual(6, len(c)) self.assertEqual((5, 6), tuple(c[4:])) self.assertEqual(2, len(c[4:])) self.assertEqual((3, 4), tuple(c[2:4])) self.assertEqual(2, len(c[2:4])) self.assertEqual((1, 2, 3, 4), tuple(c[:4])) self.assertEqual(4, len(c[:4])) self.assertEqual((1, 3), tuple(c[:4:2])) self.assertEqual(2, len(c[:4:2])) self.assertEqual((1, 4), tuple(c[::3])) self.assertEqual(2, len(c[::3])) self.assertEqual(6, c[5]) try: c[6] self.fail("Index error not raised.") except IndexError: pass try: c[-1] self.fail("Value error not raised.") except ValueError: pass try: c["boo"] self.fail("Value error not raised.") except ValueError: pass
def test_collective_sort(self): from dj.chain import chain media = chain(self.Video.objects.all(), self.Song.objects.all()) dur_asc = media.order_by('duration') self.assertEqual(dur_asc[0].duration, 211) self.assertEqual(dur_asc[1].duration, 218) self.assertEqual(dur_asc[2].duration, 225) self.assertEqual(dur_asc[3].duration, 244) self.assertEqual(dur_asc[4].duration, 253) self.assertEqual(dur_asc[5].duration, 279) self.assertEqual(dur_asc[6].duration, 307) self.assertEqual(dur_asc[7].duration, 308) dur_desc = media.order_by('-duration') self.assertEqual(dur_desc[7].duration, 211) self.assertEqual(dur_desc[6].duration, 218) self.assertEqual(dur_desc[5].duration, 225) self.assertEqual(dur_desc[4].duration, 244) self.assertEqual(dur_desc[3].duration, 253) self.assertEqual(dur_desc[2].duration, 279) self.assertEqual(dur_desc[1].duration, 307) self.assertEqual(dur_desc[0].duration, 308) title_asc = media.order_by('title') self.assertEqual(title_asc[0].title, 'Baby') self.assertEqual(title_asc[1].title, 'Bad Romance') self.assertEqual(title_asc[2].title, 'Clocks') self.assertEqual(title_asc[3].title, 'Gangnam Style') self.assertEqual(title_asc[4].title, 'Madness') self.assertEqual(title_asc[5].title, 'Somebody That I Used to Know') self.assertEqual(title_asc[6].title, 'Spectrum') self.assertEqual(title_asc[7].title, 'Waka Waka') title_desc = media.order_by('-title') self.assertEqual(title_desc[7].title, 'Baby') self.assertEqual(title_desc[6].title, 'Bad Romance') self.assertEqual(title_desc[5].title, 'Clocks') self.assertEqual(title_desc[4].title, 'Gangnam Style') self.assertEqual(title_desc[3].title, 'Madness') self.assertEqual(title_desc[2].title, 'Somebody That I Used to Know') self.assertEqual(title_desc[1].title, 'Spectrum') self.assertEqual(title_desc[0].title, 'Waka Waka')