Project

General

Profile

metacat / src / ruby / test / metacat_test.rb @ 8561

1
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
require "metacat.rb"
3
require "test/unit"
4

    
5
class MetacatTest < Test::Unit::TestCase
6

    
7
  def setup
8
    # This test case is setup for use against the ucsb/msi metacat server
9
    # You need a valid login to run the test case as well as an squery, eml-docid,
10
    # and datatable docid that will all return documents
11
    @username = 'uid=cburt,o=PISCO,dc=ecoinformatics,dc=org'
12
    @password = '7lobster'
13
    @metacat = Metacat.new("http://data.piscoweb.org/catalog/metacat")
14
    #must return at least one eml document
15
    @squery = '<?xml version="1.0"?>
16
                 <pathquery version="1.2">
17
                   <returndoctype>eml://ecoinformatics.org/eml-2.0.1</returndoctype>
18
                   <returndoctype>eml://ecoinformatics.org/eml-2.0.0</returndoctype>
19
                   <returnfield>dataset/title</returnfield>
20
                   <returnfield>dataTable/entityName</returnfield>
21
                   <returnfield>creator/individualName/surName</returnfield>
22
                   <returnfield>creator/organizationName</returnfield>
23
                   <returnfield>dataTable/physical/distribution/online/url</returnfield>
24
                   <querygroup operator="INTERSECT">
25
                     <queryterm casesensitive="false" searchmode="starts-with">
26
                       <value>PISCO:</value>
27
                       <pathexpr>title</pathexpr>
28
                     </queryterm>
29
                     <querygroup operator="INTERSECT">
30
                       <queryterm casesensitive="true" searchmode="equals">
31
                         <value>Subtidal Community Survey Data</value>
32
                         <pathexpr>keywordSet/keyword</pathexpr>
33
                       </queryterm>
34
                       <queryterm casesensitive="true" searchmode="equals">
35
                         <value>PISCO Categories</value>
36
                         <pathexpr>keywordSet/keywordThesaurus</pathexpr>
37
                       </queryterm>
38
                     </querygroup>
39
                   </querygroup>
40
                 </pathquery>'
41
    @data_table_docid = 'HMS001_020ADCP019R00_20060612.40.1'
42
    @eml_docid = 'HMS001_020ADCP019R00_20060612.50.1'
43
    # Not accessable to user Public
44
    @locked_docid = 'chad.1.1'
45
  end
46
  
47
  def teardown
48
  end
49
  
50
  # Metacat.new
51
  def test_new
52
    assert_kind_of Metacat, Metacat.new("http://data.piscoweb.org/catalog/metacat")
53
  end
54
  
55
  def test_initial_login
56
    metacat = Metacat.new("http://data.piscoweb.org/catalog/metacat", 
57
    'username' => @username, 'password' => @password)
58
    assert metacat
59
    assert metacat.logged_in?
60
  end
61
  
62
  def test_login_and_yield
63
    Metacat.new("http://data.piscoweb.org/catalog/metacat", 
64
    'username' => @username, 'password' => @password) do |metacat|
65
      assert metacat.logged_in?
66
    end
67
  end
68
  
69
  # Metacat.find()
70
  def test_error_if_docid_and_squery_set?
71
    assert_raises ArgumentError do
72
      @metacat.find(
73
      :docid => @eml_docid, 
74
      :squery => 'bs'
75
      )
76
    end
77
  end
78
    
79
  def test_nil_if_document_does_not_exist?
80
    assert_nil @metacat.find(:docid => 'bs_docid.80.9')
81
  end
82
  
83
  def test_permission_denied
84
    assert_raise(MetacatPermissionDenied) { @metacat.find(:docid  =>  @locked_docid) }
85
  end
86
  
87
  def test_returns_eml?
88
    assert_kind_of Eml, 
89
    @metacat.find(:docid => @eml_docid)  
90
  end
91
  
92
  def test_will_not_return_data_table
93
    assert_nil @metacat.find(:docid => @data_table_docid)
94
  end
95
  
96
  def test_returns_array_of_eml_objects?
97
    results = @metacat.find(:squery => @squery)
98
    assert_kind_of Array, results
99
    assert_kind_of Eml, results[0]
100
  end
101
  
102
  # Metacat.login/logout
103
  def test_login
104
    assert @metacat.login(@username, @password)
105
    assert @metacat.logged_in?
106
  end
107
  
108
  def test_logout
109
    assert @metacat.login(@username, @password)
110
    assert @metacat.logout
111
    assert_equal false, @metacat.logged_in?
112
  end  
113
  
114
  def test_failed_login
115
    assert_raise(MetacatPermissionDenied) do
116
      @metacat.login('bleh', @password)
117
    end
118
  end
119
  
120
  # Metacat.read
121
  def test_read_eml
122
    doc = @metacat.read(@eml_docid)
123
    assert_kind_of(REXML::Document, doc)
124
    assert_equal doc.root.name, 'eml'
125
  end
126
  
127
  def test_read_xml
128
    # not sure how to search for this yet
129
  end
130
  
131
  def test_read_data_table
132
    file = File.open('tmp.data_table', 'w+')
133
    @metacat.read(@data_table_docid) do |buffer|
134
      file.write(buffer)
135
    end
136
    file.close
137
    assert_equal(File.size('tmp.data_table'), File.size(File.dirname(__FILE__)+'/example.data_table'))
138
    File.delete('tmp.data_table')
139
  end
140
  
141
  # Metacat.squery
142
  def test_returns_xml
143
    doc = REXML::Document.new(@metacat.squery(@squery))    
144
    assert doc
145
    assert_equal 'resultset', doc.root.name
146
  end
147
  
148
  # Metacat.insert
149
  
150
  # Metacat.update
151
  
152
  # query_string
153
  def test_string_formatting
154
    hash = {
155
      'genus'     =>  'Caranx',
156
      'species'   =>  'melampygus'
157
    }
158
    assert_equal  '?genus=Caranx&species=melampygus', @metacat.send(:query_string, hash)
159
  end
160
end