def test_wrapper(self): inst1 = {'key0': 'foo', 'key1': 'd', 'key2': 456} inst2 = {'key0': 'foo', 'key1': 's', 'key2': 123} ctx = context.RequestContext() ctx.cell_uuid = uuids.cell # Should sort by key1 sort_ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'asc']) iw1 = multi_cell_list.RecordWrapper(ctx, sort_ctx, inst1) iw2 = multi_cell_list.RecordWrapper(ctx, sort_ctx, inst2) # Check this both ways to make sure we're comparing against -1 # and not just nonzero return from cmp() self.assertTrue(iw1 < iw2) self.assertFalse(iw2 < iw1) # Should sort reverse by key1 sort_ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'desc']) iw1 = multi_cell_list.RecordWrapper(ctx, sort_ctx, inst1) iw2 = multi_cell_list.RecordWrapper(ctx, sort_ctx, inst2) # Check this both ways to make sure we're comparing against -1 # and not just nonzero return from cmp() self.assertTrue(iw1 > iw2) self.assertFalse(iw2 > iw1) # Make sure we can tell which cell a request came from self.assertEqual(uuids.cell, iw1.cell_uuid)
def test_compare_multiple(self): # key0 should not affect ordering, but key1 should inst1 = {'key0': 'foo', 'key1': 'd', 'key2': 456} inst2 = {'key0': 'foo', 'key1': 's', 'key2': 123} # Should be equivalent to ascending by key1 ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'asc']) self.assertEqual(-1, ctx.compare_records(inst1, inst2)) # Should be equivalent to descending by key1 ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'desc']) self.assertEqual(1, ctx.compare_records(inst1, inst2))
def test_wrapper_sentinels(self): inst1 = {'key0': 'foo', 'key1': 'd', 'key2': 456} ctx = context.RequestContext() ctx.cell_uuid = uuids.cell sort_ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'asc']) iw1 = multi_cell_list.RecordWrapper(ctx, sort_ctx, inst1) # Wrappers with sentinels iw2 = multi_cell_list.RecordWrapper(ctx, sort_ctx, context.did_not_respond_sentinel) iw3 = multi_cell_list.RecordWrapper(ctx, sort_ctx, context.raised_exception_sentinel) # NOTE(danms): The sentinel wrappers always win self.assertTrue(iw2 < iw1) self.assertTrue(iw3 < iw1) self.assertFalse(iw1 < iw2) self.assertFalse(iw1 < iw3) # NOTE(danms): Comparing two wrappers with sentinels will always return # True for less-than because we're just naive about always favoring the # left hand side. This is fine for our purposes but put it here to make # it explicit. self.assertTrue(iw2 < iw3) self.assertTrue(iw3 < iw2)
def test_wrapper(self): inst1 = {'key0': 'foo', 'key1': 'd', 'key2': 456} inst2 = {'key0': 'foo', 'key1': 's', 'key2': 123} # Should sort by key1 ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'asc']) iw1 = multi_cell_list.RecordWrapper(ctx, inst1) iw2 = multi_cell_list.RecordWrapper(ctx, inst2) # Check this both ways to make sure we're comparing against -1 # and not just nonzero return from cmp() self.assertTrue(iw1 < iw2) self.assertFalse(iw2 < iw1) # Should sort reverse by key1 ctx = multi_cell_list.RecordSortContext(['key0', 'key1'], ['asc', 'desc']) iw1 = multi_cell_list.RecordWrapper(ctx, inst1) iw2 = multi_cell_list.RecordWrapper(ctx, inst2) # Check this both ways to make sure we're comparing against -1 # and not just nonzero return from cmp() self.assertTrue(iw1 > iw2) self.assertFalse(iw2 > iw1)
def test_compare_simple(self): dt1 = datetime.datetime(2015, 11, 5, 20, 30, 00) dt2 = datetime.datetime(1955, 10, 25, 1, 21, 00) inst1 = {'key0': 'foo', 'key1': 'd', 'key2': 456, 'key4': dt1} inst2 = {'key0': 'foo', 'key1': 's', 'key2': 123, 'key4': dt2} # Equal key0, inst == inst2 ctx = multi_cell_list.RecordSortContext(['key0'], ['asc']) self.assertEqual(0, ctx.compare_records(inst1, inst2)) # Equal key0, inst == inst2 (direction should not matter) ctx = multi_cell_list.RecordSortContext(['key0'], ['desc']) self.assertEqual(0, ctx.compare_records(inst1, inst2)) # Ascending by key1, inst1 < inst2 ctx = multi_cell_list.RecordSortContext(['key1'], ['asc']) self.assertEqual(-1, ctx.compare_records(inst1, inst2)) # Descending by key1, inst2 < inst1 ctx = multi_cell_list.RecordSortContext(['key1'], ['desc']) self.assertEqual(1, ctx.compare_records(inst1, inst2)) # Ascending by key2, inst2 < inst1 ctx = multi_cell_list.RecordSortContext(['key2'], ['asc']) self.assertEqual(1, ctx.compare_records(inst1, inst2)) # Descending by key2, inst1 < inst2 ctx = multi_cell_list.RecordSortContext(['key2'], ['desc']) self.assertEqual(-1, ctx.compare_records(inst1, inst2)) # Ascending by key4, inst1 > inst2 ctx = multi_cell_list.RecordSortContext(['key4'], ['asc']) self.assertEqual(1, ctx.compare_records(inst1, inst2)) # Descending by key4, inst1 < inst2 ctx = multi_cell_list.RecordSortContext(['key4'], ['desc']) self.assertEqual(-1, ctx.compare_records(inst1, inst2))