def test_get_pending_requests_retrieves_pending_requests_only(self): # Given consumer = RequestDBConsumer("someRequest") req_type = DBRequestType(name="someRequest") pending_status = DBRequestState(name="pending") processed_status = DBRequestState(name="processed") req1 = DBRequest(cbsd_id="foo1", type=req_type, state=pending_status, payload={"some": "payload1"}) req2 = DBRequest(cbsd_id="foo2", type=req_type, state=pending_status, payload={"some": "payload2"}) req3 = DBRequest(cbsd_id="foo3", type=req_type, state=processed_status, payload={"some": "payload3"}) self.session.add_all([req1, req2, req3]) self.session.commit() # When reqs = consumer.get_pending_requests(self.session) # Then self.assertEqual(2, len(list(reqs.values())[0]))
def _prepare_two_pending_and_one_processed_request(self): req_type = DBRequestType(name="someRequest") pending_status = DBRequestState(name="pending") processed_status = DBRequestState(name="processed") req1 = DBRequest(cbsd_id="foo1", type=req_type, state=pending_status, payload={"some": "payload1"}) req2 = DBRequest(cbsd_id="foo2", type=req_type, state=pending_status, payload={"some": "payload2"}) req3 = DBRequest(cbsd_id="foo3", type=req_type, state=processed_status, payload={"some": "payload3"}) self.session.add_all([req1, req2, req3]) self.session.commit()
def _create_db_requests_from_fixture(self, request_state, request_type, fixture): request_type_name = self._get_request_type_from_fixture(fixture) return [ DBRequest(cbsd_id=self._generate_cbsd_id_from_request_json( r[request_type_name][0]), state=request_state, type=request_type, payload=r[request_type_name][0]) for r in fixture ]
def test_request_merging_merges_multiple_requests_into_one(self): # Given / When request_type = "registrationRequest" req_state = DBRequestState(name="pending") req_type = DBRequestType(name=request_type) reqs = [ DBRequest(cbsd_id="foo", state=req_state, type=req_type, payload=json.dumps(r[request_type])) for r in registration_requests ] merged_requests = merge_requests({request_type: reqs}) # Then self.assertIsInstance(merged_requests, dict) self.assertEqual(1, len(merged_requests.keys())) self.assertIsInstance(list(merged_requests.values())[0], list) self.assertEqual(2, len(list(merged_requests.values())[0]))
def test_get_request_response(self, req_id, db_response_payload, grpc_expected_response_payload): # Given db_request = DBRequest(id=1, cbsd_id="foo1") db_response = DBResponse(id=1, request_id=1, response_code=0, payload=db_response_payload) self.session.add_all([db_request, db_response]) self.session.commit() # When grpc_response_payload = json.loads( self.rc_service._get_request_response(req_id).payload) # Then self.assertEqual(grpc_expected_response_payload, grpc_response_payload)
def _create_db_request(request_type: DBRequestType, request_state: DBRequestState, request_payload: Dict) -> Optional[DBRequest]: cbsd_id = None if FCC_ID in request_payload and CBSD_SERIAL_NR in request_payload: cbsd_id = f'{request_payload[FCC_ID]}/{request_payload[CBSD_SERIAL_NR]}' elif CBSD_ID in request_payload: cbsd_id = request_payload[CBSD_ID] if not cbsd_id: logger.error( f"Could not generate cbsd_id from request: {request_payload}.") return None return DBRequest(type=request_type, state=request_state, cbsd_id=cbsd_id, payload=request_payload)
def _store_requests_from_map( self, request_map: Dict[str, List[Dict]]) -> List[int]: request_db_ids = [] request_type = next(iter(request_map)) with self.session_manager.session_scope() as session: request_pending_state = session.query(DBRequestState).filter( DBRequestState.name == RequestStates.PENDING.value).scalar() req_type = session.query(DBRequestType).filter( DBRequestType.name == request_type).scalar() for request_json in request_map[request_type]: db_request = DBRequest(type=req_type, state=request_pending_state, cbsd_id=self.get_cbsd_id( request_type, request_json), payload=request_json) if db_request: logger.info(f"Adding request {db_request}.") session.add(db_request) session.flush() request_db_ids.append(db_request.id) session.commit() return request_db_ids