def test_count_explorations(self): """Test count_explorations().""" self.assertEqual(exp_services.count_explorations(), 0) exp_services.create_new(self.owner_id, "A title", "A category", "A exploration_id") self.assertEqual(exp_services.count_explorations(), 1) exp_services.create_new(self.owner_id, "A new title", "A category", "A new exploration_id") self.assertEqual(exp_services.count_explorations(), 2)
def test_get_all_explorations(self): """Test get_all_explorations().""" exploration = Exploration.get( exp_services.create_new(self.owner_id, "A title", "A category", "A exploration_id") ) self.assertItemsEqual([e.id for e in exp_services.get_all_explorations()], [exploration.id]) exploration2 = Exploration.get( exp_services.create_new(self.owner_id, "A new title", "A category", "A new exploration_id") ) self.assertItemsEqual([e.id for e in exp_services.get_all_explorations()], [exploration.id, exploration2.id])
def test_get_top_ten_improvable_states(self): exp = Exploration.get(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) state_id = exp.init_state_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 = stats_services.get_top_ten_improvable_states([exp]) 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_id)
def test_incomplete_and_default_flags(self): exp = Exploration.get(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) state_id = exp.init_state_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 = stats_services.get_top_ten_improvable_states([exp]) 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 = stats_services.get_top_ten_improvable_states([exp]) self.assertEquals(len(states), 1) self.assertEquals(states[0]['rank'], 3) self.assertEquals(states[0]['type'], 'default')
def setUp(self): """Loads the default widgets and creates a sample exploration.""" super(StateModelUnitTests, self).setUp() InteractiveWidget.load_default_widgets() self.user_id = '*****@*****.**' self.exploration = Exploration.get(exp_services.create_new( self.user_id, 'A title', 'A category', 'A exploration_id'))
def test_get_public_explorations(self): exploration = Exploration.get( exp_services.create_new(self.owner_id, "A title", "A category", "A exploration_id") ) self.assertEqual(exp_services.get_public_explorations(), []) exploration.is_public = True exploration.put() self.assertEqual([e.id for e in exp_services.get_public_explorations()], [exploration.id])
def test_creation_and_deletion_of_individual_explorations(self): """Test the create_new() and delete() methods.""" exploration = Exploration.get( exp_services.create_new(self.owner_id, "A title", "A category", "A exploration_id") ) exploration.put() retrieved_exploration = Exploration.get("A exploration_id") self.assertEqual(exploration.id, retrieved_exploration.id) self.assertEqual(exploration.title, retrieved_exploration.title) exploration.delete() with self.assertRaises(Exception): retrieved_exploration = Exploration.get("A exploration_id")
def test_two_state_default_hit(self): SECOND_STATE = 'State 2' exp = Exploration.get(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) second_state = exp.add_state(SECOND_STATE) state_1_id = exp.init_state_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 = stats_services.get_top_ten_improvable_states([exp]) 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 = stats_services.get_top_ten_improvable_states([exp]) 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): exp = Exploration.get(exp_services.create_new( '*****@*****.**', 'exploration', 'category', 'eid')) init_state = exp.init_state init_state.widget.handlers[0].rules = [ Rule(name='NotDefault', dest=init_state.id), Rule(name='Default', dest=init_state.id), ] init_state.put() EventHandler.record_rule_hit( 'eid', init_state.id, Rule(name='NotDefault', dest=init_state.id), extra_info='1') EventHandler.record_state_hit('eid', init_state.id) states = stats_services.get_top_ten_improvable_states([exp]) self.assertEquals(len(states), 0)
def test_export_to_yaml(self): """Test the export_to_yaml() method.""" exploration = Exploration.get( exp_services.create_new(self.owner_id, "A title", "A category", "A different exploration_id") ) exploration.add_state("New state") yaml_content = exp_services.export_to_yaml(exploration.id) self.assertEqual( yaml_content, """parameters: [] states: - content: [] name: '[untitled state]' param_changes: [] widget: handlers: - name: submit rules: - dest: '[untitled state]' feedback: [] inputs: {} name: Default param_changes: [] params: {} sticky: false widget_id: interactive-Continue - content: [] name: New state param_changes: [] widget: handlers: - name: submit rules: - dest: New state feedback: [] inputs: {} name: Default param_changes: [] params: {} sticky: false widget_id: interactive-Continue """, )
def post(self): """Handles POST requests.""" title = self.payload.get('title') category = self.payload.get('category') if not title: raise self.InvalidInputException('No title supplied.') if not category: raise self.InvalidInputException('No category chosen.') yaml_content = self.request.get('yaml') if yaml_content and feconf.ALLOW_YAML_FILE_UPLOAD: exploration_id = exp_services.create_from_yaml( yaml_content, self.user_id, title, category) else: exploration_id = exp_services.create_new( self.user_id, title=title, category=category) self.render_json({'explorationId': exploration_id})
def test_get_editable_explorations(self): exploration = Exploration.get( exp_services.create_new(self.owner_id, "A title", "A category", "A exploration_id") ) exploration.add_editor(self.editor_id) exploration.put() def get_editable_ids(user_id): return [e.id for e in exp_services.get_editable_explorations(user_id)] self.assertEqual(get_editable_ids(self.owner_id), [exploration.id]) self.assertEqual(get_editable_ids(self.viewer_id), []) self.assertEqual(get_editable_ids(None), []) # Set the exploration's status to published. exploration.is_public = True exploration.put() self.assertEqual(get_editable_ids(self.owner_id), [exploration.id]) self.assertEqual(get_editable_ids(self.viewer_id), []) self.assertEqual(get_editable_ids(None), [])