class TestCursor(TestCase): def setUp(self): self.MockSession = mock.Mock() self.MockSession.db = mock.MagicMock() class TestDoc(Document): class __mongometa__: name = "test_doc" session = self.MockSession a = Field(int) b = Field(S.Object(dict(a=int))) self.TestDoc = TestDoc base_iter = iter([{}, {}, {}]) mongo_cursor = mock.Mock() mongo_cursor.count = mock.Mock(return_value=3) mongo_cursor.__iter__ = lambda self: base_iter mongo_cursor.next = base_iter.next mongo_cursor.limit = mock.Mock(return_value=mongo_cursor) mongo_cursor.hint = mock.Mock(return_value=mongo_cursor) mongo_cursor.skip = mock.Mock(return_value=mongo_cursor) mongo_cursor.sort = mock.Mock(return_value=mongo_cursor) self.cursor = Cursor(TestDoc, mongo_cursor) def test_cursor(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(len(self.cursor), 3) self.assertEqual(self.cursor.count(), 3) self.assertEqual(self.cursor.next(), obj) self.cursor.limit(100) self.cursor.skip(10) self.cursor.hint("foo") self.cursor.sort("a") self.assertEqual(self.cursor.all(), [obj, obj]) self.cursor.cursor.limit.assert_called_with(100) self.cursor.cursor.skip.assert_called_with(10) self.cursor.cursor.hint.assert_called_with("foo") self.cursor.cursor.sort.assert_called_with("a") def test_first(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), None) def test_one_full(self): self.assertRaises(ValueError, self.cursor.one) def test_one_empty(self): self.cursor.all() self.assertRaises(ValueError, self.cursor.one) def test_one_ok(self): self.cursor.next() self.cursor.next() obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.one(), obj)
class TestCursor(TestCase): def setUp(self): self.MockSession = mock.Mock() self.MockSession.db = mock.MagicMock() class TestDoc(Document): class __mongometa__: name = 'test_doc' session = self.MockSession a = Field(int) b = Field(S.Object(dict(a=int))) self.TestDoc = TestDoc base_iter = iter([{}, {}, {}]) mongo_cursor = mock.Mock() mongo_cursor.count = mock.Mock(return_value=3) mongo_cursor.__iter__ = lambda self: base_iter mongo_cursor.next = base_iter.next mongo_cursor.limit = mock.Mock(return_value=mongo_cursor) mongo_cursor.hint = mock.Mock(return_value=mongo_cursor) mongo_cursor.skip = mock.Mock(return_value=mongo_cursor) mongo_cursor.sort = mock.Mock(return_value=mongo_cursor) self.cursor = Cursor(TestDoc, mongo_cursor) def test_cursor(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(len(self.cursor), 3) self.assertEqual(self.cursor.count(), 3) self.assertEqual(self.cursor.next(), obj) self.cursor.limit(100) self.cursor.skip(10) self.cursor.hint('foo') self.cursor.sort('a') self.assertEqual(self.cursor.all(), [obj, obj]) self.cursor.cursor.limit.assert_called_with(100) self.cursor.cursor.skip.assert_called_with(10) self.cursor.cursor.hint.assert_called_with('foo') self.cursor.cursor.sort.assert_called_with('a') def test_first(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), None) def test_one_full(self): self.assertRaises(ValueError, self.cursor.one) def test_one_empty(self): self.cursor.all() self.assertRaises(ValueError, self.cursor.one) def test_one_ok(self): self.cursor.next() self.cursor.next() obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.one(), obj)
class TestCursor(TestCase): def setUp(self): self.MockSession = mock.Mock() self.MockSession.db = mock.MagicMock() class IteratorMock(mock.Mock): def __init__(self, base_iter): super().__init__() self._base_iter = base_iter def __iter__(self): return self def next(self): return next(self._base_iter) __next__ = next class TestDoc(Document): class __mongometa__: name = 'test_doc' session = self.MockSession a = Field(int) b = Field(S.Object(dict(a=int))) self.TestDoc = TestDoc mongo_cursor = IteratorMock(iter([{}, {}, {}])) mongo_cursor.count = mock.Mock(return_value=3) mongo_cursor.limit = mock.Mock(return_value=mongo_cursor) mongo_cursor.hint = mock.Mock(return_value=mongo_cursor) mongo_cursor.skip = mock.Mock(return_value=mongo_cursor) mongo_cursor.sort = mock.Mock(return_value=mongo_cursor) self.cursor = Cursor(TestDoc, mongo_cursor) def test_cursor(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.count(), 3) self.assertEqual(self.cursor.next(), obj) self.cursor.limit(100) self.cursor.skip(10) self.cursor.hint('foo') self.cursor.sort('a') self.assertEqual(self.cursor.all(), [obj, obj]) self.cursor.cursor.limit.assert_called_with(100) self.cursor.cursor.skip.assert_called_with(10) self.cursor.cursor.hint.assert_called_with('foo') self.cursor.cursor.sort.assert_called_with('a') self.assertRaises(MingException, lambda: bool(self.cursor)) def test_first(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), None) def test_one_full(self): self.assertRaises(ValueError, self.cursor.one) def test_one_empty(self): self.cursor.all() self.assertRaises(ValueError, self.cursor.one) def test_one_ok(self): self.cursor.next() self.cursor.next() obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.one(), obj)
class TestCursor(TestCase): def setUp(self): self.MockSession = mock.Mock() self.MockSession.db = mock.MagicMock() class IteratorMock(mock.Mock): def __init__(self, base_iter): super(IteratorMock, self).__init__() self._base_iter = base_iter def __iter__(self): return self def next(self): return next(self._base_iter) __next__ = next class TestDoc(Document): class __mongometa__: name='test_doc' session = self.MockSession a=Field(int) b=Field(S.Object(dict(a=int))) self.TestDoc = TestDoc mongo_cursor = IteratorMock(iter([ {}, {}, {} ])) mongo_cursor.count = mock.Mock(return_value=3) mongo_cursor.limit = mock.Mock(return_value=mongo_cursor) mongo_cursor.hint = mock.Mock(return_value=mongo_cursor) mongo_cursor.skip = mock.Mock(return_value=mongo_cursor) mongo_cursor.sort = mock.Mock(return_value=mongo_cursor) self.cursor = Cursor(TestDoc, mongo_cursor) def test_cursor(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.count(), 3) self.assertEqual(self.cursor.next(), obj) self.cursor.limit(100) self.cursor.skip(10) self.cursor.hint('foo') self.cursor.sort('a') self.assertEqual(self.cursor.all(), [obj,obj]) self.cursor.cursor.limit.assert_called_with(100) self.cursor.cursor.skip.assert_called_with(10) self.cursor.cursor.hint.assert_called_with('foo') self.cursor.cursor.sort.assert_called_with('a') self.assertRaises(MingException, lambda: bool(self.cursor)) def test_first(self): obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), obj) self.assertEqual(self.cursor.first(), None) def test_one_full(self): self.assertRaises(ValueError, self.cursor.one) def test_one_empty(self): self.cursor.all() self.assertRaises(ValueError, self.cursor.one) def test_one_ok(self): self.cursor.next() self.cursor.next() obj = dict(a=None, b=dict(a=None)) self.assertEqual(self.cursor.one(), obj)