Skip to content

Commit

Permalink
adding some descriptive comments for PackageController and Validation…
Browse files Browse the repository at this point in the history
… model.

KNOWN ISSUE 'Scenario: Enter multiple URLs for validation' will fail if it is executed without other features executed preceding it
  • Loading branch information
quadrophobiac committed Jul 13, 2015
1 parent 2aeaead commit 502b5a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
31 changes: 12 additions & 19 deletions app/controllers/package_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
class PackageController < ApplicationController
before_filter :preprocess, :only => :create

# preprocess performs necessary formatting of appended or hyperlinked files on the CSVlint frontend

before_filter(:only => [:show]) { alternate_formats [:json] }

def create
Expand Down Expand Up @@ -35,7 +37,6 @@ def create
redirect_to package_path(package)
end
end
# byebug
end

def show
Expand All @@ -53,15 +54,10 @@ def show

def preprocess
remove_blanks!
# byebug
# pass files to function and return data as ActionDispatch object
params[:files] = read_files(params[:files_data]) unless params[:files_data].blank?
params[:schema_file] = read_files(params[:schema_data]).first unless params[:schema_data].blank? # currently supporting the case of all_constraints+
# params[:schema_file] = read_files(params[:schema_file]).first unless params[:schema_file].blank?
# params[:schema_file].class == ActionDispatch::Http::UploadedFile, current read_files methods will not work on this class

# the above do not run as unless evals to true when a file is uploaded OR when a URL is uploaded
params[:schema_file] = read_files(params[:schema_data]).first unless params[:schema_data].blank?
redirect_to root_path and return unless urls_valid? || params[:files].presence

load_schema
Zipfile.check!(params)
end
Expand All @@ -87,7 +83,6 @@ def remove_blanks!
end

def load_schema

# Check that schema checkbox is ticked
return unless params[:schema] == "1"
# Load schema
Expand All @@ -101,18 +96,14 @@ def load_schema
@schema = Csvlint::Schema.from_json_table( nil, schema_json )

rescue JSON::ParserError
# catch JSON parse error
# this rescue requires further work, currently in place to catch malformed or bad json uploaded schemas
@schema = Csvlint::Schema.new(nil, [], "malformed", "malformed")
# cludge - array has to be empty due to how these schemas are created in gem,
# populating said array with strings will result in undefined method `name' for "name":String
rescue
@schema = nil
end
@schema_url = "true"
# kludge solution, awaiting a logic change but which requires a refactor of schema_url across project
end

# Get schema URL from parameters
# @schema_url = params[:schema_url]
@schema_url = params[:schema_url]
end

def check_for_package
Expand All @@ -121,12 +112,13 @@ def check_for_package
end

def read_files(data)

# returns an ActionDispatch file
# this process is not evaluated by the feature tests because they can be preprocessed as ActionDispatch files
files = []
data = [data] if data.class == String
# byebug
# converts the base64 schema to an array for parsing below
data.each do |data|
# undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x007fb9a3dc6f90

file_array = data.split(";", 2)
filename = file_array[0]
uri = URI::Data.new(file_array[1])
Expand All @@ -144,6 +136,7 @@ def read_files(data)
files << file
end
files

end

end
14 changes: 9 additions & 5 deletions features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
visit root_path
end

# below used when schema or csv hyperlinked to
When(/^I enter "(.*?)" in the "(.*?)" field$/) do |text, field|
instance_variable_set("@#{field.downcase.parameterize.underscore}", text)
if field == "url"
Expand All @@ -11,6 +12,13 @@
end
end

# below used when schema or csv attached
When(/^I attach the file "(.*?)" to the "(.*?)" field$/) do |file, field_name|
@file = file
field_name = "files[]" if field_name == "file"
attach_file(field_name.to_sym, File.join(Rails.root, 'fixtures', @file))
end

When(/^I enter the CKAN repository "(.*?)" in the url field$/) do |url|
@url = url
fill_in "url_0", with: @url
Expand All @@ -24,11 +32,7 @@
click_button name
end

When(/^I attach the file "(.*?)" to the "(.*?)" field$/) do |file, field_name|
@file = file
field_name = "files[]" if field_name == "file"
attach_file(field_name.to_sym, File.join(Rails.root, 'fixtures', @file))
end


Then(/^I should see "(.*?)"$/) do |text|
page.body.should include(text)
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/package_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
],
schema: "1",
schema_data: create_data_uri('schemas/all_constraints.json', 'application/json')
# above accurately emulates how the file upload works by a user
response.should be_redirect
package = Package.first
validation = package.validations.first
Expand Down

0 comments on commit 502b5a3

Please sign in to comment.