def with_request(self, type_id: int, payload: str) -> DBCbsdBuilder: request = DBRequest( type_id=type_id, payload=json.loads(payload), ) self.cbsd.requests.append(request) return self
def test_dp_log_created_from_db_request(self, with_cbsd, serial_num, fcc_id, network_id): # Given req_type = self.session.query(DBRequestType).first() cbsd = None if with_cbsd: cbsd = self.session.query(DBCbsd).first() request = DBRequest(type=req_type, cbsd=cbsd, payload=SOME_MESSAGE) # When actual_log = make_dp_log(request) # Then expected_log = DPLog( event_timestamp=SOME_TIMESTAMP, cbsd_serial_number=serial_num, fcc_id=fcc_id, log_from=DP, log_message=SOME_MESSAGE, log_name=HEARTBEAT_REQUEST, log_to=SAS, network_id=network_id, response_code=None, ) self.assertEqual(expected_log, actual_log)
def test_dp_log_created_from_db_response(self, with_cbsd, serial_num, fcc_id, network_id): # Given req_type = self.session.query(DBRequestType).first() cbsd = None if with_cbsd: cbsd = self.session.query(DBCbsd).first() request = DBRequest(type=req_type, cbsd=cbsd, payload='some_request_message') resp_payload = {"response": {"responseCode": "0"}} response = DBResponse(request=request, response_code=200, payload=resp_payload) # When actual_log = make_dp_log(response) # Then expected_log = DPLog( cbsd_serial_number=serial_num, fcc_id=fcc_id, log_from=SAS, log_message="{'response': {'responseCode': '0'}}", log_name='heartbeatResponse', log_to=DP, network_id=network_id, response_code="0", ) self.assertEqual(expected_log, actual_log)
def test_grant_attempts_after_response(self, code, message_type, expected): cbsd = DBCbsd( cbsd_id=CBSD_ID, user_id=USER_ID, fcc_id=FCC_ID, cbsd_serial_number=CBSD_SERIAL_NR, grant_attempts=INITIAL_GRANT_ATTEMPTS, state=self._get_db_enum(DBCbsdState, CbsdStates.REGISTERED.value), desired_state=self._get_db_enum(DBCbsdState, CbsdStates.REGISTERED.value), ) request = DBRequest( type=self._get_db_enum(DBRequestType, message_type), cbsd=cbsd, payload={'cbsdId': CBSD_ID}, ) response = self._prepare_response_from_db_requests( db_requests=[request], response_code=code, ) self.session.add(request) self.session.commit() self._process_response( request_type_name=message_type, response=response, db_requests=[request], ) self.assertEqual(expected, cbsd.grant_attempts)
def _store_requests_from_map_in_db(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: req_type = session.query(DBRequestType).filter( DBRequestType.name == request_type, ).first() for request_json in request_map[request_type]: cbsd = self._get_or_create_cbsd( session, request_type, request_json, ) if not cbsd: logger.error( f"Could not obtain cbsd to bind to the request: {request_json}", ) continue db_request = DBRequest( type=req_type, cbsd=cbsd, 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
def _prepare_two_pending_and_one_processed_request(self): test_state = DBCbsdState(name="test_state") cbsds = [] for i in range(1, 4): cbsds.append( DBCbsd( id=int(i), cbsd_id=f"foo{i}", state=test_state, user_id="test_user", fcc_id=f"test_fcc_id{i}", cbsd_serial_number=f"test_serial_nr{i}", ), ) req_type = DBRequestType(name="someRequest") pending_status = DBRequestState(name="pending") processed_status = DBRequestState(name="processed") req1 = DBRequest( cbsd=cbsds[0], type=req_type, state=pending_status, payload={ "some": "payload1", }, ) req2 = DBRequest( cbsd=cbsds[1], type=req_type, state=pending_status, payload={ "some": "payload2", }, ) req3 = DBRequest( cbsd=cbsds[2], 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_type, fixture, cbsd_state): db_requests = [] for reqs in fixture: for req in reqs[request_type]: db_requests.append( DBRequest( cbsd=self._generate_cbsd_from_request_json( req, self._get_db_enum(DBCbsdState, cbsd_state), ), type=self._get_db_enum(DBRequestType, request_type), payload=req, ), ) return db_requests
def _add_relinquish_requests(self, session: Session, cbsd: DBCbsd) -> None: deregister_request_type = session.query(DBRequestType).filter( DBRequestType.name == RequestTypes.RELINQUISHMENT.value, ).scalar() grants = session.query(DBGrant).join(DBGrantState).filter( DBGrant.cbsd_id == cbsd.id, DBGrantState.name != GrantStates.IDLE.value, ) for grant in grants: request_dict = {"cbsdId": cbsd.cbsd_id, "grantId": grant.grant_id} db_request = DBRequest( type=deregister_request_type, cbsd=cbsd, payload=request_dict, ) session.add(db_request) logger.debug(f"Added {db_request=}.") pass
def test_request_merging_merges_multiple_requests_into_one(self): # Given / When request_type = "registrationRequest" req_type = DBRequestType(name=request_type) reqs = [ DBRequest( cbsd_id=1, 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 cbsd = DBCbsd(id=1, cbsd_id="foo1", state=self.unregistered_state) db_request = DBRequest(id=1, cbsd_id=cbsd.id) db_response = DBResponse( id=1, request_id=1, response_code=0, payload=db_response_payload, ) self.session.add_all([cbsd, 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)