Пример #1
0
    def test_pending_result_does_not_change_num_fails(self):
        """Inserting a new pending result should not change num_fails.

        If we already have a results row for a resource, then we change that row
        to make a pending result, this should not change num_fails or other
        fields.

        """
        # Make a resource with 1 success then 3 consecutive fails.
        results.upsert("test_resource_id", True)
        last_successful = results.get("test_resource_id")["last_successful"]
        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        num_fails = results.get("test_resource_id")["num_fails"]
        last_checked = results.get("test_resource_id")["last_checked"]

        before = datetime.datetime.utcnow()
        results._make_pending(["test_resource_id"])
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")
        assert result["num_fails"] == num_fails
        assert result["last_successful"] == last_successful
        assert result["last_checked"] == last_checked
        assert result["alive"] is False
        assert result["pending"] is True
        assert strptime(result["pending_since"]) > before
        assert strptime(result["pending_since"]) < after
    def test_make_pending_does_not_change_status_or_reason(self):
        """Marking a result as pending should not change status or reason.

        Marking a result as pending just says "we are expecting a new result
        for this resource soon", it should not change the existing results.

        """
        results.upsert("test_resource_id", True, status=200, reason="OK")
        last_successful = results.get("test_resource_id")["last_successful"]
        results.upsert("test_resource_id",
                       False,
                       status=401,
                       reason="Unauthorized")
        last_checked = results.get("test_resource_id")["last_checked"]

        results._make_pending(["test_resource_id"])

        result = results.get("test_resource_id")
        assert result["num_fails"] == 1
        assert result["last_successful"] == last_successful
        assert result["last_checked"] == last_checked
        assert result["alive"] is False
        assert result["pending"] is True
        assert result["status"] == 401
        assert result["reason"] == "Unauthorized"
    def test_pending_result_does_not_change_num_fails(self):
        """Inserting a new pending result should not change num_fails.

        If we already have a results row for a resource, then we change that row
        to make a pending result, this should not change num_fails or other
        fields.

        """
        # Make a resource with 1 success then 3 consecutive fails.
        results.upsert("test_resource_id", True)
        last_successful = results.get("test_resource_id")["last_successful"]
        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        num_fails = results.get("test_resource_id")["num_fails"]
        last_checked = results.get("test_resource_id")["last_checked"]

        before = datetime.datetime.utcnow()
        results._make_pending(["test_resource_id"])
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")
        assert result["num_fails"] == num_fails
        assert result["last_successful"] == last_successful
        assert result["last_checked"] == last_checked
        assert result["alive"] is False
        assert result["pending"] is True
        assert strptime(result["pending_since"]) > before
        assert strptime(result["pending_since"]) < after
def get(context, data_dict):
    """Get the latest link check result data for a resource.

    :param resource_id: the resource to return the result data for
    :type resource_id: string

    :returns: the latest link check data for the resource, or None if there are
      no results for this resource
    :rtype: dict or None

    """
    toolkit.check_access("ckanext_deadoralive_get", context, data_dict)

    # TODO: Validation.

    resource_id = data_dict["resource_id"]

    try:
        result = results.get(resource_id)
    except results.NoResultForResourceError:
        return None

    result["broken"] = _is_broken(result)

    return result
    def test_update_with_unicode(self):
        results.upsert("test_resource_id", True, status=200, reason="OK")

        results.upsert("test_resource_id", False, status=404, reason=u"Föoßär")

        result = results.get("test_resource_id")
        assert result["reason"] == u"Föoßär"
Пример #6
0
def get(context, data_dict):
    """Get the latest link check result data for a resource.

    :param resource_id: the resource to return the result data for
    :type resource_id: string

    :returns: the latest link check data for the resource, or None if there are
      no results for this resource
    :rtype: dict or None

    """
    toolkit.check_access("ckanext_deadoralive_get", context, data_dict)

    # TODO: Validation.

    resource_id = data_dict["resource_id"]

    try:
        result = results.get(resource_id)
    except results.NoResultForResourceError:
        return None

    result["broken"] = _is_broken(result)

    return result
