def __init__(self, subcategory=None):
     self.subcategory = subcategory
     if subcategory:
         # this is the set of recommendations for a given subcategory
         cat_title = u"Recommended For You in %s" % (
                                              subcategory.untranslated_name)
         tr_title = utf8(_("Recommended For You in %s")) % utf8(
                                                           subcategory.name)
     else:
         # this is the full set of recommendations for e.g. the lobby view
         cat_title = u"Recommended For You"
         tr_title = _("Recommended For You")
     super(RecommendedForYouCategory, self).__init__(
             cat_title,
             tr_title,
             None,
             xapian.Query(),
             flags=['available-only', 'not-installed-only'],
             item_limit=60)
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect(
         "recommend-me", self._recommend_me_result)
     self.recommender_agent.connect(
         "error", self._recommender_agent_error)
     self.recommender_agent.query_recommend_me()
Example #2
0
 def test_mocked_recagent_post_submit_profile(self, mock_spawn_helper_run):
     recommender_agent = RecommenderAgent()
     recommender_agent._calc_profile_id = lambda profile: "i-am-random"
     db = get_test_db()
     recommender_agent.post_submit_profile(db)
     args, kwargs =  mock_spawn_helper_run.call_args
     # ensure we have packages in the package list and the
     # kwargs have the names we expect
     self.assertNotEqual(kwargs['data'][0]['package_list'], [])
Example #3
0
 def test_mocked_recagent_post_submit_profile(self, mock_spawn_helper_run):
     recommender_agent = RecommenderAgent()
     recommender_agent._calc_profile_id = lambda profile: "i-am-random"
     db = get_test_db()
     recommender_agent.post_submit_profile(db)
     args, kwargs = mock_spawn_helper_run.call_args
     # ensure we have packages in the package list and the
     # kwargs have the names we expect
     self.assertNotEqual(kwargs['data'][0]['package_list'], [])
Example #4
0
class RecommendationsPanel(FramedHeaderBox):
    """
    Base class for widgets that display recommendations
    """

    __gsignals__ = {"application-activated": (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT,))}

    def __init__(self):
        FramedHeaderBox.__init__(self)
        self.recommender_agent = RecommenderAgent()
        # keep track of applications that have been viewed via a
        # recommendation so that we can detect when a recommended app
        # has been installed
        self.recommended_apps_viewed = set()
        self.backend = get_install_backend()
        self.backend.connect("transaction-started", self._on_transaction_started)
        self.backend.connect("transaction-finished", self._on_transaction_finished)

    def _on_application_activated(self, tile, app):
        self.emit("application-activated", app)
        # we only report on items if the user has opted-in to the
        # recommendations service
        if self.recommender_agent.is_opted_in():
            self.recommended_apps_viewed.add(app.pkgname)
            if network_state_is_connected():
                # let the recommendations service know that a
                # recommended item has been viewed (if it is
                # subsequently installed we will send an additional
                # signal to indicate that, in on_transaction_finished)
                # (there is no need to monitor for an error, etc., for this)
                self.recommender_agent.post_implicit_feedback(app.pkgname, RecommenderFeedbackActions.VIEWED)

    def _on_transaction_started(self, backend, pkgname, appname, trans_id, trans_type):
        if trans_type != TransactionTypes.INSTALL and pkgname in self.recommended_apps_viewed:
            # if the transaction is not an installation we don't want to
            # track it as a recommended item
            self.recommended_apps_viewed.remove(pkgname)

    def _on_transaction_finished(self, backend, result):
        if result.pkgname in self.recommended_apps_viewed:
            self.recommended_apps_viewed.remove(result.pkgname)
            if network_state_is_connected():
                # let the recommendations service know that a
                # recommended item has been successfully installed
                # (there is no need to monitor for an error, etc., for this)
                self.recommender_agent.post_implicit_feedback(result.pkgname, RecommenderFeedbackActions.INSTALLED)

    def _on_recommender_agent_error(self, agent, msg):
        LOG.warn("Error while accessing the recommender agent: %s" % msg)
        # this can happen if:
        #  - there is a real error from the agent
        #  - no cached data is available
        # hide the panel on an error
        self.hide()
