Exemplo n.º 1
0
    def test_handle_event(self):
        api = MockBibliothecaAPI(self._db, self.collection)
        api.queue_response(
            200, content=self.sample_data("item_metadata_single.xml"))
        analytics = MockAnalyticsProvider()
        monitor = BibliothecaEventMonitor(self._db,
                                          self.collection,
                                          api_class=api,
                                          analytics=analytics)

        now = datetime.datetime.utcnow()
        monitor.handle_event("ddf4gr9", "9781250015280", None, now, None,
                             CirculationEvent.DISTRIBUTOR_LICENSE_ADD)

        # The collection now has a LicensePool corresponding to the book
        # we just loaded.
        [pool] = self.collection.licensepools
        eq_("ddf4gr9", pool.identifier.identifier)

        # The book has a presentation-ready work and we know its
        # bibliographic metadata.
        eq_(True, pool.work.presentation_ready)
        eq_("The Incense Game", pool.work.title)

        # The LicensePool's circulation information has been changed
        # to reflect what we know about the book -- that we have one
        # license which (as of the instant the event happened) is
        # available.
        eq_(1, pool.licenses_owned)
        eq_(1, pool.licenses_available)

        # Three analytics events were collected: one for the license add
        # event itself, one for the 'checkin' that made the new
        # license available, and one for the first appearance of a new
        # LicensePool.
        eq_(3, analytics.count)
 def setup(self):
     super(TestCirculationAPI, self).setup()
     self.collection = MockBibliothecaAPI.mock_collection(self._db)
     edition, self.pool = self._edition(
         data_source_name=DataSource.BIBLIOTHECA,
         identifier_type=Identifier.BIBLIOTHECA_ID,
         with_license_pool=True, collection=self.collection
     )
     self.pool.open_access = False
     self.identifier = self.pool.identifier
     [self.delivery_mechanism] = self.pool.delivery_mechanisms
     self.patron = self._patron()
     self.analytics = MockAnalyticsProvider()
     self.circulation = MockCirculationAPI(
         self._db, self._default_library, analytics=self.analytics, api_map = {
             ExternalIntegration.BIBLIOTHECA : MockBibliothecaAPI
         }
     )
     self.remote = self.circulation.api_for_license_pool(self.pool)
 def setup(self):
     super(TestCirculationAPI, self).setup()
     self.collection = MockBibliothecaAPI.mock_collection(self._db)
     edition, self.pool = self._edition(
         data_source_name=DataSource.BIBLIOTHECA,
         identifier_type=Identifier.BIBLIOTHECA_ID,
         with_license_pool=True,
         collection=self.collection)
     self.pool.open_access = False
     self.identifier = self.pool.identifier
     [self.delivery_mechanism] = self.pool.delivery_mechanisms
     self.patron = self._patron()
     self.analytics = MockAnalyticsProvider()
     self.circulation = MockCirculationAPI(
         self._db,
         self._default_library,
         analytics=self.analytics,
         api_map={ExternalIntegration.BIBLIOTHECA: MockBibliothecaAPI})
     self.remote = self.circulation.api_for_license_pool(self.pool)
Exemplo n.º 4
0
    def test_run_once(self):
        api = MockBibliothecaAPI(self._db, self.collection)
        api.queue_response(
            200, content=self.sample_data("empty_end_date_event.xml"))
        api.queue_response(
            200, content=self.sample_data("item_metadata_single.xml"))
        monitor = BibliothecaEventMonitor(self._db,
                                          self.collection,
                                          api_class=api)
        now = datetime.datetime.utcnow()
        yesterday = now - datetime.timedelta(days=1)

        new_timestamp = monitor.run_once(yesterday, now)

        # Two requests were made to the API -- one to find events
        # and one to look up detailed information about the book
        # whose event we learned of.
        eq_(2, len(api.requests))

        # The result, which will be used as the new timestamp, is very
        # close to the time we called run_once(). It represents the
        # point at which we should expect new events to start showing
        # up.
        assert (new_timestamp - now).seconds < 2

        # A LicensePool was created for the identifier referred to
        # in empty_end_date_event.xml.
        [pool] = self.collection.licensepools
        eq_("d5rf89", pool.identifier.identifier)

        # But since the metadata retrieved in the follow-up request
        # was for a different book, no Work and no Edition have been
        # created. (See test_handle_event for what happens when the
        # API cooperates.)
        eq_(None, pool.work)
        eq_(None, pool.presentation_edition)

        # If we tell run_once() to work through a zero amount of time,
        # it does nothing.
        new_timestamp = monitor.run_once(yesterday, yesterday)
        eq_(new_timestamp, yesterday)
Exemplo n.º 5
0
 def setup(self):
     super(BibliothecaAPITest, self).setup()
     self.collection = MockBibliothecaAPI.mock_collection(self._db)
     self.api = MockBibliothecaAPI(self._db, self.collection)