Пример #7
0
    def test_update_with_unicode(self):
        results.upsert("test_resource_id", True, status=200, reason="OK")

        results.upsert("test_resource_id", False, status=404,
                       reason=u"Föoßär")

        result = results.get("test_resource_id")
        assert result["reason"] == u"Föoßär"
    def test_update_with_no_status_or_reason_clears(self):
        """Passing no status or reason to upsert() should clear existing."""
        results.upsert("test_resource_id", True, status=200, reason="OK")

        results.upsert("test_resource_id", False)

        result = results.get("test_resource_id")
        assert result["status"] is None
        assert result["reason"] is None
Пример #9
0
    def test_update_with_no_status_or_reason_clears(self):
        """Passing no status or reason to upsert() should clear existing."""
        results.upsert("test_resource_id", True, status=200, reason="OK")

        results.upsert("test_resource_id", False)

        result = results.get("test_resource_id")
        assert result["status"] is None
        assert result["reason"] is None
Пример #10
0
    def test_update_replacing_status_and_reason(self):
        """Passing status and reason params to upsert() should overwrite."""
        results.upsert("test_resource_id", True, status=200, reason="OK")

        results.upsert("test_resource_id", False, status=404,
                       reason="Not Found")

        result = results.get("test_resource_id")
        assert result["status"] == 404
        assert result["reason"] == "Not Found"
Пример #11
0
    def test_update_replacing_status_and_reason(self):
        """Passing status and reason params to upsert() should overwrite."""
        results.upsert("test_resource_id", True, status=200, reason="OK")

        results.upsert("test_resource_id",
                       False,
                       status=404,
                       reason="Not Found")

        result = results.get("test_resource_id")
        assert result["status"] == 404
        assert result["reason"] == "Not Found"
Пример #12
0
    def test_incrementing_num_fails(self):
        """Test that repeated bad results increment num_fails."""

        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", False)
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")

        assert result["num_fails"] == 3
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
Пример #13
0
    def test_make_pending_does_not_change_status_or_reason(self):
        """Marking a result as pending should not change status or reason.

        Marking a result as pending just says "we are expecting a new result
        for this resource soon", it should not change the existing results.

        """
        results.upsert("test_resource_id", True, status=200, reason="OK")
        last_successful = results.get("test_resource_id")["last_successful"]
        results.upsert("test_resource_id", False, status=401,
                       reason="Unauthorized")
        last_checked = results.get("test_resource_id")["last_checked"]

        results._make_pending(["test_resource_id"])

        result = results.get("test_resource_id")
        assert result["num_fails"] == 1
        assert result["last_successful"] == last_successful
        assert result["last_checked"] == last_checked
        assert result["alive"] is False
        assert result["pending"] is True
        assert result["status"] == 401
        assert result["reason"] == "Unauthorized"
Пример #14
0
    def test_incrementing_num_fails(self):
        """Test that repeated bad results increment num_fails."""

        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", False)
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")

        assert result["num_fails"] == 3
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
Пример #15
0
    def test_initial_pending_result(self):
        """Test creating a pending result for a resource that has no results.

        """
        before = datetime.datetime.utcnow()
        results._make_pending(["test_resource_id"])
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")
        assert result["num_fails"] == 0
        assert result["last_successful"] is None
        assert result["last_checked"] is None
        assert result["alive"] is None
        assert result["pending"] is True
        assert strptime(result["pending_since"]) > before
        assert strptime(result["pending_since"]) < after
Пример #16
0
    def test_reset_num_fails(self):
        """Test that a successful result resets num_fails to 0."""

        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", True)
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")

        assert result["num_fails"] == 0
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
        assert strptime(result["last_successful"]) > before
        assert strptime(result["last_successful"]) < after
