def test_while_loop(self): self.db.get.side_effect = iter([{}, {}, {}]) self.db.save.side_effect = iter( [DummyException, ResourceConflict, None]) generate_lot_id(self.ctime, self.db) assert self.db.get.call_count == 3 assert self.db.save.call_count == 3
def post(self): """This API request is targeted to creating new Lot.""" lot_id = generate_id() lot = self.request.validated['lot'] lot.id = lot_id if not lot.get('lotID'): lot.lotID = generate_lot_id(get_now(), self.db, self.server_id) self.request.registry.notify(LotInitializeEvent(lot)) default_status = type(lot).fields['status'].default status = self.request.json_body['data'].get('status', default_status) if status == 'draft': lot.status = status else: self.request.errors.add('body', 'status', 'You can create only in draft status') self.request.errors.status = 403 return acc = set_ownership(lot, self.request) self.request.validated['lot'] = lot self.request.validated['lot_src'] = {} if save_lot(self.request): self.LOGGER.info('Created lot {} ({})'.format(lot_id, lot.lotID), extra=context_unpack(self.request, {'MESSAGE_ID': 'lot_create'}, { 'lot_id': lot_id, 'lotID': lot.lotID })) self.request.response.status = 201 self.request.response.headers['Location'] = self.request.route_url( '{}:Lot'.format(lot.lotType), lot_id=lot_id) return {'data': lot.serialize(lot.status), 'access': acc}
def post(self): """This API request is targeted to creating new Lot.""" lot_id = generate_id() lot = self.request.validated['lot'] lot.id = lot_id if not lot.get('lotID'): lot.lotID = generate_lot_id(get_now(), self.db, self.server_id) self.request.registry.notify(LotInitializeEvent(lot)) if self.request.json_body['data'].get('status') == 'draft': lot.status = 'draft' set_ownership(lot, self.request) # rewrite as subscriber? self.request.validated['lot'] = lot self.request.validated['lot_src'] = {} if save_lot(self.request): self.LOGGER.info('Created lot {} ({})'.format(lot_id, lot.lotID), extra=context_unpack(self.request, {'MESSAGE_ID': 'lot_create'}, { 'lot_id': lot_id, 'lotID': lot.lotID })) self.request.response.status = 201 self.request.response.headers['Location'] = self.request.route_url( '{}:Lot'.format(lot.lotType), lot_id=lot_id) return { 'data': lot.serialize(lot.status), 'access': { 'token': lot.owner_token } }
def test_generate_id(self, mock_project_configurator): test_prefix = 'TEST-ID' mock_project_configurator.LOT_PREFIX = test_prefix result = generate_lot_id(get_now(), self.db) key = get_now().date().isoformat() index = self.db.get(key, 1) mock_id = '{}-{:04}-{:02}-{:02}-{:06}{}'.format( test_prefix, get_now().year, get_now().month, get_now().day, index, '') self.assertEqual(result, mock_id)
def test_generation_with_index(self): index = 2 self.db.get.side_effect = iter([{self.mocked_key: index}]) mocked_lotIDdoc = 'lotID' mocked_lotID = {self.mocked_key: index + 1} lot_id = 'UA-LR-DGF-{:04}-{:02}-{:02}-{:06}{}'.format( self.ctime.year, self.ctime.month, self.ctime.day, index, '') returned_lot_id = generate_lot_id(self.ctime, self.db) assert lot_id == returned_lot_id assert self.db.get.call_count == 1 self.db.get.assert_called_with(mocked_lotIDdoc, {'_id': mocked_lotIDdoc}) assert self.db.save.call_count == 1 self.db.save.assert_called_with(mocked_lotID)