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_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, TablesMatcher, UserRolesMatcher

Instance Method Summary collapse

Instance Method Details

#be_brokenObject

Examples:

it {should_not be_broken}


354
355
356
# File 'lib/scraperwiki-api/matchers.rb', line 354

def be_broken
  ExceptionMessageMatcher.new nil
end

#be_editable_by(expected) ⇒ Object

Examples:

it {should be_editable_by 'frabcus'}


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

def be_editable_by(expected)
  UserRolesMatcher.new expected
end

#be_privateObject

Examples:

it {should be_private}


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

def be_private
  PrivacyStatusMatcher.new 'private'
end

#be_protectedObject

Examples:

it {should be_protected}


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

def be_protected
  PrivacyStatusMatcher.new 'visible'
end

#be_publicObject

Examples:

it {should be_public}


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

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')}


324
325
326
# File 'lib/scraperwiki-api/matchers.rb', line 324

def have_a_row_count_of(expected)
  CountMatcher.new expected
end

#have_at_least_the_keys(expected) ⇒ Object

Examples:

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


285
286
287
# File 'lib/scraperwiki-api/matchers.rb', line 285

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')}


304
305
306
# File 'lib/scraperwiki-api/matchers.rb', line 304

def have_at_most_the_keys(expected)
  ExtraKeysMatcher.new expected
end

#have_blank_valuesObject

Examples:

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


558
559
560
# File 'lib/scraperwiki-api/matchers.rb', line 558

def have_blank_values
  HaveBlankValues.new nil
end

#have_integer_valuesObject

Examples:

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


703
704
705
# File 'lib/scraperwiki-api/matchers.rb', line 703

def have_integer_values
  HaveIntegerValues.new nil
end

#have_run_within(expected) ⇒ Object

Examples:

it {should have_run_within(7).days}


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

def have_run_within(expected)
  LastRunMatcher.new expected
end

#have_unique_valuesObject

Examples:

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


646
647
648
# File 'lib/scraperwiki-api/matchers.rb', line 646

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')}


684
685
686
# File 'lib/scraperwiki-api/matchers.rb', line 684

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')}


596
597
598
# File 'lib/scraperwiki-api/matchers.rb', line 596

def have_values_matching(expected)
  HaveValuesMatching.new expected
end

#have_values_of(expected) ⇒ Object

Examples:

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


577
578
579
# File 'lib/scraperwiki-api/matchers.rb', line 577

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')}


665
666
667
# File 'lib/scraperwiki-api/matchers.rb', line 665

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')}


753
754
755
# File 'lib/scraperwiki-api/matchers.rb', line 753

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')}


772
773
774
# File 'lib/scraperwiki-api/matchers.rb', line 772

def have_values_with_at_most_the_keys(expected)
  HaveValuesWithAtMostTheKeys.new expected
end

#never_runObject

Examples:

it {should never_run}


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

def never_run
  RunIntervalMatcher.new :never
end

#run(expected) ⇒ Object

Examples:

it {should run(:daily)}


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

def run(expected)
  RunIntervalMatcher.new expected
end

#set_any_of(expected) ⇒ Object

Examples:

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


449
450
451
# File 'lib/scraperwiki-api/matchers.rb', line 449

def set_any_of(expected)
  SetAnyOf.new expected
end