Пример #17
0
    def test_initial_pending_result(self):
        """Test creating a pending result for a resource that has no results.

        """
        before = datetime.datetime.utcnow()
        results._make_pending(["test_resource_id"])
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")
        assert result["num_fails"] == 0
        assert result["last_successful"] is None
        assert result["last_checked"] is None
        assert result["alive"] is None
        assert result["pending"] is True
        assert strptime(result["pending_since"]) > before
        assert strptime(result["pending_since"]) < after
Пример #18
0
    def test_reset_num_fails(self):
        """Test that a successful result resets num_fails to 0."""

        results.upsert("test_resource_id", False)
        results.upsert("test_resource_id", False)
        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", True)
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")

        assert result["num_fails"] == 0
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
        assert strptime(result["last_successful"]) > before
        assert strptime(result["last_successful"]) < after
Пример #19
0
 def test_insert_result_with_status_and_reason(self):
     """Test cresting a new result row with a status and reason."""
     before = datetime.datetime.utcnow()
     results.upsert("test_resource_id", False, status=500,
                    reason="Internal Server Error")
     after = datetime.datetime.utcnow()
     result = results.get("test_resource_id")
     assert result["resource_id"] == "test_resource_id"
     assert result["alive"] is False
     assert strptime(result["last_checked"]) > before
     assert strptime(result["last_checked"]) < after
     assert result["last_successful"] is None
     assert result["num_fails"] == 1
     assert result["pending"] is False
     assert result["pending_since"] is None
     assert result["status"] == 500
     assert result["reason"] == "Internal Server Error"
Пример #20
0
 def test_insert_result_with_status_and_reason(self):
     """Test cresting a new result row with a status and reason."""
     before = datetime.datetime.utcnow()
     results.upsert("test_resource_id",
                    False,
                    status=500,
                    reason="Internal Server Error")
     after = datetime.datetime.utcnow()
     result = results.get("test_resource_id")
     assert result["resource_id"] == "test_resource_id"
     assert result["alive"] is False
     assert strptime(result["last_checked"]) > before
     assert strptime(result["last_checked"]) < after
     assert result["last_successful"] is None
     assert result["num_fails"] == 1
     assert result["pending"] is False
     assert result["pending_since"] is None
     assert result["status"] == 500
     assert result["reason"] == "Internal Server Error"
Пример #21
0
    def test_reset_pending_status(self):
        """Test that either a successful or failed result resets pending and
        pending_since.

        """
        import ckan.model

        result = results._LinkCheckerResult(
            "test_resource_id", None, pending=True)
        result.pending = True
        result.pending_since
        ckan.model.Session.add(result)
        ckan.model.Session.commit()

        results.upsert("test_resource_id", True)

        result = results.get("test_resource_id")

        assert result["pending"] is False
        assert result["pending_since"] is None
Пример #22
0
    def test_reset_pending_status(self):
        """Test that either a successful or failed result resets pending and
        pending_since.

        """
        import ckan.model

        result = results._LinkCheckerResult("test_resource_id",
                                            None,
                                            pending=True)
        result.pending = True
        result.pending_since
        ckan.model.Session.add(result)
        ckan.model.Session.commit()

        results.upsert("test_resource_id", True)

        result = results.get("test_resource_id")

        assert result["pending"] is False
        assert result["pending_since"] is None
Пример #23
0
    def test_insert_failed_result(self):
        """Test checking a resource for the first time when the link is broken.

        """
        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", False)
        after = datetime.datetime.utcnow()
        result = results.get("test_resource_id")
        assert result["resource_id"] == "test_resource_id"
        assert result["alive"] is False
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
        assert result["last_successful"] is None
        assert result["num_fails"] == 1
        assert result["pending"] is False
        assert result["pending_since"] is None

        # status and reason should be None, since we didn't pass either to
        # upsert().
        assert result["status"] is None
        assert result["reason"] is None
Пример #24
0
    def test_insert_failed_result(self):
        """Test checking a resource for the first time when the link is broken.

        """
        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", False)
        after = datetime.datetime.utcnow()
        result = results.get("test_resource_id")
        assert result["resource_id"] == "test_resource_id"
        assert result["alive"] is False
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
        assert result["last_successful"] is None
        assert result["num_fails"] == 1
        assert result["pending"] is False
        assert result["pending_since"] is None

        # status and reason should be None, since we didn't pass either to
        # upsert().
        assert result["status"] is None
        assert result["reason"] is None
