我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用psycopg2.extras.Range()。
def test_keywords(self): from psycopg2.extras import Range r = Range(upper=20) self.assertEqual(r.lower, None) self.assertEqual(r.upper, 20) self.assertTrue(not r.isempty) self.assertTrue(r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(not r.lower_inc) self.assertTrue(not r.upper_inc) r = Range(lower=10, bounds='(]') self.assertEqual(r.lower, 10) self.assertEqual(r.upper, None) self.assertTrue(not r.isempty) self.assertTrue(not r.lower_inf) self.assertTrue(r.upper_inf) self.assertTrue(not r.lower_inc) self.assertTrue(not r.upper_inc)
def test_eq_hash(self): def assert_equal(r1, r2): self.assertTrue(r1 == r2) self.assertTrue(hash(r1) == hash(r2)) from psycopg2.extras import Range assert_equal(Range(empty=True), Range(empty=True)) assert_equal(Range(), Range()) assert_equal(Range(10, None), Range(10, None)) assert_equal(Range(10, 20), Range(10, 20)) assert_equal(Range(10, 20), Range(10, 20, '[)')) assert_equal(Range(10, 20, '[]'), Range(10, 20, '[]')) def assert_not_equal(r1, r2): self.assertTrue(r1 != r2) self.assertTrue(hash(r1) != hash(r2)) assert_not_equal(Range(10, 20), Range(10, 21)) assert_not_equal(Range(10, 20), Range(11, 20)) assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]'))
def test_eq_hash(self): def assert_equal(r1, r2): self.assert_(r1 == r2) self.assert_(hash(r1) == hash(r2)) from psycopg2.extras import Range assert_equal(Range(empty=True), Range(empty=True)) assert_equal(Range(), Range()) assert_equal(Range(10, None), Range(10, None)) assert_equal(Range(10, 20), Range(10, 20)) assert_equal(Range(10, 20), Range(10, 20, '[)')) assert_equal(Range(10, 20, '[]'), Range(10, 20, '[]')) def assert_not_equal(r1, r2): self.assert_(r1 != r2) self.assert_(hash(r1) != hash(r2)) assert_not_equal(Range(10, 20), Range(10, 21)) assert_not_equal(Range(10, 20), Range(11, 20)) assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]'))
def test_keywords(self): from psycopg2.extras import Range r = Range(upper=20) self.assertEqual(r.lower, None) self.assertEqual(r.upper, 20) self.assert_(not r.isempty) self.assert_(r.lower_inf) self.assert_(not r.upper_inf) self.assert_(not r.lower_inc) self.assert_(not r.upper_inc) r = Range(lower=10, bounds='(]') self.assertEqual(r.lower, 10) self.assertEqual(r.upper, None) self.assert_(not r.isempty) self.assert_(not r.lower_inf) self.assert_(r.upper_inf) self.assert_(not r.lower_inc) self.assert_(not r.upper_inc)
def test_eq_hash(self): from psycopg2.extras import Range def assert_equal(r1, r2): self.assert_(r1 == r2) self.assert_(hash(r1) == hash(r2)) assert_equal(Range(empty=True), Range(empty=True)) assert_equal(Range(), Range()) assert_equal(Range(10, None), Range(10, None)) assert_equal(Range(10, 20), Range(10, 20)) assert_equal(Range(10, 20), Range(10, 20, '[)')) assert_equal(Range(10, 20, '[]'), Range(10, 20, '[]')) def assert_not_equal(r1, r2): self.assert_(r1 != r2) self.assert_(hash(r1) != hash(r2)) assert_not_equal(Range(10, 20), Range(10, 21)) assert_not_equal(Range(10, 20), Range(11, 20)) assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]'))
def test_noparam(self): from psycopg2.extras import Range r = Range() self.assertTrue(not r.isempty) self.assertEqual(r.lower, None) self.assertEqual(r.upper, None) self.assertTrue(r.lower_inf) self.assertTrue(r.upper_inf) self.assertTrue(not r.lower_inc) self.assertTrue(not r.upper_inc)
def test_empty(self): from psycopg2.extras import Range r = Range(empty=True) self.assertTrue(r.isempty) self.assertEqual(r.lower, None) self.assertEqual(r.upper, None) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(not r.lower_inc) self.assertTrue(not r.upper_inc)
def test_nobounds(self): from psycopg2.extras import Range r = Range(10, 20) self.assertEqual(r.lower, 10) self.assertEqual(r.upper, 20) self.assertTrue(not r.isempty) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(r.lower_inc) self.assertTrue(not r.upper_inc)
def test_bounds(self): from psycopg2.extras import Range for bounds, lower_inc, upper_inc in [ ('[)', True, False), ('(]', False, True), ('()', False, False), ('[]', True, True)]: r = Range(10, 20, bounds) self.assertEqual(r.lower, 10) self.assertEqual(r.upper, 20) self.assertTrue(not r.isempty) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertEqual(r.lower_inc, lower_inc) self.assertEqual(r.upper_inc, upper_inc)
def test_nonzero(self): from psycopg2.extras import Range self.assertTrue(Range()) self.assertTrue(Range(10, 20)) self.assertTrue(not Range(empty=True))
def test_eq_wrong_type(self): from psycopg2.extras import Range self.assertNotEqual(Range(10, 20), ())
def test_eq_subclass(self): from psycopg2.extras import Range, NumericRange class IntRange(NumericRange): pass class PositiveIntRange(IntRange): pass self.assertEqual(Range(10, 20), IntRange(10, 20)) self.assertEqual(PositiveIntRange(10, 20), IntRange(10, 20)) # as the postgres docs describe for the server-side stuff, # ordering is rather arbitrary, but will remain stable # and consistent.
def test_gt_ordering(self): from psycopg2.extras import Range self.assertTrue(not Range(empty=True) > Range(0, 4)) self.assertTrue(Range(1, 2) > Range(0, 4)) self.assertTrue(not Range(0, 4) > Range(1, 2)) self.assertTrue(Range(1, 2) > Range()) self.assertTrue(not Range() > Range(1, 2)) self.assertTrue(Range(1) > Range(upper=1)) self.assertTrue(not Range() > Range()) self.assertTrue(not Range(empty=True) > Range(empty=True)) self.assertTrue(not Range(1, 2) > Range(1, 2)) with py3_raises_typeerror(): self.assertTrue(not 1 > Range(1, 2)) with py3_raises_typeerror(): self.assertTrue(Range(1, 2) > 1)
def test_le_ordering(self): from psycopg2.extras import Range self.assertTrue(Range(empty=True) <= Range(0, 4)) self.assertTrue(not Range(1, 2) <= Range(0, 4)) self.assertTrue(Range(0, 4) <= Range(1, 2)) self.assertTrue(not Range(1, 2) <= Range()) self.assertTrue(Range() <= Range(1, 2)) self.assertTrue(not Range(1) <= Range(upper=1)) self.assertTrue(Range() <= Range()) self.assertTrue(Range(empty=True) <= Range(empty=True)) self.assertTrue(Range(1, 2) <= Range(1, 2)) with py3_raises_typeerror(): self.assertTrue(1 <= Range(1, 2)) with py3_raises_typeerror(): self.assertTrue(not Range(1, 2) <= 1)
def test_ge_ordering(self): from psycopg2.extras import Range self.assertTrue(not Range(empty=True) >= Range(0, 4)) self.assertTrue(Range(1, 2) >= Range(0, 4)) self.assertTrue(not Range(0, 4) >= Range(1, 2)) self.assertTrue(Range(1, 2) >= Range()) self.assertTrue(not Range() >= Range(1, 2)) self.assertTrue(Range(1) >= Range(upper=1)) self.assertTrue(Range() >= Range()) self.assertTrue(Range(empty=True) >= Range(empty=True)) self.assertTrue(Range(1, 2) >= Range(1, 2)) with py3_raises_typeerror(): self.assertTrue(not 1 >= Range(1, 2)) with py3_raises_typeerror(): self.assertTrue(Range(1, 2) >= 1)
def test_pickling(self): from psycopg2.extras import Range r = Range(0, 4) self.assertEqual(loads(dumps(r)), r)
def test_cast_empty(self): from psycopg2.extras import Range cur = self.conn.cursor() for type in self.builtin_ranges: cur.execute("select 'empty'::%s" % type) r = cur.fetchone()[0] self.assertTrue(isinstance(r, Range), type) self.assertTrue(r.isempty)
def test_register_range_adapter(self): from psycopg2.extras import Range, register_range cur = self.conn.cursor() cur.execute("create type textrange as range (subtype=text)") rc = register_range('textrange', 'TextRange', cur) TextRange = rc.range self.assertTrue(issubclass(TextRange, Range)) self.assertEqual(TextRange.__name__, 'TextRange') r = TextRange('a', 'b', '(]') cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertEqual(r1.lower, 'a') self.assertEqual(r1.upper, 'b') self.assertTrue(not r1.lower_inc) self.assertTrue(r1.upper_inc) cur.execute("select %s", ([r, r, r],)) rs = cur.fetchone()[0] self.assertEqual(len(rs), 3) for r1 in rs: self.assertEqual(r1.lower, 'a') self.assertEqual(r1.upper, 'b') self.assertTrue(not r1.lower_inc) self.assertTrue(r1.upper_inc) # clear the adapters to allow precise count by scripts/refcounter.py del ext.adapters[rc.range, ext.ISQLQuote]
def test_noparam(self): from psycopg2.extras import Range r = Range() self.assert_(not r.isempty) self.assertEqual(r.lower, None) self.assertEqual(r.upper, None) self.assert_(r.lower_inf) self.assert_(r.upper_inf) self.assert_(not r.lower_inc) self.assert_(not r.upper_inc)
def test_empty(self): from psycopg2.extras import Range r = Range(empty=True) self.assert_(r.isempty) self.assertEqual(r.lower, None) self.assertEqual(r.upper, None) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assert_(not r.lower_inc) self.assert_(not r.upper_inc)
def test_nobounds(self): from psycopg2.extras import Range r = Range(10, 20) self.assertEqual(r.lower, 10) self.assertEqual(r.upper, 20) self.assert_(not r.isempty) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assert_(r.lower_inc) self.assert_(not r.upper_inc)
def test_bounds(self): from psycopg2.extras import Range for bounds, lower_inc, upper_inc in [ ('[)', True, False), ('(]', False, True), ('()', False, False), ('[]', True, True)]: r = Range(10, 20, bounds) self.assertEqual(r.lower, 10) self.assertEqual(r.upper, 20) self.assert_(not r.isempty) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assertEqual(r.lower_inc, lower_inc) self.assertEqual(r.upper_inc, upper_inc)
def test_bad_bounds(self): from psycopg2.extras import Range self.assertRaises(ValueError, Range, bounds='(') self.assertRaises(ValueError, Range, bounds='[}')
def test_nonzero(self): from psycopg2.extras import Range self.assert_(Range()) self.assert_(Range(10, 20)) self.assert_(not Range(empty=True))
def test_lt_ordering(self): from psycopg2.extras import Range self.assert_(Range(empty=True) < Range(0, 4)) self.assert_(not Range(1, 2) < Range(0, 4)) self.assert_(Range(0, 4) < Range(1, 2)) self.assert_(not Range(1, 2) < Range()) self.assert_(Range() < Range(1, 2)) self.assert_(not Range(1) < Range(upper=1)) self.assert_(not Range() < Range()) self.assert_(not Range(empty=True) < Range(empty=True)) self.assert_(not Range(1, 2) < Range(1, 2)) with py3_raises_typeerror(): self.assert_(1 < Range(1, 2)) with py3_raises_typeerror(): self.assert_(not Range(1, 2) < 1)
def test_gt_ordering(self): from psycopg2.extras import Range self.assert_(not Range(empty=True) > Range(0, 4)) self.assert_(Range(1, 2) > Range(0, 4)) self.assert_(not Range(0, 4) > Range(1, 2)) self.assert_(Range(1, 2) > Range()) self.assert_(not Range() > Range(1, 2)) self.assert_(Range(1) > Range(upper=1)) self.assert_(not Range() > Range()) self.assert_(not Range(empty=True) > Range(empty=True)) self.assert_(not Range(1, 2) > Range(1, 2)) with py3_raises_typeerror(): self.assert_(not 1 > Range(1, 2)) with py3_raises_typeerror(): self.assert_(Range(1, 2) > 1)
def test_le_ordering(self): from psycopg2.extras import Range self.assert_(Range(empty=True) <= Range(0, 4)) self.assert_(not Range(1, 2) <= Range(0, 4)) self.assert_(Range(0, 4) <= Range(1, 2)) self.assert_(not Range(1, 2) <= Range()) self.assert_(Range() <= Range(1, 2)) self.assert_(not Range(1) <= Range(upper=1)) self.assert_(Range() <= Range()) self.assert_(Range(empty=True) <= Range(empty=True)) self.assert_(Range(1, 2) <= Range(1, 2)) with py3_raises_typeerror(): self.assert_(1 <= Range(1, 2)) with py3_raises_typeerror(): self.assert_(not Range(1, 2) <= 1)
def test_ge_ordering(self): from psycopg2.extras import Range self.assert_(not Range(empty=True) >= Range(0, 4)) self.assert_(Range(1, 2) >= Range(0, 4)) self.assert_(not Range(0, 4) >= Range(1, 2)) self.assert_(Range(1, 2) >= Range()) self.assert_(not Range() >= Range(1, 2)) self.assert_(Range(1) >= Range(upper=1)) self.assert_(Range() >= Range()) self.assert_(Range(empty=True) >= Range(empty=True)) self.assert_(Range(1, 2) >= Range(1, 2)) with py3_raises_typeerror(): self.assert_(not 1 >= Range(1, 2)) with py3_raises_typeerror(): self.assert_(Range(1, 2) >= 1)