Module: ScraperWiki::API::Matchers

Defined in:
lib/scraperwiki-api/matchers.rb

Overview

RSpec matchers for ScraperWiki scrapers.

Examples:

require 'scraperwiki-api'
api = ScraperWiki::API.new

info = api.scraper_getinfo('example-scraper').first

describe 'example-scraper' do
  include ScraperWiki::API::Matchers
  subject {info}

  it {should be_protected}
  it {should be_editable_by('frabcus')}
  it {should run(:daily)}
  it {should_not be_broken}
  it {should have_a_table('swdata')}
  it {should have_a_row_count_of(42).on('swdata')}

  # Check for missing keys:
  it {should have_at_least_the_keys(['name', 'email']).on('swdata')}

  # Check for extra keys:
  it {should have_at_most_the_keys(['name', 'email', 'tel', 'fax']).on('swdata')}
end

data = api.datastore_sqlite('example-scraper', 'SELECT * from `swdata`')

describe 'example-scraper' do
  include ScraperWiki::API::Matchers
  subject {data}

  it {should set_any_of(['name', 'first_name', 'last_name'])}

  # Validate the values of individual fields:
  it {should_not have_blank_values.in('name')}
  it {should have_unique_values.in('email')}
  it {should have_values_of(['M', 'F']).in('gender')}
  it {should have_values_matching(/\A[^@\s]+@[^@\s]+\z/).in('email')}
  it {should have_values_starting_with('http://').in('url')}
  it {should have_values_ending_with('Inc.').in('company_name')}
  it {should have_integer_values.in('year')}

  # If you store a hash or an array of hashes in a field as a JSON string,
  # you can validate the values of these subfields by chaining on an +at+:
  it {should have_values_of(['M', 'F']).in('extra').at('gender')}

  # Check for missing keys within subfields:
  it {should have_values_with_at_least_the_keys(['subfield1', 'subfield2']).in('fieldA')}

  # Check for extra keys within subfields:
  it {should have_values_with_at_most_the_keys(['subfield1', 'subfield2', 'subfield3', 'subfield4']).in('fieldA')}
end

See Also:

Defined Under Namespace

Classes: CountMatcher, CustomMatcher, DatastoreMatcher, ExceptionMessageMatcher, ExtraKeysMatcher, FieldKeyMatcher, FieldMatcher, HaveBlankValues, HaveIntegerValues, HaveUniqueValues, HaveValuesEndingWith, HaveValuesMatching, HaveValuesOf, HaveValuesStartingWith, HaveValuesWithAtLeastTheKeys, HaveValuesWithAtMostTheKeys, KeysMatcher, LastRunMatcher, MissingKeysMatcher, PrivacyStatusMatcher, RunEventsMatcher, RunIntervalMatcher, ScraperInfoMatcher, SetAnyOf, TableMatcher, TablesMatcher, UserRolesMatcher

Instance Method Summary collapse

Instance Method Details

#be_brokenObject

Examples:

it {should_not be_broken}


393
394
395
# File 'lib/scraperwiki-api/matchers.rb', line 393

def be_broken
  ExceptionMessageMatcher.new nil
end

#be_editable_by(expected) ⇒ Object

Examples:

it {should be_editable_by 'frabcus'}


138
139
140
# File 'lib/scraperwiki-api/matchers.rb', line 138

def be_editable_by(expected)
  UserRolesMatcher.new expected
end

#be_privateObject

Examples:

it {should be_private}


116
117
118
# File 'lib/scraperwiki-api/matchers.rb', line 116

def be_private
  PrivacyStatusMatcher.new 'private'
end

#be_protectedObject

Examples:

it {should be_protected}


111
112
113
# File 'lib/scraperwiki-api/matchers.rb', line 111

def be_protected
  PrivacyStatusMatcher.new 'visible'
end

#be_publicObject

Examples:

it {should be_public}


106
107
108
# File 'lib/scraperwiki-api/matchers.rb', line 106

def be_public
  PrivacyStatusMatcher.new 'public'
end

#have_a_row_count_of(expected) ⇒ Object

Examples:

it {should have_a_row_count_of(42).on('swdata')}


363
364
365
# File 'lib/scraperwiki-api/matchers.rb', line 363

def have_a_row_count_of(expected)
  CountMatcher.new expected
end

#have_a_table(expected) ⇒ Object

Examples:

it {should have_a_table('swdata')}


