def test_get_id_from_name(self): """Test converting state names to ids.""" id_1 = "123" name_1 = "State 1" State.create(self.exploration, name_1, state_id=id_1) self.assertEqual(State._get_id_from_name(name_1, self.exploration), id_1) with self.assertRaises(Exception): State._get_id_from_name("fake_name", self.exploration) self.assertEqual(State._get_id_from_name(feconf.END_DEST, self.exploration), feconf.END_DEST)
def create(cls, user, title, category, exploration_id=None, init_state_name="Activity 1", image_id=None): """Creates and returns a new exploration.""" # Generate a new exploration id, if one wasn't passed in. exploration_id = exploration_id or cls.get_new_id(title) # Temporarily create a fake initial state key. state_id = State.get_new_id(init_state_name) fake_state_key = ndb.Key(Exploration, exploration_id, State, state_id) # Note that demo explorations do not have owners, so user may be None. exploration = cls( id=exploration_id, title=title, init_state=fake_state_key, category=category, image_id=image_id, states=[fake_state_key], editors=[user] if user else [], ) exploration.put() # Finally, create the initial state and check that it has the right key. new_init_state = State.create(exploration, init_state_name, state_id=state_id) assert fake_state_key == new_init_state.key return exploration
def add_state(self, state_name): """Adds a new state, and returns it.""" if self._has_state_named(state_name): raise Exception("Duplicate state name %s" % state_name) state = State.create(self, state_name) self.states.append(state.key) self.put() return state
def test_create_and_get_state(self): """Test creation and retrieval of states.""" id_1 = "123" name_1 = "State 1" state_1 = State.create(self.exploration, name_1, state_id=id_1) fetched_state_1 = State.get(id_1, parent=self.exploration.key) self.assertEqual(fetched_state_1, state_1) fetched_state_by_name_1 = State.get_by_name(name_1, self.exploration) self.assertEqual(fetched_state_by_name_1, state_1) # Test the failure cases. id_2 = "fake_id" name_2 = "fake_name" with self.assertRaises(Exception): State.get(id_2, parent=self.exploration.key) fetched_state_by_name_2 = State.get_by_name(name_2, self.exploration, strict=False) self.assertIsNone(fetched_state_by_name_2) with self.assertRaises(Exception): State.get_by_name(name_2, self.exploration, strict=True) # The default behavior is to fail noisily. with self.assertRaises(Exception): State.get_by_name(name_2, self.exploration)