def get_state_by_id(self, state_id, strict=True): """Returns a state of the exploration, given its id.""" if state_id not in self.state_ids: raise Exception( 'Invalid state id %s for exploration %s' % (state_id, self.id)) return State.get(state_id, strict=strict)
def test_state_operations(self): """Test adding, renaming and checking existence of states.""" exploration = FakeExploration(owner_id='*****@*****.**') exploration.add_state('Initial state') self.assertEqual(len(exploration.state_ids), 1) default_state = State.get(exploration.state_ids[0]) default_state_name = default_state.name exploration.rename_state(default_state.id, 'Renamed state') self.assertEqual(len(exploration.state_ids), 1) self.assertEqual(default_state.name, 'Renamed state') # Add a new state. second_state = exploration.add_state('State 2') self.assertEqual(len(exploration.state_ids), 2) # It is OK to rename a state to itself. exploration.rename_state(second_state.id, second_state.name) self.assertEqual(second_state.name, 'State 2') # But it is not OK to add or rename a state using a name that already # exists. with self.assertRaises(Exception): exploration.add_state('State 2') with self.assertRaises(Exception): exploration.rename_state(second_state.id, 'Renamed state') # The exploration now has exactly two states. self.assertFalse(exploration._has_state_named(default_state_name)) self.assertTrue(exploration._has_state_named('Renamed state')) self.assertTrue(exploration._has_state_named('State 2'))