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 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_create_db_request_inserts_requests_to_db_when_cbsdId_argument_present_in_payload( self, payload, expected, result_type): # Given foo_type = DBRequestType(id=1, name="foo_type") foo_state = DBRequestState(id=1, name="foo_state") self.session.add_all([foo_type, foo_state]) self.session.commit() # When result = self.rc_service._create_db_request(foo_type, foo_state, payload) requests = self.session.query(DBRequest).all() # Then self.assertEqual(expected, len(requests)) self.assertIsInstance(result, result_type)
def initialize(self) -> None: with self.session_manager.session_scope() as s: for _type in RequestTypes: if not s.query(DBRequestType).filter( DBRequestType.name == _type.value).first(): request_type = DBRequestType(name=_type.value) s.add(request_type) for state in RequestStates: if not s.query(DBRequestState).filter( DBRequestState.name == state.value).first(): request_state = DBRequestState(name=state.value) s.add(request_state) for state in GrantStates: if not s.query(DBGrantState).filter( DBGrantState.name == state.value).first(): grant_state = DBGrantState(name=state.value) s.add(grant_state) s.commit()
def test_store_requests_from_map_stores_requests_in_db( self, request_map, expected_db_ids, expected_ids_returned_by_store_requests_from_map_method): # Given request_map = request_map if request_map: for type in request_map.keys(): self.session.add(DBRequestType(name=type)) request_pending_state = DBRequestState( name=RequestStates.PENDING.value) self.session.add(request_pending_state) self.session.commit() # When response_ids = self.rc_service._store_requests_from_map(request_map) db_request_ids = self.session.query(DBRequest.id).all() db_request_ids = [_id for (_id, ) in db_request_ids] # Then self.assertListEqual(db_request_ids, expected_db_ids) self.assertListEqual( response_ids, expected_ids_returned_by_store_requests_from_map_method)
def test_processor_splits_sas_response_into_separate_db_objects_and_links_them_with_requests( self, processor_strategy, requests_fixtures): # When request_type_name = self._get_request_type_from_fixture( requests_fixtures) response_type_name = request_response[request_type_name] pending_state = DBRequestState(name=RequestStates.PENDING.value) processed_state = DBRequestState(name=RequestStates.PROCESSED.value) grant_state_idle = DBGrantState(name=GrantStates.IDLE.value) grant_state_granted = DBGrantState(name=GrantStates.GRANTED.value) grant_state_authorized = DBGrantState( name=GrantStates.AUTHORIZED.value) request_type = DBRequestType(name=request_type_name) db_requests = self._create_db_requests_from_fixture( request_state=pending_state, request_type=request_type, fixture=requests_fixtures) nr_of_requests = len(db_requests) response_payload = self._create_response_payload_from_db_requests( response_type_name=response_type_name, db_requests=db_requests) any_url = 'https://foo.com/foobar' responses.add(responses.GET, any_url, json=response_payload, status=200) response = requests.get( any_url ) # url and method don't matter, I'm just crafting a qualified response here self.session.add_all([pending_state, processed_state]) self.session.add_all( [grant_state_idle, grant_state_granted, grant_state_authorized]) self.session.add_all(db_requests) self.session.commit() processor = ResponseDBProcessor( response_type_name, request_map_key_func=processor_strategy["request_map_key"], response_map_key_func=processor_strategy["response_map_key"], process_responses_func=processor_strategy["process_responses"], ) processor.process_response(db_requests, response, self.session) self.session.commit() # Then self.assertEqual(2, self.session.query(DBRequestState).count()) self.assertEqual( 1, self.session.query(DBRequestType).filter( DBRequestType.name == request_type_name).count()) self.assertEqual(nr_of_requests, self.session.query(DBRequest).count()) self.assertListEqual( [r.id for r in db_requests], [_id for (_id, ) in self.session.query(DBResponse.id).all()]) self.assertListEqual( ["processed"] * nr_of_requests, [r.state.name for r in self.session.query(DBRequest).all()])