Skip to content

Commit

Permalink
Added unit tests with an enforced minimum coverage (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesiarmes authored Jun 9, 2023
1 parent 05325d9 commit f42d4e4
Show file tree
Hide file tree
Showing 55 changed files with 1,192 additions and 46 deletions.
1 change: 1 addition & 0 deletions .github/config/rubocop_linter_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ check_name: 'RuboCop Results'
versions:
- rubocop: 'latest'
- rubocop-rake: 'latest'
- rubocop-rspec: 'latest'

# Description: RuboCop configuration file path relative to the workspace.
# Valid options: A valid file path inside of the workspace.
Expand Down
16 changes: 15 additions & 1 deletion .github/workflows/branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: RuboCop Linter
uses: andrewmcodes/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

spec:
runs-on: ubuntu-latest
env:
COVERAGE: 1

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
# runs 'bundle install' and caches installed gems automatically
bundler-cache: true
- name: Run tests
run: bundle exec rspec
16 changes: 15 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: RuboCop Linter
uses: andrewmcodes/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

spec:
runs-on: ubuntu-latest
env:
COVERAGE: 1

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
# runs 'bundle install' and caches installed gems automatically
bundler-cache: true
- name: Run tests
run: bundle exec rspec
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ vendor
# Ignore import and export files.
./*.csv
./*.json

# Ignore test outputs
coverage
4 changes: 4 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--require spec_helper.rb
--color
--format RSpec::Github::Formatter
--format documentation
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require:
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
Expand All @@ -10,3 +11,8 @@ AllCops:

Naming/MemoizedInstanceVariableName:
Enabled: false

# Favor more explicit contexts over limited nesting.
RSpec/NestedGroups:
AllowedGroups:
- context
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ gemspec
group :development do
gem 'rake', '~> 13.0'
gem 'rubocop', '~> 1.48'
gem 'rubocop-factory_bot', '~> 2.23'
gem 'rubocop-rake', '~> 0.6'
gem 'rubocop-rspec', '~> 2.22'
end

group :test do
gem 'factory_bot', '~> 6.2'
gem 'rspec', '~> 3.12'
gem 'rspec-github', '~> 2.4'
gem 'simplecov', '~> 0.22'
end
67 changes: 54 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (7.0.4.3)
activesupport (= 7.0.4.3)
activerecord (7.0.4.3)
activemodel (= 7.0.4.3)
activesupport (= 7.0.4.3)
activesupport (7.0.4.3)
activemodel (7.0.5)
activesupport (= 7.0.5)
activerecord (7.0.5)
activemodel (= 7.0.5)
activesupport (= 7.0.5)
activesupport (7.0.5)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand All @@ -29,14 +29,18 @@ GEM
ast (2.4.2)
bson (4.15.0)
concurrent-ruby (1.2.2)
down (5.4.0)
diff-lcs (1.5.0)
docile (1.4.0)
down (5.4.1)
addressable (~> 2.8)
faraday (2.7.4)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
faraday (2.7.6)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
file_exists (0.2.0)
i18n (1.13.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
ibm_db (5.4.1)
activerecord (< 7.1)
Expand All @@ -48,14 +52,31 @@ GEM
mongo (2.18.2)
bson (>= 4.14.1, < 5.0.0)
parallel (1.23.0)
parser (3.2.2.1)
parser (3.2.2.3)
ast (~> 2.4.1)
racc
public_suffix (5.0.1)
racc (1.7.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.0)
rexml (3.2.5)
rubocop (1.51.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-github (2.4.0)
rspec-core (~> 3.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.52.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
Expand All @@ -65,13 +86,27 @@ GEM
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.1)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.18.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.23.1)
rubocop (~> 1.33)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.22.0)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sequel (5.68.0)
sequel (5.69.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
thor (1.2.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -86,9 +121,15 @@ PLATFORMS

DEPENDENCIES
cmr-entity-resolution!
factory_bot (~> 6.2)
rake (~> 13.0)
rspec (~> 3.12)
rspec-github (~> 2.4)
rubocop (~> 1.48)
rubocop-factory_bot (~> 2.23)
rubocop-rake (~> 0.6)
rubocop-rspec (~> 2.22)
simplecov (~> 0.22)

BUNDLED WITH
2.4.10
7 changes: 5 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

require 'rspec/core/rake_task'
require 'rubocop/rake_task'

task default: [:rubocop]
task default: %i[spec rubocop]

RuboCop::RakeTask.new do |task|
RuboCop::RakeTask.new(:rubocop) do |task|
task.requires << 'rubocop'
end

RSpec::Core::RakeTask.new(:spec)
2 changes: 1 addition & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def from_file(path)

def initialize
defaults
yield self
yield self if block_given?
initialize_logger
end

Expand Down
6 changes: 6 additions & 0 deletions lib/destination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

# Helper methods for loading destinations.
module Destination
class InvalidDestination < RuntimeError; end

# Load a destination based on the configuration file.
#
# @param destination_config [Hash] Destination configuration from the config
# file.
# @return [Destination::Base]
#
# @raise [InvalidDestination] When the destination type can not be found.
def self.from_config(destination_config)
Object.const_get("Destination::#{destination_config[:type]}").new(destination_config)
rescue NameError
raise InvalidDestination, "Unknown destination type #{destination_config[:type]}"
end
end
2 changes: 1 addition & 1 deletion lib/destination/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def add_record(record)
#
# @return [Hash]
def defaults
{ field_map: [] }
{ field_map: {}, transformations: [] }
end
end
end
1 change: 1 addition & 0 deletions lib/destination/jsonl.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'json'
require_relative 'file'

module Destination
Expand Down
8 changes: 5 additions & 3 deletions lib/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

require 'faraday'
require_relative 'destination'
require_relative 'transformation'
require_relative 'transformable'

# Exports data from senzing into a configured destination.
class Export
include Transformable

def initialize(config)
@config = config
end
Expand All @@ -25,12 +27,12 @@ def from_file
private

def process_record(record)
Transformation.transform(@config, record, destination.config[:transformations])
record = transform(destination, record)

# Map fields.
output = {}
@destination.config[:field_map].each do |field, map|
output[map] = record[field]
output[map.to_sym] = record[field]
end

output
Expand Down
14 changes: 13 additions & 1 deletion lib/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

# Filter records during processing.
module Filter
class InvalidFilter < RuntimeError; end

# Pass a record through all configured filters to determine if it should be
# kept.
#
# @param config [Config] Configuration object.
# @param record [CSV::Row] The record to filter.
# @param record [Hash] The record to filter.
# @return [Boolean] Whether or not this record should be included.
def self.filter(config, record)
result = config.filters.all? do |filter|
Expand All @@ -21,9 +23,19 @@ def self.filter(config, record)
result
end

# Load a filter based on the defined configuration.
#
# @param filter_config [Hash|String] The name of a filter or a configuration
# hash for one.
# @return [Filter::Base]
#
# @raise [InvalidFilter] When the filter can not be found.
def self.filter_from_config(filter_config)
return Object.const_get("Filter::#{filter_config}").new unless filter_config.is_a?(Hash)

Object.const_get("Filter::#{filter_config[:filter]}").new(filter_config)
rescue NameError
type = filter_config.is_a?(Hash) ? filter_config[:filter] : filter_config
raise InvalidFilter, "Unknown filter type #{type}"
end
end
7 changes: 7 additions & 0 deletions lib/filter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ def initialize(filter_config = {})
@filter_config = defaults.merge(filter_config)
end

# Returns the configuration for the current filter.
#
# @return [Hash]
def config
@filter_config
end

# Apply the filter to a record to determine if it should be kept.
#
# @param record [CSV::ROW] The record to apply the filter to.
Expand Down
14 changes: 14 additions & 0 deletions lib/filterable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative 'filter'

# Helper module that adds transformation support.
module Filterable
# Apply filters to a record
#
# @param record [Hash] The record to apply filters to.
# @return [Boolean] Whether or not the record should be included.
def filter(record)
Filter.filter(@config, record)
end
end
Loading

0 comments on commit f42d4e4

Please sign in to comment.