def _require_playthrough_data_is_valid(self, playthrough_data): """Checks whether the playthrough data dict has the correct keys. Args: playthrough_data: dict. Dict representing a playthrough. """ playthrough_properties = [ 'exp_id', 'exp_version', 'issue_type', 'issue_customization_args', 'playthrough_actions', 'is_valid' ] for playthrough_property in playthrough_properties: if playthrough_property not in playthrough_data: raise self.InvalidInputException( '%s not in playthrough data dict.' % playthrough_property) playthrough_actions = [ stats_domain.LearnerAction.from_dict(playthrough_action_dict) for playthrough_action_dict in playthrough_data['playthrough_actions'] ] dummy_playthrough = stats_domain.Playthrough( 'dummy_playthrough_id', playthrough_data['exp_id'], playthrough_data['exp_version'], playthrough_data['issue_type'], playthrough_data['issue_customization_args'], playthrough_actions, playthrough_data['is_valid']) try: dummy_playthrough.validate() except utils.ValidationError as e: raise self.InvalidInputException(e)
def get_playthroughs_multi(playthrough_ids): """Retrieves multiple Playthrough domain objects. Args: playthrough_ids: list(str). List of playthrough IDs. Returns: list(Playthrough). List of playthrough domain objects. """ playthrough_instances = stats_models.PlaythroughModel.get_multi( playthrough_ids) # TODO(pranavsid98): Replace below lines with get_from_model. playthroughs = [] for playthrough_instance in playthrough_instances: playthrough_actions = [] for action in playthrough_instance.playthrough_actions: playthrough_actions.append( stats_domain.LearnerAction.from_dict(action)) playthroughs.append( stats_domain.Playthrough( playthrough_instance.id, playthrough_instance.exp_id, playthrough_instance.exp_version, playthrough_instance.issue_type, playthrough_instance.issue_customization_args, playthrough_actions)) return playthroughs
def get_playthrough_from_model(playthrough_model): """Gets a PlaythroughModel domain object from a PlaythroughModel instance. Args: playthrough_model: PlaythroughModel. Playthrough model in datastore. Returns: Playthrough. The domain object for a playthrough. """ actions = [] for action_dict in playthrough_model.actions: _migrate_to_latest_action_schema(action_dict) actions.append(stats_domain.LearnerAction.from_dict(action_dict)) return stats_domain.Playthrough( playthrough_model.exp_id, playthrough_model.exp_version, playthrough_model.issue_type, playthrough_model.issue_customization_args, actions)
def test_to_dict(self): playthrough = stats_domain.Playthrough( 'playthrough_id1', 'exp_id1', 1, 'EarlyQuit', { 'state_name': { 'value': 'state_name1' }, 'time_spent_in_exp_in_msecs': { 'value': 200 } }, [{ 'action_id': 'ExplorationStart', 'action_customization_args': { 'state_name': { 'value': 'state_name1' } } }], True) playthrough_dict = playthrough.to_dict() self.assertEqual(playthrough_dict['id'], 'playthrough_id1') self.assertEqual(playthrough_dict['exp_id'], 'exp_id1') self.assertEqual(playthrough_dict['exp_version'], 1) self.assertEqual(playthrough_dict['issue_id'], 'EarlyQuit') self.assertEqual( playthrough_dict['issue_customization_args'], { 'state_name': { 'value': 'state_name1' }, 'time_spent_in_exp_in_msecs': { 'value': 200 } }) self.assertEqual(playthrough_dict['playthrough_actions'], [{ 'action_id': 'ExplorationStart', 'action_customization_args': { 'state_name': { 'value': 'state_name1' } } }]) self.assertEqual(playthrough_dict['is_valid'], True)
def test_validate(self): playthrough = stats_domain.Playthrough( 'playthrough_id1', 'exp_id1', 1, 'EarlyQuit', { 'state_name': { 'value': 'state_name1' }, 'time_spent_in_exp_in_msecs': { 'value': 200 } }, [{ 'action_id': 'ExplorationStart', 'action_customization_args': { 'state_name': { 'value': 'state_name1' } } }], True) playthrough.validate() # Change ID to int. playthrough.id = 5 with self.assertRaisesRegexp( utils.ValidationError, ('Expected ID to be a string, received %s' % (type(5)))): playthrough.validate() # Change exp_version to string. playthrough.id = 'playthrough_id1' playthrough.exp_version = '1' with self.assertRaisesRegexp( utils.ValidationError, ('Expected exp_version to be an int, received %s' % (type('1')))): playthrough.validate() # Change to invalid issue_id. playthrough.exp_version = 1 playthrough.issue_id = 'InvalidIssueId' with self.assertRaisesRegexp( utils.ValidationError, ('Invalid issue ID: %s' % playthrough.issue_id)): playthrough.validate() # Change playthrough_actions list. playthrough.issue_id = 'EarlyQuit' playthrough.playthrough_actions = [{ 'action_id': 'ExplorationStart', }] with self.assertRaisesRegexp( utils.ValidationError, ('KeyError in an element of playthrough_actions')): playthrough.validate() # Change to invalid action_id. playthrough.playthrough_actions = [{ 'action_id': 'InvalidActionId', 'action_customization_args': { 'state_name': { 'value': 'state_name1' } } }] with self.assertRaisesRegexp( utils.ValidationError, ('Invalid action ID: %s' % 'InvalidActionId')): playthrough.validate()