|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class LibkeyMockResponse |
| 4 | + attr_reader :status |
| 5 | + |
| 6 | + def initialize(status, body) |
| 7 | + @status = status |
| 8 | + @body = body |
| 9 | + end |
| 10 | + |
| 11 | + def to_s |
| 12 | + @body |
| 13 | + end |
| 14 | +end |
| 15 | + |
| 16 | +class LibkeyConnectionError |
| 17 | + def timeout(_) |
| 18 | + self |
| 19 | + end |
| 20 | + |
| 21 | + def get(_url) |
| 22 | + raise HTTP::ConnectionError, 'forced connection failure' |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +class LibkeyParsingError |
| 27 | + def timeout(_) |
| 28 | + self |
| 29 | + end |
| 30 | + |
| 31 | + def get(_url) |
| 32 | + LibkeyMockResponse.new(200, 'This is not valid json') |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +class LibkeyTest < ActiveSupport::TestCase |
| 37 | + test 'enabled? method returns true if both env are set' do |
| 38 | + ClimateControl.modify(LIBKEY_ID: 'foo', LIBKEY_KEY: 'bar') do |
| 39 | + assert Libkey.enabled? |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + test 'enabled? method returns false if either env not set' do |
| 44 | + ClimateControl.modify(LIBKEY_ID: nil) do |
| 45 | + refute Libkey.enabled? |
| 46 | + end |
| 47 | + |
| 48 | + ClimateControl.modify(LIBKEY_KEY: nil) do |
| 49 | + refute Libkey.enabled? |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + test 'lookup does nothing with a type other than "doi" or "pmid"' do |
| 54 | + refute Libkey.lookup(type: 'foo', identifier: 'foobar') |
| 55 | + end |
| 56 | + |
| 57 | + test 'lookup does work with any identifier value' do |
| 58 | + VCR.use_cassette('libkey nonexistent') do |
| 59 | + result = Libkey.lookup(type: 'doi', identifier: 'foobar') |
| 60 | + |
| 61 | + refute result |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + test 'lookup gets a valid response for DOIs' do |
| 66 | + VCR.use_cassette('libkey doi') do |
| 67 | + result = Libkey.lookup(type: 'doi', identifier: '10.1038/s41567-023-02305-y') |
| 68 | + |
| 69 | + assert_instance_of Hash, result |
| 70 | + assert_equal result.keys, %i[link text type] |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + test 'lookup gets a valid response for PMIDs' do |
| 75 | + VCR.use_cassette('libkey pmid') do |
| 76 | + result = Libkey.lookup(type: 'pmid', identifier: '22110403') |
| 77 | + |
| 78 | + assert_instance_of Hash, result |
| 79 | + assert_equal result.keys, %i[link text type] |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + test 'libkey model catches connection errors' do |
| 84 | + libkey_client = LibkeyConnectionError.new |
| 85 | + |
| 86 | + result = Libkey.lookup(type: 'doi', identifier: '10.1038/s41567-023-02305-y', libkey_client:) |
| 87 | + |
| 88 | + assert_instance_of Hash, result |
| 89 | + assert_equal 'A connection error has occurred', result['error'] |
| 90 | + end |
| 91 | + |
| 92 | + test 'libkey model catches parsing errors' do |
| 93 | + libkey_client = LibkeyParsingError.new |
| 94 | + |
| 95 | + result = Libkey.lookup(type: 'doi', identifier: '10.1038/s41567-023-02305-y', libkey_client:) |
| 96 | + |
| 97 | + assert_instance_of Hash, result |
| 98 | + assert_equal 'A parsing error has occurred', result['error'] |
| 99 | + end |
| 100 | +end |
0 commit comments