def test_scalars(self): result = self._fixture() eq_(list(result.scalars()), [1, 2, 1, 4]) result = self._fixture() eq_(list(result.scalars(2)), [1, 2, 2, 2])
def test_scalar_none_one(self): result = self._fixture(data=[(1, None, 2)]) result = result.scalars(1).unique() # one is returning None, see? eq_(result.one(), None)
def test_scalar_only_on_filter(self): # test a mixture of the "real" result and the # scalar filter, where scalar has unique and real result does not. # this is new as of [ticket:5503] where we have created # ScalarResult / MappingResult "filters" that don't modify # the Result result = self._fixture( data=[ (1, 1, 2), (3, 4, 5), (1, 1, 2), (3, None, 5), (3, 4, 5), (3, None, 5), ] ) # result is non-unique. u_s is unique on column 0 u_s = result.scalars(0).unique() eq_(next(u_s), 1) # unique value 1 from first row eq_(next(result), (3, 4, 5)) # second row eq_(next(u_s), 3) # skip third row, return 3 for fourth row eq_(next(result), (3, 4, 5)) # non-unique fifth row eq_(u_s.all(), []) # unique set is done because only 3 is left
def test_scalars_mappings(self): result = self._fixture() eq_( list(result.scalars().mappings()), [{"a": 1}, {"a": 2}, {"a": 1}, {"a": 4}], )
def test_scalar_none_one_or_none(self): result = self._fixture(data=[(1, None, 2)]) result = result.scalars(1).unique() # the orm.Query can actually do this right now, so we sort of # have to allow for this unforuntately, unless we want to raise? eq_(result.one_or_none(), None)
def test_merge_scalars(self, merge_fixture): r1, r2, r3, r4 = merge_fixture for r in (r1, r2, r3, r4): r.scalars(0) result = r1.merge(r2, r3, r4) eq_(result.scalars(0).all(), [7, 8, 9, 10, 11, 12])
def test_scalars_plus_one_none(self): result = self._fixture(num_rows=0) result = result.scalars() assert_raises_message( exc.NoResultFound, "No row was found when one was required", result.one, )
def test_merge_unique(self, dupe_fixture): r1, r2 = dupe_fixture r1.scalars("y") r2.scalars("y") result = r1.merge(r2) # uniqued 2, 2, 1, 3 eq_(result.scalars("y").unique().all(), [2, 1, 3])
def test_merge_preserve_unique(self, dupe_fixture): r1, r2 = dupe_fixture r1.unique().scalars("y") r2.unique().scalars("y") result = r1.merge(r2) # unique takes place eq_(result.scalars("y").all(), [2, 1, 3])
def test_scalar_none_all_unique(self): result = self._fixture(data=[ (1, None, 2), (3, 4, 5), (3, None, 5), (3, None, 5), (3, 4, 5), ]) result = result.scalars(1).unique() eq_(result.all(), [None, 4])
def test_scalar_none_iterate(self): result = self._fixture(data=[ (1, None, 2), (3, 4, 5), (3, None, 5), (3, None, 5), (3, 4, 5), ]) result = result.scalars(1) eq_(list(result), [None, 4, None, None, 4])
def test_scalars_no_fetchone(self): result = self._fixture() s = result.scalars() assert not hasattr(s, "fetchone") # original result is unchanged eq_(result.mappings().fetchone(), {"a": 1, "b": 1, "c": 1}) # scalars eq_(s.all(), [2, 1, 4])
def test_scalars_freeze(self): result = self._fixture() result = result.scalars(1) frozen = result.freeze() r1 = frozen() eq_(r1.fetchall(), [1, 1, 3, 1]) r2 = frozen().unique() eq_(r2.fetchall(), [1, 3])
def test_scalar_none_many(self): result = self._fixture(data=[ (1, None, 2), (3, 4, 5), (3, None, 5), (3, None, 5), (3, 4, 5), ]) result = result.scalars(1) eq_(result.fetchmany(3), [None, 4, None]) eq_(result.fetchmany(5), [None, 4])
def test_scalars_no_fetchone(self): result = self._fixture() result = result.scalars() assert_raises_message( exc.InvalidRequestError, r"Can't use fetchone\(\) when returning scalar values; ", result.fetchone, ) # mappings() switches the flag off eq_(result.mappings().fetchone(), {"a": 1})
def test_scalar_none_first(self): result = self._fixture(data=[(1, None, 2)]) result = result.scalars(1).unique() eq_(result.first(), None)
def test_scalars_plus_one(self): result = self._fixture(num_rows=1) row = result.scalars().one() eq_(row, 1)
def test_scalars_one_w_unique(self): result = self._fixture(data=[(1, None, 2)]) result = result.unique() eq_(result.scalars().one(), 1)