def test_yaml_methods(self): """Test the as_yaml() and create_from_yaml() methods.""" exploration = Exploration.create( self.user, 'A title', 'A category', 'A different exploration_id') exploration.add_state('New state') yaml_file = exploration.as_yaml() self.assertEqual(yaml_file, """Activity 1: content: [] param_changes: [] widget: handlers: - name: submit rules: - dest: Activity 1 feedback: [] inputs: {} name: Default param_changes: [] params: {} sticky: false widget_id: Continue New state: content: [] param_changes: [] widget: handlers: - name: submit rules: - dest: New state feedback: [] inputs: {} name: Default param_changes: [] params: {} sticky: false widget_id: Continue """) exploration2 = Exploration.create_from_yaml( yaml_file, self.user, 'Title', 'Category') self.assertEqual(len(exploration2.states), 2) self.assertEqual(exploration2.as_yaml(), yaml_file) self.assertEqual(Exploration.query().count(), 2) with self.assertRaises(Exception): Exploration.create_from_yaml( 'No_initial_state_name', self.user, 'Title', 'category') with self.assertRaises(Exception): Exploration.create_from_yaml( 'Invalid\ninit_state_name:\nMore stuff', self.user, 'Title', 'category') with self.assertRaises(Exception): Exploration.create_from_yaml( 'State1:\n(\nInvalid yaml', self.user, 'Title', 'category') # Check that no new exploration was created. self.assertEqual(Exploration.query().count(), 2)
def test_incomplete_and_default_flags(self): InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') state_id = exp.init_state.get().id # Hit the default once, and do an incomplete twice. The result should # be classified as incomplete. for i in range(3): EventHandler.record_state_hit('eid', state_id) EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 2) self.assertEquals(states[0]['type'], 'incomplete') # Now hit the default two more times. The result should be classified # as default. for i in range(2): EventHandler.record_state_hit('eid', state_id) EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default')
def test_state_operations(self): """Test adding, renaming and checking existence of states.""" exploration = Exploration.create(self.user, 'Title', 'Category', 'eid') exploration.put() self.assertEqual(len(exploration.states), 1) default_state = exploration.states[0].get() default_state_name = default_state.name exploration.rename_state(default_state, 'Renamed state') self.assertEqual(len(exploration.states), 1) self.assertEqual(default_state.name, 'Renamed state') # Add a new state. second_state = exploration.add_state('State 2') self.assertEqual(len(exploration.states), 2) # It is OK to rename a state to itself. exploration.rename_state(second_state, 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, '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'))
def test_get_top_ten_improvable_states(self): InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') state_id = exp.init_state.get().id EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='2') EventHandler.record_rule_hit( 'eid', state_id, Rule(name='Default', dest=state_id), extra_info='1') EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) EventHandler.record_state_hit('eid', state_id) states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 1) self.assertEquals(states[0]['exp_id'], 'eid') self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['state_id'], exp.init_state.get().id)
def post(self, user): """Handles POST requests.""" payload = json.loads(self.request.get('payload')) title = payload.get('title') category = payload.get('category') if not title: raise self.InvalidInputException('No title supplied.') if not category: raise self.InvalidInputException('No category chosen.') yaml = self.request.get('yaml') if yaml and feconf.ALLOW_YAML_FILE_UPLOAD: exploration = Exploration.create_from_yaml( yaml_file=yaml, user=user, title=title, category=category) else: exploration = Exploration.create( user, title=title, category=category) self.response.write(json.dumps({ 'explorationId': exploration.id, }))
def test_dataset_operations(self): """Test adding, deleting and adding values to a dataset.""" exploration = Exploration.create(self.user, 'Title', 'Category', 'eid') exploration.put() self.assertEqual(len(exploration.datasets), 0) exploration.add_dataset('Dataset 1', ['Name', 'Number']) self.assertEqual(len(exploration.datasets), 1) self.assertEqual(exploration.datasets[0].name, 'Dataset 1') self.assertIsNone(exploration.datasets[0].get_random_param_set()) # The same dataset name cannot be used more than once. with self.assertRaises(Exception): exploration.add_dataset('Dataset 1', ['Name', 'Number']) # Parameter names cannot be repeated across datasets. with self.assertRaises(Exception): exploration.add_dataset('Dataset 2', ['Name']) # It is not possible to add data to a non-existent dataset. with self.assertRaises(Exception): exploration.add_data_to_dataset('Fake dataset', []) exploration.add_data_to_dataset('Dataset 1', []) self.assertIsNone(exploration.datasets[0].get_random_param_set()) # Parameter set keys must match the dataset schema. with self.assertRaises(Exception): exploration.add_data_to_dataset('Dataset 1', [{'Fake Key': 'John'}]) with self.assertRaises(KeyError): exploration.add_data_to_dataset('Dataset 1', [{'Name': 'John'}]) exploration.add_data_to_dataset( 'Dataset 1', [{'Name': 'John', 'Number': '123'}]) param_set = exploration.datasets[0].get_random_param_set() self.assertEqual(len(param_set.params), 2) self.assertEqual(param_set.params[0].name, 'Name') self.assertEqual(param_set.params[0].obj_type, 'UnicodeString') self.assertEqual(param_set.params[0].values[0], 'John') self.assertEqual(param_set.params[1].name, 'Number') self.assertEqual(param_set.params[1].obj_type, 'UnicodeString') self.assertEqual(param_set.params[1].values[0], '123') param_set_key = param_set.key self.assertTrue(ParamSet.get_by_id(param_set_key.id())) with self.assertRaises(Exception): exploration.delete_dataset('Fake dataset') exploration.add_dataset('Dataset 2', ['Address', 'Zip code']) self.assertEqual(len(exploration.datasets), 2) exploration.delete_dataset('Dataset 1') self.assertEqual(len(exploration.datasets), 1) self.assertIsNone(ParamSet.get_by_id(param_set_key.id()))
def setUp(self): """Loads the default widgets and create sample users and explorations.""" super(StateModelUnitTests, self).setUp() InteractiveWidget.load_default_widgets() self.user = User(email="*****@*****.**") self.another_user = User(email="*****@*****.**") self.exploration = Exploration.create(self.user, "A title", "A category", "A exploration_id") self.exploration.put()
def test_editor_permissions(self): """Test permissions.""" exploration = Exploration.create(self.user, 'Title', 'Category', 'eid') exploration.put() self.assertTrue(exploration.is_editable_by(self.user)) self.assertFalse(exploration.is_editable_by(self.another_user)) self.assertItemsEqual( Exploration.get_viewable_explorations(self.user), [exploration]) self.assertItemsEqual( Exploration.get_viewable_explorations(self.another_user), [])
def test_create_get_and_delete_exploration(self): """Test the create(), get() and delete() methods.""" exploration = Exploration.create( self.user, 'A title', 'A category', 'A exploration_id') exploration.put() retrieved_exploration = Exploration.get('A exploration_id') self.assertEqual(exploration, retrieved_exploration) exploration.delete() with self.assertRaises(Exception): retrieved_exploration = Exploration.get('A exploration_id')
def setUp(self): """Loads the default widgets.""" super(ExplorationServicesUnitTests, self).setUp() InteractiveWidget.load_default_widgets() self.owner = User(email='*****@*****.**') self.editor = User(email='*****@*****.**') self.viewer = User(email='*****@*****.**') self.exploration = Exploration.create( self.owner, 'A title', 'A category', 'A exploration_id') self.exploration.editors.append(self.editor) self.exploration.put()
def test_two_state_default_hit(self): SECOND_STATE = 'State 2' InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') second_state = exp.add_state(SECOND_STATE) state_1_id = exp.init_state.get().id state_2_id = second_state.id # Hit the default rule of state 1 once, and the default rule of state 2 # twice. EventHandler.record_state_hit('eid', state_1_id) EventHandler.record_rule_hit( 'eid', state_1_id, Rule(name='Default', dest=state_1_id), extra_info='1') for i in range(2): EventHandler.record_state_hit('eid', state_2_id) EventHandler.record_rule_hit( 'eid', state_2_id, Rule(name='Default', dest=state_2_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 2) self.assertEquals(states[0]['rank'], 2) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state_2_id) self.assertEquals(states[1]['rank'], 1) self.assertEquals(states[1]['type'], 'default') self.assertEquals(states[1]['state_id'], state_1_id) # Hit the default rule of state 1 two more times. for i in range(2): EventHandler.record_state_hit('eid', state_1_id) EventHandler.record_rule_hit( 'eid', state_1_id, Rule(name='Default', dest=state_1_id), extra_info='1') states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 2) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default') self.assertEquals(states[0]['state_id'], state_1_id) self.assertEquals(states[1]['rank'], 2) self.assertEquals(states[1]['type'], 'default') self.assertEquals(states[1]['state_id'], state_2_id)
def test_no_improvement_flag_hit(self): InteractiveWidget.load_default_widgets() exp = Exploration.create( User(email='*****@*****.**'), 'exploration', 'category', 'eid') init_state = exp.init_state.get() state_id = init_state.id init_state.widget.handlers[0].rules = [ Rule(name='NotDefault', dest=state_id), Rule(name='Default', dest=state_id), ] init_state.put() EventHandler.record_rule_hit( 'eid', state_id, Rule(name='NotDefault', dest=state_id), extra_info='1') EventHandler.record_state_hit('eid', state_id) states = Statistics.get_top_ten_improvable_states(['eid']) self.assertEquals(len(states), 0)