def create_new( user_id, title, category, exploration_id=None, init_state_name=feconf.DEFAULT_STATE_NAME, image_id=None): """Creates, saves and returns a new exploration id.""" # Generate a new exploration id, if one wasn't passed in. exploration_id = exploration_id or ExplorationModel.get_new_id(title) state_id = State.get_new_id(init_state_name) new_state = State(id=state_id, name=init_state_name) new_state.put() # Note that demo explorations do not have owners, so user_id may be None. exploration = ExplorationModel( id=exploration_id, title=title, category=category, image_id=image_id, state_ids=[state_id], editor_ids=[user_id] if user_id else []) exploration.put() return exploration.id
def test_exploration_class(self): """Test the Exploration model class.""" exploration = ExplorationModel(id='The exploration hash id') # A new exploration should have a default title property. self.assertEqual(exploration.title, 'New exploration') # A new exploration should have a default is_public property. self.assertEqual(exploration.is_public, False) state = State(id='The state hash id') state.put() # The 'state_ids' property must be a list of strings. with self.assertRaises(SyntaxError): exploration.state_ids = 'A string' exploration.put() with self.assertRaises(ValidationError): exploration.state_ids = [state] exploration.put() exploration.state_ids = [state.id] # An Exploration must have a category. with self.assertRaises(ValidationError): exploration.put() exploration.category = 'The category' # The 'parameters' property must be a list of Parameter objects. with self.assertRaises(ValidationError): exploration.parameters = 'A string' exploration.put() exploration.parameters = [] parameter = Parameter(name='theParameter', obj_type='Int') with self.assertRaises(AttributeError): exploration.parameters = [parameter.key] exploration.parameters = [parameter] # The 'is_public' property must be a boolean. with self.assertRaises(ValidationError): exploration.is_public = 'true' exploration.put() exploration.is_public = True # The 'image_id' property must be a string. # TODO: This fails now because of the behaviour of CharField image = Image(id='The image') with self.assertRaises(ValidationError): exploration.image_id = image exploration.put() exploration.image_id = 'A string' exploration.editor_ids = ['A user id'] # Put and retrieve the exploration. exploration.put() retrieved_exploration = Exploration.get('The exploration hash id') self.assertEqual(retrieved_exploration.category, 'The category') self.assertEqual(retrieved_exploration.title, 'New exploration') self.assertEqual(retrieved_exploration.state_ids, [state.id]) self.assertEqual(retrieved_exploration.parameters, [parameter]) self.assertEqual(retrieved_exploration.is_public, True) self.assertEqual(retrieved_exploration.image_id, 'A string') self.assertEqual(retrieved_exploration.editor_ids, ['A user id'])