Пример #25
0
    def test_update_with_failed_result(self):
        """Test updating a resource's row with a new failed result."""
        results.upsert("test_resource_id", True)

        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", False)
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")
        assert result["resource_id"] == "test_resource_id"
        assert result["alive"] is False
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
        assert strptime(result["last_successful"]) < before
        assert result["num_fails"] == 1
        assert result["pending"] is False
        assert result["pending_since"] is None

        # status and reason should be None, since we didn't pass either to
        # upsert().
        assert result["status"] is None
        assert result["reason"] is None
Пример #26
0
    def test_update_with_failed_result(self):
        """Test updating a resource's row with a new failed result."""
        results.upsert("test_resource_id", True)

        before = datetime.datetime.utcnow()
        results.upsert("test_resource_id", False)
        after = datetime.datetime.utcnow()

        result = results.get("test_resource_id")
        assert result["resource_id"] == "test_resource_id"
        assert result["alive"] is False
        assert strptime(result["last_checked"]) > before
        assert strptime(result["last_checked"]) < after
        assert strptime(result["last_successful"]) < before
        assert result["num_fails"] == 1
        assert result["pending"] is False
        assert result["pending_since"] is None

        # status and reason should be None, since we didn't pass either to
        # upsert().
        assert result["status"] is None
        assert result["reason"] is None
Пример #27
0
    def test_that_it_creates_pending_checks(self):
        """get_resources_to_check() should create pending link checker results
        for all the resources it returns."""

        # A resource that has never been checked.
        resource_1 = factories.Resource()['id']

        # A resource that was checked > 24 hours ago.
        resource_2 = factories.Resource()['id']
        thirty_hours_ago = datetime.datetime.utcnow() - datetime.timedelta(
            hours=30)
        results.upsert(resource_2, True, last_checked=thirty_hours_ago)

        # A resource with a pending check from > 2 hours ago.
        resource_3 = factories.Resource()['id']
        three_hours_ago = datetime.datetime.utcnow() - datetime.timedelta(
            hours=3)
        results._make_pending([resource_3], three_hours_ago)

        results.get_resources_to_check(10)

        for resource in (resource_1, resource_2, resource_3):
            result = results.get(resource)
            assert result["pending"] is True
Пример #28
0
    def test_that_it_creates_pending_checks(self):
        """get_resources_to_check() should create pending link checker results
        for all the resources it returns."""

        # A resource that has never been checked.
        resource_1 = factories.Resource()['id']

        # A resource that was checked > 24 hours ago.
        resource_2 = factories.Resource()['id']
        thirty_hours_ago = datetime.datetime.utcnow() - datetime.timedelta(
            hours=30)
        results.upsert(resource_2, True, last_checked=thirty_hours_ago)

        # A resource with a pending check from > 2 hours ago.
        resource_3 = factories.Resource()['id']
        three_hours_ago = datetime.datetime.utcnow() - datetime.timedelta(
            hours=3)
        results._make_pending([resource_3], three_hours_ago)

        results.get_resources_to_check(10)

        for resource in (resource_1, resource_2, resource_3):
            result = results.get(resource)
            assert result["pending"] is True
Пример #29
0
 def test_insert_result_with_unicode(self):
     """Test upsert() and get() with non-ASCII chars in the reason string."""
     results.upsert("test_resource_id", False, status=500, reason=u"Föobäß")
     result = results.get("test_resource_id")
     assert result["reason"] == u"Föobäß"
Пример #30
0
 def test_insert_result_with_unicode(self):
     """Test upsert() and get() with non-ASCII chars in the reason string."""
     results.upsert("test_resource_id", False, status=500,
                    reason=u"Föobäß")
     result = results.get("test_resource_id")
     assert result["reason"] == u"Föobäß"