253
254
255
# File 'lib/scraperwiki-api/matchers.rb', line 253

def have_a_table(expected)
  TableMatcher.new expected
end

#have_at_least_the_keys(expected) ⇒ Object

Examples:

it {should have_at_least_the_keys(['fieldA', 'fieldB']).on('swdata')}


311
312
313
# File 'lib/scraperwiki-api/matchers.rb', line 311

def have_at_least_the_keys(expected)
  MissingKeysMatcher.new expected
end

#have_at_most_the_keys(expected) ⇒ Object

Examples:

it {should have_at_most_the_keys(['fieldA', 'fieldB', 'fieldC', 'fieldD']).on('swdata')}


335
336
337
# File 'lib/scraperwiki-api/matchers.rb', line 335

def have_at_most_the_keys(expected)
  ExtraKeysMatcher.new expected
end

#have_blank_valuesObject

Examples:

it {should_not have_blank_values.in('name')}


601
602
603
# File 'lib/scraperwiki-api/matchers.rb', line 601

def have_blank_values
  HaveBlankValues.new nil
end

#have_integer_valuesObject

Examples:

it {should have_integer_values.in('year')}


746
747
748
# File 'lib/scraperwiki-api/matchers.rb', line 746

def have_integer_values
  HaveIntegerValues.new nil
end

#have_run_within(expected) ⇒ Object

Examples:

it {should have_run_within(7).days}


233
234
235
# File 'lib/scraperwiki-api/matchers.rb', line 233

def have_run_within(expected)
  LastRunMatcher.new expected
end

#have_unique_valuesObject

Examples:

it {should have_unique_values.in('email')}


689
690
691
# File 'lib/scraperwiki-api/matchers.rb', line 689

def have_unique_values
  HaveUniqueValues.new nil
end

#have_values_ending_with(expected) ⇒ Object

Examples:

it {should have_values_ending_with('Inc.').in('company_name')}


727
728
729
# File 'lib/scraperwiki-api/matchers.rb', line 727

def have_values_ending_with(expected)
  HaveValuesEndingWith.new expected
end

#have_values_matching(expected) ⇒ Object

Examples:

it {should have_values_matching(/\A[^@\s]+@[^a\s]+\z/).in('email')}


639
640
641
# File 'lib/scraperwiki-api/matchers.rb', line 639

def have_values_matching(expected)
  HaveValuesMatching.new expected
end

#have_values_of(expected) ⇒ Object

Examples:

it {should have_values_of(['M', 'F']).in('gender')}


620
621
622
# File 'lib/scraperwiki-api/matchers.rb', line 620

def have_values_of(expected)
  HaveValuesOf.new expected
end

#have_values_starting_with(expected) ⇒ Object

Examples:

it {should have_values_starting_with('http://').in('url')}


708
709
710
# File 'lib/scraperwiki-api/matchers.rb', line 708

def have_values_starting_with(expected)
  HaveValuesStartingWith.new expected
end

#have_values_with_at_least_the_keys(expected) ⇒ Object

Examples:

it {should have_values_with_at_least_the_keys(['subfield1', 'subfield2']).in('fieldA')}


796
797
798
# File 'lib/scraperwiki-api/matchers.rb', line 796

def have_values_with_at_least_the_keys(expected)
  HaveValuesWithAtLeastTheKeys.new expected
end

#have_values_with_at_most_the_keys(expected) ⇒ Object

Examples:

it {should have_values_with_at_most_the_keys(['subfield1', 'subfield2', 'subfield3', 'subfield4']).in('fieldA')}


815
816
817
# File 'lib/scraperwiki-api/matchers.rb', line 815

def have_values_with_at_most_the_keys(expected)
  HaveValuesWithAtMostTheKeys.new expected
end

#never_runObject

Examples:

it {should never_run}


171
172
173
# File 'lib/scraperwiki-api/matchers.rb', line 171

def never_run
  RunIntervalMatcher.new :never
end

#run(expected) ⇒ Object

Examples:

it {should run(:daily)}


166
167
168
# File 'lib/scraperwiki-api/matchers.rb', line 166

def run(expected)
  RunIntervalMatcher.new expected
end

#set_any_of(expected) ⇒ Object

Examples:

it {should set_any_of(['name', 'first_name', 'last_name'])}


492
493
494
# File 'lib/scraperwiki-api/matchers.rb', line 492

def set_any_of(expected)
  SetAnyOf.new expected
end