def test_02_supports_fail(self):
     p = MyPlugin()
     for url in UNSUPPORTED_URLS:
         assert not p.supports({"url" : [url]}), "Failed with URL " + url
    def __run_resource_and_result_test(self, test_items):
        global CURRENT_REQUEST
        
        # go through each file and result object
        for path, comparison in test_items.iteritems():

            detect_should_fail = False
            if comparison.get('type') == 'failed-to-obtain-license':
                detect_should_fail = True

            # construct a request object, using the provenance/source url as the provider url
            record = {}
            record['bibjson'] = {}
            record['provider'] = {}
            record['provider']['url'] = [comparison['provenance']['source']]
            record = models.MessageObject(record=record)
            
            # set the current request so that the monkey patch knows how to respond
            CURRENT_REQUEST = comparison['provenance']['source']
            
            # run the plugin
            p = MyPlugin()
            p.license_detect(record)

            record = record.record
            
            if not detect_should_fail:
                assert "license" in record['bibjson'], 'While testing with ' + path
                assert record['bibjson']['license'] is not None
                assert len(record['bibjson']['license']) == 1  # only 1 license was detected
            else:
                assert not "license" in record['bibjson'], 'While testing with ' + path
                continue
            
            # The rules for the comparison licence object are:
            # - if a key has a value, there resulting object's value must match exactly
            # - if a key has been omitted, it will not be tested
            # - if a key's value is the empty string, the resulting object's key's value must be the empty string
            # - if a key's value is None, the resulting object MUST NOT have the key or MUST be the empty string
            # - if a key's value is -1, the resulting object MUST have the key
            licence = record['bibjson']['license'][0]
            for key, value in comparison.iteritems():
                if key == "provenance":
                    # for better layout of code, let's do provenance separately
                    continue
                if value is None:
                    # the resulting object MUST NOT have the key or MUST be the empty string
                    assert key not in licence or licence.get(key) == "", ((key, value), licence.get(key))
                elif value == -1:
                    # the resulting object MUST have the key
                    assert key in licence, ((key, value), licence.get(key))
                else:
                    # the resulting object must match the comparison object
                    assert value == licence.get(key), ((key, value), licence.get(key))
            
            prov = licence.get("provenance", {})
            for key, value in comparison.get("provenance", {}).iteritems():
                if value is None:
                    # the resulting object MUST NOT have the key
                    assert key not in prov or prov.get(key) == "", ((key, value), prov.get(key))
                elif value == -1:
                    # the resulting object MUST have the key
                    assert key in prov, ((key, value), prov.get(key))
                else:
                    # the resulting object must match the comparison object
                    assert value == prov.get(key), ((key, value), prov.get(key))
 def test_01_supports_success(self):
     p = MyPlugin()
     for url in SUPPORTED_URLS:
         assert p.supports({"url" : [url]})