def test_flags(self): request = Request() self.assertIsNone(request.flags) self.assertFalse(request.slave_ok) msg_bytes, request_id = self._pack_request('db.collection', False) request = OpQuery.unpack(msg_bytes, None, None, request_id) self.assertIsInstance(request, OpQuery) self.assertNotIsInstance(request, Command) self.assertEqual(0, request.flags) self.assertFalse(request.slave_ok) self.assertFalse(request.slave_okay) # Synonymous. msg_bytes, request_id = self._pack_request('db.$cmd', False) request = OpQuery.unpack(msg_bytes, None, None, request_id) self.assertIsInstance(request, Command) self.assertEqual(0, request.flags) msg_bytes, request_id = self._pack_request('db.collection', True) request = OpQuery.unpack(msg_bytes, None, None, request_id) self.assertEqual(4, request.flags) self.assertTrue(request.slave_ok) msg_bytes, request_id = self._pack_request('db.$cmd', True) request = OpQuery.unpack(msg_bytes, None, None, request_id) self.assertEqual(4, request.flags)
def test_fields(self): self.assertIsNone(OpQuery({}).fields) self.assertEqual({ '_id': False, 'a': 1 }, OpQuery({}, fields={ '_id': False, 'a': 1 }).fields)
def test_projection(self): q = {} fields = {'foo': True} # OP_QUERY, server = MockupDB(auto_ismaster=True, min_wire_version=0, max_wire_version=3) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) cursor = client.test.collection.find(q, fields) with going(next, cursor): request = server.receives(OpQuery(q, fields=fields)) request.reply([], cursor_id=0) # "find" command. server = MockupDB(auto_ismaster=True, min_wire_version=0, max_wire_version=4) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) cursor = client.test.collection.find(q, fields) cmd = Command( SON([('find', 'collection'), ('filter', q), ('projection', fields)])) with going(next, cursor): request = server.receives(cmd) request.ok(cursor={'id': 0, 'firstBatch': []})
def test_repr(self): self.assertEqual('Request()', repr(Request())) self.assertEqual('Request({})', repr(Request({}))) self.assertEqual('Request({})', repr(Request([{}]))) self.assertEqual('Request(flags=4)', repr(Request(flags=4))) self.assertEqual('OpQuery({})', repr(OpQuery())) self.assertEqual('OpQuery({})', repr(OpQuery({}))) self.assertEqual('OpQuery({})', repr(OpQuery([{}]))) self.assertEqual('OpQuery({}, flags=SlaveOkay)', repr(OpQuery(flags=4))) self.assertEqual('OpQuery({}, flags=SlaveOkay)', repr(OpQuery({}, flags=4))) self.assertEqual('OpQuery({}, flags=TailableCursor|AwaitData)', repr(OpQuery({}, flags=34))) self.assertEqual('Command({})', repr(Command())) self.assertEqual('Command({"foo": 1})', repr(Command('foo'))) son = SON([('b', 1), ('a', 1), ('c', 1)]) self.assertEqual('Command({"b": 1, "a": 1, "c": 1})', repr(Command(son))) self.assertEqual('Command({}, flags=SlaveOkay)', repr(Command(flags=4))) self.assertEqual('OpInsert({}, {})', repr(OpInsert([{}, {}]))) self.assertEqual('OpInsert({}, {})', repr(OpInsert({}, {})))
def test_update_userdetails(self): user_details = {'userId': 'user1', 'firstName': 'myFirstName', 'lastName': 'myLastName', 'age': 20} userId = "user1" details = go(display_userdetails, userId, self.testConf) query = OpQuery({"userId": "user1"}, namespace="Books.myapp_user", fields={"_id": False}) request = self.server.receives(query) request.reply(user_details) output = details() for key in output: if key in user_details: self.assertEqual(user_details[key], output[key])
def test_assert_matches(self): request = OpQuery({'x': 17}, flags=QUERY_FLAGS['SlaveOkay']) request.assert_matches(request) with self.assertRaises(AssertionError): request.assert_matches(Command('foo'))