def test_order_queryset(self): """ order queryset by field or method """ qs = Shareholder.objects.all() self.assertEqual(list(_order_queryset(qs, 'pk')), list(qs.order_by('pk'))) self.assertEqual(list(_order_queryset(qs, '-pk')), list(qs.order_by('-pk')))
def test_order_queryset_share_count(self): """ order queryset by field or method """ qs = Shareholder.objects.all() # desc share count res = _order_queryset(qs, '-share_count') for idx, r in enumerate(res): if idx > 0: self.assertTrue(r.share_count() <= res[idx-1].share_count()) # asc share_count res = _order_queryset(qs, 'share_count') for idx, r in enumerate(res): if idx > 0: self.assertTrue(r.share_count() >= res[idx-1].share_count())
def test_order_queryset_postal_code(self): qs = Shareholder.objects.all() for idx, s in enumerate(qs): s.user.userprofile.postal_code = u'{}'.format(idx) s.user.userprofile.save() res = _order_queryset(qs, 'user__userprofile__postal_code') for idx, r in enumerate(res): if idx > 0: self.assertGreater(r.user.userprofile.postal_code, res[idx-1].user.userprofile.postal_code) res = _order_queryset(qs, '-user__userprofile__postal_code') for idx, r in enumerate(res): if idx > 0: self.assertLess(r.user.userprofile.postal_code, res[idx-1].user.userprofile.postal_code)
def test_order_queryset_cumulated_face_value(self): qs = Shareholder.objects.all() cs = qs.last() for idx, s in enumerate(qs): mommy.make('shareholder.Position', seller=cs, buyer=s, count=idx) res = _order_queryset(qs, 'cumulated_face_value') for idx, r in enumerate(res): if idx > 0: self.assertGreaterEqual(r.cumulated_face_value(), res[idx-1].cumulated_face_value()) res = _order_queryset(qs, '-cumulated_face_value') for idx, r in enumerate(res): if idx > 0: self.assertLessEqual(r.cumulated_face_value(), res[idx-1].cumulated_face_value())
def test_order_queryset_user__email(self): company = CompanyGenerator().generate() mommy.make(Shareholder, company=company, _quantity=10, _fill_optional=True) qs = company.shareholder_set.all() for idx, s in enumerate(qs): s.user.email = u'{}@example.com'.format(idx) s.user.save() res = _order_queryset(qs, 'user__email') for idx, r in enumerate(res): if idx > 0: self.assertNotEqual(r.user.email, '') self.assertIn(u'@', r.user.email) self.assertGreater(r.user.email, res[idx-1].user.email) res = _order_queryset(qs, '-user__email') for idx, r in enumerate(res): if idx > 0: self.assertLess(r.user.email, res[idx-1].user.email)
def test_order_queryset_company_name(self): """ sort for last_name also should sort company names """ qs = Shareholder.objects.all() for idx, s in enumerate(qs): s.user.userprofile.company_name = str(unichr(idx+97)) s.user.first_name = u'' s.user.last_name = u'' s.user.save() s.user.userprofile.save() res = _order_queryset(qs, 'get_full_name') for idx, r in enumerate(res): if idx > 0: self.assertGreater(r.get_full_name(), res[idx-1].get_full_name()) res = _order_queryset(qs, '-get_full_name') for idx, r in enumerate(res): if idx > 0: self.assertLess(r.get_full_name(), res[idx-1].get_full_name())
def get_active_shareholders(company, date, ordering, security=None): qs = company.get_active_shareholders(date=date, security=security) result = _order_queryset(qs, ordering) return result
def get_active_option_holders(company, date, ordering, security=None): kwargs = dict(date=date, security=security) qs = company.get_active_option_holders(**kwargs) result = _order_queryset(qs, ordering) return result