Example #5
0
 def __init__(self):
     FramedHeaderBox.__init__(self)
     self.recommender_agent = RecommenderAgent()
     # keep track of applications that have been viewed via a
     # recommendation so that we can detect when a recommended app
     # has been installed
     self.recommended_apps_viewed = set()
     self.backend = get_install_backend()
     self.backend.connect("transaction-started",
                          self._on_transaction_started)
     self.backend.connect("transaction-finished",
                          self._on_transaction_finished)
Example #6
0
 def __init__(self, pkgname):
     super(AppRecommendationsCategory,
           self).__init__(u"People Also Installed",
                          _(u"People Also Installed"),
                          None,
                          xapian.Query(),
                          flags=['available-only', 'not-installed-only'],
                          item_limit=4)
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect("recommend-app",
                                    self._recommend_app_result)
     self.recommender_agent.connect("error", self._recommender_agent_error)
     self.recommender_agent.query_recommend_app(pkgname)
Example #7
0
 def setUp(self):
     self.loop = GLib.MainLoop(GLib.main_context_default())
     self.error = False
     if "SOFTWARE_CENTER_RECOMMENDER_HOST" in os.environ:
         orig_host = os.environ.get("SOFTWARE_CENTER_RECOMMENDER_HOST")
         self.addCleanup(os.environ.__setitem__,
                         "SOFTWARE_CENTER_RECOMMENDER_HOST", orig_host)
     else:
         self.addCleanup(os.environ.pop, "SOFTWARE_CENTER_RECOMMENDER_HOST")
     server = "https://rec.staging.ubuntu.com"
     os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = server
     # most tests need it
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect("error", self.on_query_error)
 def disabled_test_recagent_query_recommend_all_apps(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("recommend-all-apps", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_recommend_all_apps()
     self.loop.run()
     self.assertFalse(self.error)
 def test_recagent_query_server_status(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("server-status", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_server_status()
     self.loop.run()
     self.assertFalse(self.error)
 def disabled_test_recagent_query_profile(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("profile", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_profile(pkgnames=["pitivi", "fretsonfire"])
     self.loop.run()
     self.assertFalse(self.error)
Example #11
0
 def __init__(self, subcategory=None):
     self.subcategory = subcategory
     if subcategory:
         # this is the set of recommendations for a given subcategory
         cat_title = u"Recommended For You in %s" % (
                                              subcategory.untranslated_name)
         tr_title = utf8(_("Recommended For You in %s")) % utf8(
                                                           subcategory.name)
     else:
         # this is the full set of recommendations for e.g. the lobby view
         cat_title = u"Recommended For You"
         tr_title = _("Recommended For You")
     super(RecommendedForYouCategory, self).__init__(
             cat_title,
             tr_title,
             None,
             xapian.Query(),
             flags=['available-only', 'not-installed-only'],
             item_limit=60)
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect(
         "recommend-me", self._recommend_me_result)
     self.recommender_agent.connect(
         "error", self._recommender_agent_error)
     self.recommender_agent.query_recommend_me()
 def disabled_test_recagent_post_submit_profile(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("submit-profile-finished", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     db = get_test_db()
     recommender_agent.post_submit_profile(db)
     self.loop.run()
     self.assertFalse(self.error)
 def test_recagent_query_error(self):
     # NOTE: This tests the error condition itself! it simply forces an error
     #       'cuz there definitely isn't a server here  :)
     os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = "https://test-no-server-here.staging.ubuntu.com"
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("recommend-top", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_recommend_top()
     self.loop.run()
     self.assertTrue(self.error)
 def __init__(self, catview):
     FramedHeaderBox.__init__(self)
     # FIXME: we only need the catview for "add_titles_to_flowgrid"
     #        and "on_category_clicked" so we should be able to
     #        extract this to a "leaner" widget
     self.catview = catview
     self.catview.connect("application-activated",
                          self._on_application_activated)
     self.recommender_agent = RecommenderAgent()
Example #15
0
 def disabled_test_recagent_query_profile(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("profile", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_profile(pkgnames=["pitivi", "fretsonfire"])
     self.loop.run()
     self.assertFalse(self.error)
Example #16
0
 def test_recagent_query_server_status(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("server-status", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_server_status()
     self.loop.run()
     self.assertFalse(self.error)
 def test_mocked_recagent_post_submit_profile(self, mock_spawn_helper_run):
     def _patched_on_submit_profile_data(*args, **kwargs):
         piston_submit_profile = {}
         recommender_agent.emit("submit-profile-finished", 
                                piston_submit_profile)
     mock_spawn_helper_run.side_effect = _patched_on_submit_profile_data
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("submit-profile-finished", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent._calc_profile_id = lambda profile: "i-am-random"
     db = get_test_db()
     recommender_agent.post_submit_profile(db)
     self.assertFalse(self.error)
     args, kwargs =  mock_spawn_helper_run.call_args
     self.assertNotEqual(kwargs['data'][0]['package_list'], [])
Example #18
0
 def test_recagent_query_recommend_app(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("recommend-app", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_recommend_app("pitivi")
     self.loop.run()
     self.assertFalse(self.error)
Example #19
0
 def __init__(self):
     FramedHeaderBox.__init__(self)
     self.recommender_agent = RecommenderAgent()
     # keep track of applications that have been viewed via a
     # recommendation so that we can detect when a recommended app
     # has been installed
     self.recommended_apps_viewed = set()
     self.backend = get_install_backend()
     self.backend.connect("transaction-started", self._on_transaction_started)
     self.backend.connect("transaction-finished", self._on_transaction_finished)
Example #20
0
 def disabled_test_recagent_post_submit_profile(self):
     # NOTE: This requires a working recommender host that is reachable
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("submit-profile-finished", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     db = get_test_db()
     recommender_agent.post_submit_profile(db)
     self.loop.run()
     self.assertFalse(self.error)
Example #21
0
 def __init__(self, pkgname):
     super(AppRecommendationsCategory, self).__init__(
         u"People Also Installed", _("People Also Installed"), None, 
         xapian.Query(),flags=['available-only', 'not-installed-only'], 
         item_limit=4)
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect(
         "recommend-app", self._recommend_app_result)
     self.recommender_agent.connect(
         "error", self._recommender_agent_error)
     self.recommender_agent.query_recommend_app(pkgname)
Example #22
0
 def __init__(self):
     super(RecommendedForYouCategory, self).__init__(
         u"Recommended for You", _("Recommended for You"), None, 
         xapian.Query(),flags=['available-only', 'not-installed-only'], 
         item_limit=60)
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect(
         "recommend-me", self._recommend_me_result)
     self.recommender_agent.connect(
         "error", self._recommender_agent_error)
     self.recommender_agent.query_recommend_me()
Example #23
0
 def test_recagent_query_error(self):
     # NOTE: This tests the error condition itself! it simply forces an error
     #       'cuz there definitely isn't a server here  :)
     os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = "https://test-no-server-here.staging.ubuntu.com"
     recommender_agent = RecommenderAgent()
     recommender_agent.connect("recommend-top", self.on_query_done)
     recommender_agent.connect("error", self.on_query_error)
     recommender_agent.query_recommend_top()
     self.loop.run()
     self.assertTrue(self.error)
Example #24
0
 def setUp(self):
     self.loop = GLib.MainLoop(GLib.main_context_default())
     self.error = False
     if "SOFTWARE_CENTER_RECOMMENDER_HOST" in os.environ:
         orig_host = os.environ.get("SOFTWARE_CENTER_RECOMMENDER_HOST")
         self.addCleanup(os.environ.__setitem__,
             "SOFTWARE_CENTER_RECOMMENDER_HOST", orig_host)
     else:
         self.addCleanup(os.environ.pop, "SOFTWARE_CENTER_RECOMMENDER_HOST")
     server = "https://rec.staging.ubuntu.com"
     os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = server
     # most tests need it
     self.recommender_agent = RecommenderAgent()
     self.recommender_agent.connect("error", self.on_query_error)
Example #25
0
class RecommendedForYouCategory(Category):

    __gsignals__ = {
        "needs-refresh": (
            GObject.SIGNAL_RUN_LAST,
            GObject.TYPE_NONE,
            (),
        ),
        "recommender-agent-error": (
            GObject.SIGNAL_RUN_LAST,
            GObject.TYPE_NONE,
            (GObject.TYPE_STRING, ),
        ),
    }

    def __init__(self, db, subcategory=None):
        self.db = db
        self.subcategory = subcategory
        if subcategory:
            # this is the set of recommendations for a given subcategory
            cat_title = u"Recommended For You in %s" % (
                subcategory.untranslated_name)
            tr_title = utf8(_("Recommended For You in %s")) % utf8(
                subcategory.name)
        else:
            # this is the full set of recommendations for e.g. the lobby view
            cat_title = u"Recommended For You"
            tr_title = _("Recommended For You")
        super(RecommendedForYouCategory,
              self).__init__(cat_title,
                             tr_title,
                             None,
                             xapian.Query(),
                             flags=['available-only', 'not-installed-only'],
                             item_limit=60)
        self.recommender_agent = RecommenderAgent()
        self.recommender_agent.connect("recommend-me",
                                       self._recommend_me_result)
        self.recommender_agent.connect("error", self._recommender_agent_error)
        self.recommender_agent.query_recommend_me()

    def _recommend_me_result(self, recommender_agent, result_list):
        pkgs = []
        for item in result_list['data']:
            pkgs.append(item['package_name'])
        if self.subcategory:
            self.query = xapian.Query(xapian.Query.OP_AND,
                                      self.db.get_query_for_pkgnames(pkgs),
                                      self.subcategory.query)
        else:
            self.query = self.db.get_query_for_pkgnames(pkgs)
        self.emit("needs-refresh")

    def _recommender_agent_error(self, recommender_agent, msg):
        LOG.warn("Error while accessing the recommender service: %s" % msg)
        self.emit("recommender-agent-error", msg)
Example #26
0
class RecommendedForYouCategory(Category):

    __gsignals__ = {
        "needs-refresh": (GObject.SIGNAL_RUN_LAST,
                          GObject.TYPE_NONE,
                          (),
                          ),
        "recommender-agent-error": (GObject.SIGNAL_RUN_LAST,
                                    GObject.TYPE_NONE,
                                    (GObject.TYPE_STRING,),
                                    ),
    }

    def __init__(self, db, subcategory=None):
        self.db = db
        self.subcategory = subcategory
        if subcategory:
            # this is the set of recommendations for a given subcategory
            cat_title = u"Recommended For You in %s" % (
                subcategory.untranslated_name)
            tr_title = utf8(_("Recommended For You in %s")) % utf8(
                subcategory.name)
        else:
            # this is the full set of recommendations for e.g. the lobby view
            cat_title = u"Recommended For You"
            tr_title = _("Recommended For You")
        super(RecommendedForYouCategory, self).__init__(
                cat_title,
                tr_title,
                None,
                xapian.Query(),
                flags=['available-only', 'not-installed-only'],
                item_limit=60)
        self.recommender_agent = RecommenderAgent()
        self.recommender_agent.connect(
            "recommend-me", self._recommend_me_result)
        self.recommender_agent.connect(
            "error", self._recommender_agent_error)
        self.recommender_agent.query_recommend_me()

    def _recommend_me_result(self, recommender_agent, result_list):
        pkgs = []
        for item in result_list['data']:
            pkgs.append(item['package_name'])
        if self.subcategory:
            self.query = xapian.Query(xapian.Query.OP_AND,
                                  self.db.get_query_for_pkgnames(pkgs),
                                  self.subcategory.query)
        else:
            self.query = self.db.get_query_for_pkgnames(pkgs)
        self.emit("needs-refresh")

    def _recommender_agent_error(self, recommender_agent, msg):
        LOG.warn("Error while accessing the recommender service: %s"
                                                            % msg)
        self.emit("recommender-agent-error", msg)
Example #27
0
class AppRecommendationsCategory(Category):

    __gsignals__ = {
        "needs-refresh": (GObject.SIGNAL_RUN_LAST,
                          GObject.TYPE_NONE,
                          (),
                          ),
        "recommender-agent-error": (GObject.SIGNAL_RUN_LAST,
                                    GObject.TYPE_NONE,
                                    (GObject.TYPE_STRING,),
                                    ),
    }

    def __init__(self, db, pkgname):
        super(AppRecommendationsCategory, self).__init__(
            u"People Also Installed",
            _(u"People Also Installed"),
            None,
            xapian.Query(),
            flags=['available-only', 'not-installed-only'],
            item_limit=4)
        self.db = db
        self.recommender_agent = RecommenderAgent()
        self.recommender_agent.connect(
            "recommend-app", self._recommend_app_result)
        self.recommender_agent.connect(
            "error", self._recommender_agent_error)
        self.recommender_agent.query_recommend_app(pkgname)

    def _recommend_app_result(self, recommender_agent, result_list):
        pkgs = []
        for item in result_list['data']:
            pkgs.append(item['package_name'])
        self.query = self.db.get_query_for_pkgnames(pkgs)
        self.emit("needs-refresh")

    def _recommender_agent_error(self, recommender_agent, msg):
        LOG.warn("Error while accessing the recommender service: %s"
                                                            % msg)
        self.emit("recommender-agent-error", msg)
Example #28
0
class AppRecommendationsCategory(Category):

    __gsignals__ = {
        "needs-refresh": (
            GObject.SIGNAL_RUN_LAST,
            GObject.TYPE_NONE,
            (),
        ),
        "recommender-agent-error": (
            GObject.SIGNAL_RUN_LAST,
            GObject.TYPE_NONE,
            (GObject.TYPE_STRING, ),
        ),
    }

    def __init__(self, db, pkgname):
        super(AppRecommendationsCategory,
              self).__init__(u"People Also Installed",
                             _(u"People Also Installed"),
                             None,
                             xapian.Query(),
                             flags=['available-only', 'not-installed-only'],
                             item_limit=4)
        self.db = db
        self.recommender_agent = RecommenderAgent()
        self.recommender_agent.connect("recommend-app",
                                       self._recommend_app_result)
        self.recommender_agent.connect("error", self._recommender_agent_error)
        self.recommender_agent.query_recommend_app(pkgname)

    def _recommend_app_result(self, recommender_agent, result_list):
        pkgs = []
        for item in result_list['data']:
            pkgs.append(item['package_name'])
        self.query = self.db.get_query_for_pkgnames(pkgs)
        self.emit("needs-refresh")

    def _recommender_agent_error(self, recommender_agent, msg):
        LOG.warn("Error while accessing the recommender service: %s" % msg)
        self.emit("recommender-agent-error", msg)
Example #29
0
class RecommendationsPanel(FramedHeaderBox):
    """
    Base class for widgets that display recommendations
    """

    __gsignals__ = {
        "application-activated": (GObject.SIGNAL_RUN_LAST,
                                  GObject.TYPE_NONE,
                                  (GObject.TYPE_PYOBJECT,),
                                  ),
    }

    def __init__(self):
        FramedHeaderBox.__init__(self)
        self.recommender_agent = RecommenderAgent()
        # keep track of applications that have been viewed via a
        # recommendation so that we can detect when a recommended app
        # has been installed
        self.recommended_apps_viewed = set()
        self.backend = get_install_backend()
        self.backend.connect("transaction-started",
                             self._on_transaction_started)
        self.backend.connect("transaction-finished",
                             self._on_transaction_finished)

    def _on_application_activated(self, tile, app):
        self.emit("application-activated", app)
        # we only report on items if the user has opted-in to the
        # recommendations service
        if self.recommender_agent.is_opted_in():
            self.recommended_apps_viewed.add(app.pkgname)
            if network_state_is_connected():
                # let the recommendations service know that a
                # recommended item has been viewed (if it is
                # subsequently installed we will send an additional
                # signal to indicate that, in on_transaction_finished)
                # (there is no need to monitor for an error, etc., for this)
                self.recommender_agent.post_implicit_feedback(
                        app.pkgname,
                        RecommenderFeedbackActions.VIEWED)

    def _on_transaction_started(self, backend, pkgname, appname, trans_id,
                                trans_type):
        if (trans_type != TransactionTypes.INSTALL and
                pkgname in self.recommended_apps_viewed):
            # if the transaction is not an installation we don't want to
            # track it as a recommended item
            self.recommended_apps_viewed.remove(pkgname)

    def _on_transaction_finished(self, backend, result):
        if result.pkgname in self.recommended_apps_viewed:
            self.recommended_apps_viewed.remove(result.pkgname)
            if network_state_is_connected():
                # let the recommendations service know that a
                # recommended item has been successfully installed
                # (there is no need to monitor for an error, etc., for this)
                self.recommender_agent.post_implicit_feedback(
                        result.pkgname,
                        RecommenderFeedbackActions.INSTALLED)

    def _on_recommender_agent_error(self, agent, msg):
        LOG.warn("Error while accessing the recommender agent: %s" % msg)
        # this can happen if:
        #  - there is a real error from the agent
        #  - no cached data is available
        # hide the panel on an error
        self.hide()
Example #30
0
class RealTestRecommenderAgent(unittest.TestCase):
    """ tests the recommender agent """
    @unittest.skipIf(os.getuid() == 0, "this is not supported running as root")
    def setUp(self):
        self.loop = GLib.MainLoop(GLib.main_context_default())
        self.error = False
        if "SOFTWARE_CENTER_RECOMMENDER_HOST" in os.environ:
            orig_host = os.environ.get("SOFTWARE_CENTER_RECOMMENDER_HOST")
            self.addCleanup(os.environ.__setitem__,
                            "SOFTWARE_CENTER_RECOMMENDER_HOST", orig_host)
        else:
            self.addCleanup(os.environ.pop, "SOFTWARE_CENTER_RECOMMENDER_HOST")
        server = "https://rec.staging.ubuntu.com"
        os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = server
        # most tests need it
        self.recommender_agent = RecommenderAgent()
        self.recommender_agent.connect("error", self.on_query_error)

    def on_query_done(self, recagent, data):
        #print "query done, data: '%s'" % data
        self.loop.quit()
        self.error = False
        self.error_msg = ""

    def on_query_error(self, recagent, error):
        #print "query error received: ", error
        self.loop.quit()
        self.error = True
        self.error_msg = error

    def assertServerReturnsWithNoError(self):
        self.loop.run()
        self.assertFalse(self.error, "got error: '%s'" % self.error_msg)

    def test_recagent_query_server_status(self):
        self.recommender_agent.connect("server-status", self.on_query_done)
        self.recommender_agent.query_server_status()
        self.assertServerReturnsWithNoError()

    @unittest.skip("server returns 401")
    def test_recagent_post_submit_profile(self):
        # NOTE: This requires a working recommender host that is reachable
        db = get_test_db()
        self.recommender_agent.connect("submit-profile-finished",
                                       self.on_query_done)
        self.recommender_agent.post_submit_profile(db)
        self.assertServerReturnsWithNoError()
        #print mock_request._post

    @unittest.skip("server returns 401")
    def test_recagent_query_submit_anon_profile(self):
        self.recommender_agent.connect("submit-anon-profile-finished",
                                       self.on_query_done)
        self.recommender_agent.post_submit_anon_profile(
            uuid="xxxyyyzzz",
            installed_packages=["pitivi", "fretsonfire"],
            extra="")
        self.assertServerReturnsWithNoError()

    @unittest.skip("server returns 401")
    def test_recagent_query_profile(self):
        self.recommender_agent.connect("profile", self.on_query_done)
        self.recommender_agent.query_profile(
            pkgnames=["pitivi", "fretsonfire"])
        self.assertServerReturnsWithNoError()

    @unittest.skip("server returns 401")
    def test_recagent_query_recommend_me(self):
        self.recommender_agent.connect("recommend-me", self.on_query_done)
        self.recommender_agent.query_recommend_me()
        self.assertServerReturnsWithNoError()

    def test_recagent_query_recommend_app(self):
        self.recommender_agent.connect("recommend-app", self.on_query_done)
        self.recommender_agent.query_recommend_app("pitivi")
        self.assertServerReturnsWithNoError()

    def test_recagent_query_recommend_all_apps(self):
        self.recommender_agent.connect("recommend-all-apps",
                                       self.on_query_done)
        self.recommender_agent.query_recommend_all_apps()
        self.assertServerReturnsWithNoError()

    def test_recagent_query_recommend_top(self):
        self.recommender_agent.connect("recommend-top", self.on_query_done)
        self.recommender_agent.query_recommend_top()
        self.assertServerReturnsWithNoError()

    def test_recagent_query_error(self):
        # NOTE: This tests the error condition itself! it simply forces an error
        #       'cuz there definitely isn't a server here  :)
        fake_server = "https://test-no-server-here.staging.ubuntu.com"
        os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = fake_server
        recommender_agent = RecommenderAgent()
        recommender_agent.connect("recommend-top", self.on_query_done)
        recommender_agent.connect("error", self.on_query_error)
        recommender_agent.query_recommend_top()
        self.loop.run()
        self.assertTrue(self.error)

    @unittest.skip("server returns 401")
    def test_recagent_post_implicit_feedback(self):
        self.recommender_agent.connect("submit-implicit-feedback-finished",
                                       self.on_query_done)
        from softwarecenter.enums import RecommenderFeedbackActions
        self.recommender_agent.post_implicit_feedback(
            "bluefish", RecommenderFeedbackActions.INSTALLED)
        self.assertServerReturnsWithNoError()
Example #31
0
class RealTestRecommenderAgent(unittest.TestCase):
    """ tests the recommender agent """
    
    @unittest.skipIf(os.getuid() == 0, 
                     "this is not supported running as root")
    def setUp(self):
        self.loop = GLib.MainLoop(GLib.main_context_default())
        self.error = False
        if "SOFTWARE_CENTER_RECOMMENDER_HOST" in os.environ:
            orig_host = os.environ.get("SOFTWARE_CENTER_RECOMMENDER_HOST")
            self.addCleanup(os.environ.__setitem__,
                "SOFTWARE_CENTER_RECOMMENDER_HOST", orig_host)
        else:
            self.addCleanup(os.environ.pop, "SOFTWARE_CENTER_RECOMMENDER_HOST")
        server = "https://rec.staging.ubuntu.com"
        os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = server
        # most tests need it
        self.recommender_agent = RecommenderAgent()
        self.recommender_agent.connect("error", self.on_query_error)

    def on_query_done(self, recagent, data):
        #print "query done, data: '%s'" % data
        self.loop.quit()
        self.error = False
        self.error_msg = ""

    def on_query_error(self, recagent, error):
        #print "query error received: ", error
        self.loop.quit()
        self.error = True
        self.error_msg = error

    def assertServerReturnsWithNoError(self):
        self.loop.run()
        self.assertFalse(self.error, "got error: '%s'" % self.error_msg)

    def test_recagent_query_server_status(self):
        self.recommender_agent.connect("server-status", self.on_query_done)
        self.recommender_agent.query_server_status()
        self.assertServerReturnsWithNoError()

    @unittest.skip("server returns 401")
    def test_recagent_post_submit_profile(self):
        # NOTE: This requires a working recommender host that is reachable
        db = get_test_db()
        self.recommender_agent.connect(
            "submit-profile-finished", self.on_query_done)
        self.recommender_agent.post_submit_profile(db)
        self.assertServerReturnsWithNoError()
        #print mock_request._post
        
    @unittest.skip("server returns 401")
    def test_recagent_query_submit_anon_profile(self):
        self.recommender_agent.connect(
            "submit-anon-profile-finished", self.on_query_done)
        self.recommender_agent.post_submit_anon_profile(
                uuid="xxxyyyzzz",
                installed_packages=["pitivi", "fretsonfire"],
                extra="")
        self.assertServerReturnsWithNoError()

    @unittest.skip("server returns 401")
    def test_recagent_query_profile(self):
        self.recommender_agent.connect("profile", self.on_query_done)
        self.recommender_agent.query_profile(pkgnames=["pitivi", "fretsonfire"])
        self.assertServerReturnsWithNoError()

    @unittest.skip("server returns 401")
    def test_recagent_query_recommend_me(self):
        self.recommender_agent.connect("recommend-me", self.on_query_done)
        self.recommender_agent.query_recommend_me()
        self.assertServerReturnsWithNoError()

    def test_recagent_query_recommend_app(self):
        self.recommender_agent.connect("recommend-app", self.on_query_done)
        self.recommender_agent.query_recommend_app("pitivi")
        self.assertServerReturnsWithNoError()

    def test_recagent_query_recommend_all_apps(self):
        self.recommender_agent.connect("recommend-all-apps", self.on_query_done)
        self.recommender_agent.query_recommend_all_apps()
        self.assertServerReturnsWithNoError()

    def test_recagent_query_recommend_top(self):
        self.recommender_agent.connect("recommend-top", self.on_query_done)
        self.recommender_agent.query_recommend_top()
        self.assertServerReturnsWithNoError()

    def test_recagent_query_error(self):
        # NOTE: This tests the error condition itself! it simply forces an error
        #       'cuz there definitely isn't a server here  :)
        fake_server = "https://test-no-server-here.staging.ubuntu.com"
        os.environ["SOFTWARE_CENTER_RECOMMENDER_HOST"] = fake_server
        recommender_agent = RecommenderAgent()
        recommender_agent.connect("recommend-top", self.on_query_done)
        recommender_agent.connect("error", self.on_query_error)
        recommender_agent.query_recommend_top()
        self.loop.run()
        self.assertTrue(self.error)

    @unittest.skip("server returns 401")
    def test_recagent_post_implicit_feedback(self):
        self.recommender_agent.connect("submit-implicit-feedback-finished",
                                  self.on_query_done)
        from softwarecenter.enums import RecommenderFeedbackActions
        self.recommender_agent.post_implicit_feedback(
                "bluefish",
                RecommenderFeedbackActions.INSTALLED)
        self.assertServerReturnsWithNoError()