Пример #1
0
    def setUp(self):
        super().setUp()
        self.some_id = random.randint(1, 10000)
        self.matched_count = 1
        self.modified_count = 1

        self.item = self.Model(_id=int_to_id_obj(self.some_id))
        self.item_json = self.item.serialize(
            field_filter_fn=self.manager._default_exclude_in_save_fn
        )

        def side_effect(filter_data, update):

            self.assertEqual(update, {"$set": self.item_json})

            self.assertFalse(
                'field' in update['$set']
            )
            self.assertFalse(
                'externalField' in update['$set']
            )
            self.assertFalse(
                'noneInternalField' in update['$set']
            )

            self.assertEqual(filter_data, {"_id": int_to_id_obj(self.some_id)})

            class UpdateResultMock:
                def __init__(self, matched_count, modified_count):
                    self.matched_count = matched_count
                    self.modified_count = modified_count

            return UpdateResultMock(self.matched_count, self.modified_count)

        self.manager.collection.update_one.side_effect = side_effect
Пример #2
0
    def test_calls_insert_one_with_serialized_data(self):

        saved_item = self.manager.save(self.item)
        hex_str_from_id = id_obj_to_hex_str(
            int_to_id_obj(self.some_id)
        )
        self.assertEqual(saved_item._id, hex_str_from_id)
        self.assert_function_calls()
Пример #3
0
 def test_creates_an_id_from_an_int(self):
     id_int = random.randint(1, int(
         12*'f', 16
     ))
     id_obj = utils.int_to_id_obj(id_int)
     self.assertEqual(
         int.from_bytes(id_obj.binary, 'big'),
         id_int
     )
Пример #4
0
        def side_effect(filter_data):
            self.assertEqual(
                filter_data,
                {"_id": int_to_id_obj(self.item._id)}
            )

            class DeleteResultMock:
                def __init__(self, deleted_count):
                    self.deleted_count = deleted_count

            return DeleteResultMock(self.deleted_count)
Пример #5
0
 def test_creates_a_hex_str_from_an_id_obj(self):
     rand_int = random.randint(1, int(
         12*'f', 16
     ))
     hex_str = hex(rand_int)
     id_obj = utils.int_to_id_obj(rand_int)
     hex_from_obj = utils.id_obj_to_hex_str(id_obj)
     # we cannot compare the strings directly because they are formatted differently
     # (leading zeroes + leading 0x. but they should represent the same integer.
     self.assertEqual(
         int(hex_str, 16), int(hex_from_obj, 16)
     )
Пример #6
0
    def setUp(self):
        super().setUp()
        self.question_mongo_mock = mock.Mock(name='questoin collection mock')
        answer_mongo_mock = mock.Mock(name='answer collection mock')

        self.insert_result_mock = mock.Mock(name='insert_result_mock')
        self.insert_result_mock.inserted_id = utils.int_to_id_obj(randrange(30000))
        self.question_mongo_mock.insert_one = mock.Mock(name='insert_one', return_value=self.insert_result_mock)
        answer_mongo_mock.find = mock.Mock(name="answer.find", return_value=[])
        self.get_db_mock.return_value = {
            'QuestionModel': self.question_mongo_mock,
            'AnswerModel': answer_mongo_mock
        }
Пример #7
0
        def side_effect(filter_data, update):

            self.assertEqual(update, {"$set": self.item_json})

            self.assertFalse(
                'field' in update['$set']
            )
            self.assertFalse(
                'externalField' in update['$set']
            )
            self.assertFalse(
                'noneInternalField' in update['$set']
            )

            self.assertEqual(filter_data, {"_id": int_to_id_obj(self.some_id)})

            class UpdateResultMock:
                def __init__(self, matched_count, modified_count):
                    self.matched_count = matched_count
                    self.modified_count = modified_count

            return UpdateResultMock(self.matched_count, self.modified_count)
Пример #8
0
 def test_is_false_if_id_is_given(self):
     instance = ModelBase(_id=int_to_id_obj(random.randint(1, 10000)))
     self.assertFalse(instance.is_new())
Пример #9
0
    def test_throws_not_found_exception_if_no_item_was_found_for_given_id(self):
        self.deleted_count = 0

        with self.assertRaises(NotFoundError):
            self.manager.delete_by_id(int_to_id_obj(self.some_id))
Пример #10
0
 def test_delete_by_id_calls_delete_one(self):
     self.manager.delete_by_id(int_to_id_obj(self.some_id))
     self.manager.collection.delete_one.assert_called_once()
Пример #11
0
 def __init__(self, inserted_int_id):
     if inserted_int_id:
         self.inserted_id = int_to_id_obj(inserted_int_id)
     else:
         self.inserted_id = inserted_int_id
Пример #12
0
 def test_deserialize_with_hex_str(self):
     random_id = random.randint(0, 1000)
     _id = int_to_id_obj(random_id)
     id_hex_str = id_obj_to_hex_str(_id)
     deserialized = self.field._deserialize(id_hex_str, {})
     self.assertEqual(deserialized, _id)
Пример #13
0
    def test_deserialize_with_id_obj(self):
        random_id = random.randint(0, 1000)
        _id = int_to_id_obj(random_id)

        deserialized = self.field._deserialize(_id, {})
        self.assertEqual(deserialized, _id)