def _build_state(self, session: Session) -> State: db_grant_idle_state_id = session.query(DBGrantState.id).filter( DBGrantState.name == GrantStates.IDLE.value, ).scalar() db_request_pending_state_id = session.query(DBRequestState.id).filter( DBRequestState.name == RequestStates.PENDING.value, ).scalar() # Selectively load sqlalchemy object relations using a single query to avoid commit races. # We want to have CBSD entity "grants" relation only contain grants in a Non-IDLE state. # We want to have CBSD entity "requests" relation only contain PENDING requests. db_configs = session.query(DBActiveModeConfig).join(DBCbsd).options( joinedload(DBActiveModeConfig.cbsd).options( joinedload(DBCbsd.channels), joinedload( DBCbsd.grants.and_( DBGrant.state_id != db_grant_idle_state_id, ), ), joinedload( DBCbsd.requests.and_( DBRequest.state_id == db_request_pending_state_id, ), ), ), ).filter(*self._get_filter()).populate_existing() configs = [self._build_config(db_config) for db_config in db_configs] session.commit() return State(active_mode_configs=configs)
def test_get_state_with_multiple_cbsds(self): some_cbsd = DBCbsdBuilder(). \ with_state(self.unregistered). \ with_registration('some'). \ with_eirp_capabilities(0, 10, 20, 1). \ with_active_mode_config(self.registered). \ build() other_cbsd = DBCbsdBuilder(). \ with_state(self.registered). \ with_registration('other'). \ with_eirp_capabilities(5, 15, 25, 3). \ with_active_mode_config(self.registered). \ build() self.session.add_all([some_cbsd, other_cbsd]) self.session.commit() some_config = ActiveModeConfigBuilder(). \ with_state(Unregistered). \ with_registration('some'). \ with_eirp_capabilities(0, 10, 20, 1). \ with_desired_state(Registered). \ build() other_config = ActiveModeConfigBuilder(). \ with_state(Registered). \ with_registration('other'). \ with_eirp_capabilities(5, 15, 25, 3). \ with_desired_state(Registered). \ build() expected = State(active_mode_configs=[some_config, other_config]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_should_send_data_if_cbsd_needs_deletion(self): cbsd = DBCbsdBuilder(). \ with_id(SOME_ID). \ with_state(self.registered). \ with_desired_state(self.registered). \ with_registration('some'). \ with_max_ibw(1000). \ with_carrier_aggregation(True). \ with_available_frequencies(FREQUENCIES). \ deleted(). \ build() self.session.add(cbsd) self.session.commit() am_cbsd = ActiveModeCbsdBuilder(). \ with_id(SOME_ID). \ with_state(Registered). \ with_desired_state(Registered). \ with_registration('some'). \ deleted(). \ with_category('b'). \ with_grant_settings( GrantSettings( grant_redundancy_enabled=True, carrier_aggregation_enabled=True, max_ibw_mhz=1000, available_frequencies=FREQUENCIES, ), ).build() # noqa: E123 expected = State(cbsds=[am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def _build_state(self, session: Session) -> State: db_configs = session.query(DBActiveModeConfig).join(DBCbsd).options( joinedload(DBActiveModeConfig.cbsd).options( joinedload(DBCbsd.channels), joinedload(DBCbsd.grants).options(joinedload(DBGrant.state)), ), ).filter(*self._get_filter()) configs = [self._build_config(session, x) for x in db_configs] session.commit() return State(active_mode_configs=configs)
def test_get_basic_state(self): cbsd = self._prepare_base_cbsd().build() self.session.add(cbsd) self.session.commit() am_cbsd = self._prepare_base_active_mode_cbsd().build() expected = State(cbsds=[am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_get_state_with_pending_requests(self): cbsd = self._prepare_base_cbsd(). \ with_request(self.grant, '{"key2":"value2"}'). \ build() self.session.add(cbsd) self.session.commit() expected = State() actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_not_get_state_with_single_step_enabled_and_without_installation_params(self): cbsd = self._prepare_base_cbsd(). \ with_single_step_enabled(). \ build() self.session.add(cbsd) self.session.commit() expected = State() actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_get_state_with_processed_requests(self): cbsd = self._prepare_base_cbsd(). \ with_request(self.processed, self.grant, '{"key1":"value1"}'). \ build() self.session.add(cbsd) self.session.commit() config = self._prepare_base_active_mode_config().build() expected = State(cbsds=[config]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_not_get_state_without_registration_params(self): cbsd = DBCbsdBuilder(). \ with_state(self.unregistered). \ with_eirp_capabilities(0, 10, 20, 1). \ with_active_mode_config(self.registered). \ build() self.session.add(cbsd) self.session.commit() expected = State() actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_not_get_state_with_single_step_enabled_category_a_and_outdoor(self): cbsd = self._prepare_base_cbsd(). \ with_category('a'). \ with_installation_params(1, 2, 3, 'AGL', False). \ with_single_step_enabled(). \ build() self.session.add(cbsd) self.session.commit() expected = State() actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_not_get_state_without_eirp_capabilities(self): cbsd = DBCbsdBuilder(). \ with_state(self.unregistered). \ with_registration('some'). \ with_desired_state(self.registered). \ build() self.session.add(cbsd) self.session.commit() expected = State() actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_get_state_with_last_seen(self): cbsd = self._prepare_base_cbsd(). \ with_last_seen(1). \ build() self.session.add(cbsd) self.session.commit() config = self._prepare_base_active_mode_config(). \ with_last_seen(1). \ build() expected = State(cbsds=[config]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_get_state_for_cbsd_marked_for_deletion(self): cbsd = self._prepare_base_cbsd(). \ deleted(). \ build() self.session.add(cbsd) self.session.commit() config = self._prepare_base_active_mode_config(). \ deleted(). \ build() expected = State(active_mode_configs=[config]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_get_state_with_frequency_preferences(self): cbsd = self._prepare_base_cbsd(). \ with_preferences(15, [3600, 3580, 3620]). \ build() self.session.add(cbsd) self.session.commit() am_cbsd = self._prepare_base_active_mode_cbsd(). \ with_preferences(15, [3600, 3580, 3620]). \ build() expected = State(cbsds=[am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(actual, expected)
def test_get_state_for_single_step(self): cbsd = self._prepare_base_cbsd(). \ with_single_step_enabled(). \ with_installation_params(1, 2, 3, 'AGL', True). \ build() self.session.add(cbsd) self.session.commit() am_cbsd = self._prepare_base_active_mode_cbsd(). \ with_single_step_enabled(). \ with_installation_params(1, 2, 3, 'AGL', True). \ build() expected = State(cbsds=[am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def test_get_state_with_channels(self): cbsd = self._prepare_base_cbsd(). \ with_channel(1, 2, 3). \ with_channel(5, 6). \ build() self.session.add(cbsd) self.session.commit() am_cbsd = self._prepare_base_active_mode_cbsd(). \ with_channel(1, 2, 3). \ with_channel(5, 6). \ build() expected = State(cbsds=[am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(actual, expected)
def test_get_state_with_grants(self): cbsd = self._prepare_base_cbsd(). \ with_grant("granted_grant", self.granted, 3). \ with_grant("authorized_grant", self.authorized, 5, 6). \ build() self.session.add(cbsd) self.session.commit() am_cbsd = self._prepare_base_active_mode_cbsd(). \ with_grant("granted_grant", Granted, 3, 0). \ with_grant("authorized_grant", Authorized, 5, 6). \ build() expected = State(cbsds=[am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(actual, expected)
def test_get_state_with_multiple_cbsds(self): some_cbsd = self._prepare_base_cbsd(). \ with_id(SOME_ID). \ with_registration('some'). \ build() other_cbsd = self._prepare_base_cbsd(). \ with_id(OTHER_ID). \ with_registration('other'). \ build() self.session.add_all([some_cbsd, other_cbsd]) self.session.commit() some_am_cbsd = self._prepare_base_active_mode_cbsd(). \ with_id(SOME_ID). \ with_registration('some'). \ build() other_am_cbsd = self._prepare_base_active_mode_cbsd(). \ with_id(OTHER_ID). \ with_registration('other'). \ build() expected = State(cbsds=[some_am_cbsd, other_am_cbsd]) actual = self.amc_service.GetState(GetStateRequest(), None) self.assertEqual(expected, actual)
def _build_state(db_cbsds: List[DBCbsd]) -> State: cbsds = [_build_cbsd(db_cbsd) for db_cbsd in db_cbsds] return State(cbsds=cbsds)