diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c69fec0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +lib/goodsheet/spreadsheet_v1.rb +notex.txt \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a95e290 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in goodsheet.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..30df398 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2013 Iwan Buetti + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c13210f --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# Goodsheet + +Read and validate the content of a spreadsheet. +The gem take advantage of wonderful validation methods available in Rails ActiveModel library and the methods of Roo gem to read and validate a spreadsheet. +Refer to the [official guide](http://guides.rubyonrails.org/active_record_validations.html) for the validation rules. +Thanks to [Roo gem](https://github.com/Empact/roo) Goodsheet can handle OpenOffice, LibreOffice, Excel (both '.xls' and '.xlsx') and Google spreadsheets. + + +## Installation + +Add this line to your application's Gemfile: + + gem 'goodsheet' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install goodsheet + +## Usage + +### Getting started + +```ruby +ss = Goodsheet::Spreadsheet.new("my_data.xlsx") +res = ss.read do + column_names :a => 0, :b => 1 + validates :a, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 10 } + validates :b, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 100 } +end + +res.valid? # => true +res.values # => {:a => [1.0, 1.0, 1.4], :b => []} +``` + +By default: +* the first sheet is selected +* one line (the first) is skipped (i'm expeting that is the header line) + +Pass your validation rules into the block passed to the read method, together with the column_names method that define the position (or index) and the name of the columns you want to read. + +### Advanced usage + +to do + + + +Warning: +* integer numbers are converted to float numbers. Also don't pretend to obtain an integer in validation. This undesired behaviour depend on Roo gem +* if you import data from a CSV spreadsheet keep in mind that numbers are readed as strings + + + + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..0f55802 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require "bundler/gem_tasks" +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs << 'test' +end + +desc "Run tests" +task :default => :test diff --git a/goodsheet.gemspec b/goodsheet.gemspec new file mode 100644 index 0000000..2cbb909 --- /dev/null +++ b/goodsheet.gemspec @@ -0,0 +1,27 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'goodsheet/version' + +Gem::Specification.new do |spec| + spec.name = "goodsheet" + spec.version = Goodsheet::VERSION + spec.authors = ["Iwan Buetti"] + spec.email = ["iwan.buetti@gmail.com"] + spec.description = "Little gem that take advantage of Roo gem and Rails ActiveModel validation methods to read and validate the content of a spreadsheet" + spec.summary = "Extract and validate data from a spreadsheet" + spec.homepage = "https://github.com/iwan/goodsheet" + spec.license = "MIT" + spec.date = '2013-07-19' + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.4" + spec.add_development_dependency "rake" + + spec.add_dependency('roo', '>= 1.12.1') # https://github.com/Empact/roo + spec.add_dependency('activemodel', '>= 3.2.14') + spec.add_dependency('google-drive-ruby') +end diff --git a/lib/goodsheet.rb b/lib/goodsheet.rb new file mode 100644 index 0000000..4f396f1 --- /dev/null +++ b/lib/goodsheet.rb @@ -0,0 +1,12 @@ +require "goodsheet/version" + +module Goodsheet + autoload :Row, 'goodsheet/row' + autoload :SheetNotFoundError, 'goodsheet/exceptions' + autoload :ReadResult, 'goodsheet/read_result' + autoload :Row, 'goodsheet/row' + autoload :Spreadsheet, 'goodsheet/spreadsheet' + autoload :ValidationError, 'goodsheet/validation_error' + autoload :ValidationErrors, 'goodsheet/validation_errors' + autoload :Version, 'goodsheet/version' +end diff --git a/lib/goodsheet/exceptions.rb b/lib/goodsheet/exceptions.rb new file mode 100644 index 0000000..995ad61 --- /dev/null +++ b/lib/goodsheet/exceptions.rb @@ -0,0 +1,5 @@ +module Goodsheet + + class SheetNotFoundError < StandardError; end + +end diff --git a/lib/goodsheet/read_result.rb b/lib/goodsheet/read_result.rb new file mode 100644 index 0000000..9dd18bb --- /dev/null +++ b/lib/goodsheet/read_result.rb @@ -0,0 +1,28 @@ +module Goodsheet + + class ReadResult + attr_reader :values + + def initialize(errors=ValidationErrors.new) + @errors = errors + @values = {} + end + + def valid? + @errors.empty? + end + + def invalid? + !valid? + end + + def add(attribute, row, force_nil=nil) + attribute = attribute.to_sym + (@values[attribute] ||= []) << (row.send(attribute) || force_nil) + end + + def errors + @errors.array + end + end +end \ No newline at end of file diff --git a/lib/goodsheet/row.rb b/lib/goodsheet/row.rb new file mode 100644 index 0000000..e2f259b --- /dev/null +++ b/lib/goodsheet/row.rb @@ -0,0 +1,76 @@ +require 'active_model' + +module Goodsheet + + class Row + include ActiveModel::Validations + include ActiveModel::Conversion + extend ActiveModel::Naming + + class << self + attr_accessor :keys + end + @keys = {} # idx => key + + def initialize(arr) + arr.each_with_index do |v, idx| + if k = self.class.keys[idx] + send("#{k}=", v) + end + end + super() + end + + def self.inherit(block) + c = Class.new(self) do + @keys = {} # idx => key + end + c.class_eval(&block) + c + end + + # Define the position (or index) and the name of columns. + # There are available three mode to define them: + # using an hash index to name (like { 0 => :year, 2 => :day }) + # or name to index (like { :year => 0, :day => 2 }) or using an array + # with the names at desired positions (like [:year, nil, :day]), put a nil + # at the position + # The positions are 0-based. + def self.column_names(param) + @keys = {} + if param.is_a? Hash + if param.first[0].is_a? Integer + param.each do |idx, name| + self.keys[idx] = name + attr_accessor name + end + else + param.each do |name, idx| + self.keys[idx] = name + attr_accessor name + end + end + elsif param.is_a? Array + param.each_with_index do |name, idx| + if name + self.keys[idx] = name + attr_accessor name + end + end + + else + raise "parameter non valid" + end + end + + + def persisted? + false + end + + # Get the list of attributes (the columns to import) + def self.row_attributes + @keys.values + end + end +end diff --git a/lib/goodsheet/spreadsheet.rb b/lib/goodsheet/spreadsheet.rb new file mode 100644 index 0000000..e284a37 --- /dev/null +++ b/lib/goodsheet/spreadsheet.rb @@ -0,0 +1,130 @@ +require 'roo' + +module Goodsheet + + class Spreadsheet < Roo::Spreadsheet + attr_reader :time_zone, :skip, :header_row, :max_errors, :row_limit + + # Valid options: + # :skip : number of rows to skip (default: 1) + # :header_row : header's row index (0 based, default: 0) + # :time_zone : time zone string + def initialize(filename, options={}) + set_options(options) + @filename = filename + @ss = Roo::Spreadsheet.open(filename, options) + end + + + # idx can be a number or a string + def sheet(idx, options={}) + set_options(options) + @ss.sheet(idx) + check_sheet_exists + end + + def sheets + @ss.sheets + end + + + def get_header + @ss.row(@header_row+1) # because roo in 1-based + end + + # Get the currently selected sheet's name + def name + @ss.default_sheet + end + + def total_rows + @ss.parse.size + end + + def rows_wo_header + @ss.parse.size - @skip + end + alias :rows :rows_wo_header + + # Valid options: + # :max_errors : The validation will be stopped if the number of errors exceed max_errors (default: 0 or don't stop) + # :limit : Max number of rows to validate (default: 0 or validate all rows) + + # + def validate(options={}, &block) + skip = options[:skip] || @skip + header_row = options[:header_row] || @header_row + max_errors = options[:max_errors] || @max_errors + row_limit = options[:row_limit] || @row_limit + + validation_errors = ValidationErrors.new + + my_class = options[:my_custom_row_class] || build_my_class(block) + + line = skip # 0-based, from the top + @ss.parse[skip..-1].each do |row| # row is an array of elements + validation_errors.add(line, my_class.new(row)) + break if max_errors>0 && validation_errors.size >= max_errors + break if row_limit && row_limit>0 && line>=(row_limit+skip-1) + line +=1 + end + validation_errors + end + + + # Columns must be an hash: labe for values and the column index like {:price => 5} + def read(options={}, &block) + skip = options[:skip] || @skip + header_row = options[:header_row] || @header_row + max_errors = options[:max_errors] || @max_errors + row_limit = options[:row_limit] || @row_limit + force_nil = options[:force_nil] + + my_class = build_my_class(block) + options[:my_custom_row_class] = my_class + read_result = ReadResult.new(validate(options){ block }) + return read_result if read_result.invalid? + + line = skip # 0-based, from the top + @ss.parse[skip..-1].each do |row| # row is an array of elements + my_class.row_attributes.each do |attribute| + read_result.add(attribute, my_class.new(row), force_nil) + end + break if row_limit && row_limit>0 && line>=(row_limit + skip - 1) + line +=1 + end + read_result + end + + + private + + def build_my_class(block) + Object.const_set get_custom_row_class_name, Row.inherit(block) + end + + def check_sheet_exists + begin + @ss.cell(1,1) + rescue ArgumentError => e + raise Goodsheet::SheetNotFoundError + rescue RangeError => e + raise Goodsheet::SheetNotFoundError + end + end + + def get_custom_row_class_name + "CustRow_#{(Time.now.to_f*(10**10)).to_i}" + end + + def set_options(options) + @time_zone = options.delete(:zone) || "Rome" + @skip = options.delete(:skip) || 1 + @header_row = options.delete(:header_row) || 0 + @max_errors = options.delete(:max_errors) || 0 + @row_limit = options.delete(:row_limit) || 0 + end + end +end + + diff --git a/lib/goodsheet/validation_error.rb b/lib/goodsheet/validation_error.rb new file mode 100644 index 0000000..ec2267d --- /dev/null +++ b/lib/goodsheet/validation_error.rb @@ -0,0 +1,13 @@ +module Goodsheet + + class ValidationError + def initialize(line, val_err) + @line = line + @val_err = val_err + end + + def to_s + "line #{@line} is invalid for the following reason(s): #{@val_err.full_messages.join(', ')}" + end + end +end \ No newline at end of file diff --git a/lib/goodsheet/validation_errors.rb b/lib/goodsheet/validation_errors.rb new file mode 100644 index 0000000..2e35356 --- /dev/null +++ b/lib/goodsheet/validation_errors.rb @@ -0,0 +1,26 @@ +module Goodsheet + + class ValidationErrors + attr_reader :array + + def initialize + @array = [] + end + + def add(line_number, row) + @array << ValidationError.new(line_number+1, row.errors) if row.invalid? + end + + def empty? + @array.empty? + end + + def size + @array.size + end + + def to_s + @array.to_s + end + end +end diff --git a/lib/goodsheet/version.rb b/lib/goodsheet/version.rb new file mode 100644 index 0000000..1c5d641 --- /dev/null +++ b/lib/goodsheet/version.rb @@ -0,0 +1,3 @@ +module Goodsheet + VERSION = "0.2.1" +end diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..438e619 --- /dev/null +++ b/notes.txt @@ -0,0 +1,68 @@ + +Usage: + +ss = Goodsheet::Spreadsheet.new("data.xls") +ss.sheet(0) # selecting the first sheet +result = ss.read(:skip => 1) do + column_at 0, :year, :presence => true, :numericality => { :only_integer => false, :greater_than_or_equal_to => 2000, :less_than_or_equal_to => 2100 } +end +result.values # => { :year => [2012, 2012, 2012, 2013] } + +# when the validation fails: +ss.sheet(1) # selecting the second sheet +result = ss.read(:skip => 1) do + column_at 0, :year, :presence => true, :numericality => { :only_integer => false, :greater_than_or_equal_to => 2000, :less_than_or_equal_to => 2100 } +end + +result.values # => {} +result.valid? # => false +result.errors # => return an array of ValidationError objects + +# -------------------- + +ss = Goodsheet::Spreadsheet.new("data.xls") +ss.sheets # => ["Sheet1", "Sheet2"] +ss.sheet(0) # selecting the first sheet +ss.name # => "Sheet1" +ss.sheet("Sheet1") # alternative way to select a sheet (by name) +ss.sheet("Sheet1", :header_row => 1) # you can specify the row index (0-based) used for header + + +Validation + + +class MyRow < Row + attr_accessor :year + validates :year, :presence => true, :numericality => { :only_integer => false, :greater_than_or_equal_to => 2000, :less_than_or_equal_to => 2100 } +end + +The validation method will return an array of validation errors: +validation_errors = ss.validate do |row| + MyRow.new(:year => row[0]) +end + +You can limit the validation errors using :max_errors option: +ss.validate(:max_errors => 100) do |row| + # ... +end + +or limit the numbers of rows to read: +ss.validate(:limit => 50) do |row| + # ... +end + + +Reading + +The read method allow to validate and put the content in an hash: +values = @ss.read do |row| + MyRow.new(:year => row[0]) +end + +# => + +The :force_nil option allow to force empties cells to a value, and the :skip to skip a desired number of rows: +values = @ss.read(:force_nil => 0.0, :skip => 6) do |row| + MyRow.new(:year => row[0]) +end + diff --git a/test/fixtures/fixtures_template.xlsx b/test/fixtures/fixtures_template.xlsx new file mode 100644 index 0000000..5365895 Binary files /dev/null and b/test/fixtures/fixtures_template.xlsx differ diff --git a/test/fixtures/ss_01.xls b/test/fixtures/ss_01.xls new file mode 100644 index 0000000..1e9bb59 Binary files /dev/null and b/test/fixtures/ss_01.xls differ diff --git a/test/fixtures/ss_02.csv b/test/fixtures/ss_02.csv new file mode 100644 index 0000000..60131bb --- /dev/null +++ b/test/fixtures/ss_02.csv @@ -0,0 +1 @@ +year,month,day,wday,1 or 2 or 3,value 2013,1,1,Tue,1,78.8 2013,1,2,Wed,3,33.3 2013,1,3,Thu,1,35.3 2013,1,4,Fri,2,45 2013,1,5,Sat,2,41.4 2013,1,6,Sun,2,76.8 2013,1,7,Mon,1,78.5 2013,1,8,Tue,2,22 2013,1,9,Wed,3,35.8 2013,1,10,Thu,3,33.6 2013,1,11,Fri,3,80.8 2013,1,12,Sat,3,70.5 2013,1,13,Sun,2,75.6 2013,1,14,Mon,1,31.6 2013,1,15,Tue,1,22.3 2013,1,16,Wed,3,69.4 2013,1,17,Thu,1,66.1 2013,1,18,Fri,3,15.2 2013,1,19,Sat,1,59.4 2013,1,20,Sun,3,47.2 2013,1,21,Mon,2,2 2013,1,22,Tue,2,86.3 2013,1,23,Wed,2,6.6 2013,1,24,Thu,1,44.3 2013,1,25,Fri,3,1 2013,1,26,Sat,2,61.2 2013,1,27,Sun,1,69 2013,1,28,Mon,1,73.7 2013,1,29,Tue,1,48.2 2013,1,30,Wed,1,88.1 2013,1,31,Thu,2,59.6 2013,2,1,Fri,1,38.8 2013,2,2,Sat,3,85.9 2013,2,3,Sun,2,96.8 2013,2,4,Mon,2,23.1 2013,2,5,Tue,1,0.1 2013,2,6,Wed,2,26.6 2013,2,7,Thu,2,54.9 2013,2,8,Fri,3,43.8 2013,2,9,Sat,3,43.4 2013,2,10,Sun,2,74 2013,2,11,Mon,1,85.8 2013,2,12,Tue,1,6.5 2013,2,13,Wed,1,52.5 2013,2,14,Thu,3,82.5 2013,2,15,Fri,2,89.2 2013,2,16,Sat,2,8.9 2013,2,17,Sun,3,36.1 2013,2,18,Mon,2,50.6 2013,2,19,Tue,3,99.5 2013,2,20,Wed,1,61.5 2013,2,21,Thu,1,64.1 2013,2,22,Fri,1,15.6 2013,2,23,Sat,2,41.6 2013,2,24,Sun,2,11.4 2013,2,25,Mon,2,58.4 2013,2,26,Tue,2,12 2013,2,27,Wed,3,47.9 2013,2,28,Thu,2,52.4 2013,3,1,Fri,1,3.9 2013,3,2,Sat,2,71.7 2013,3,3,Sun,1,91.6 2013,3,4,Mon,2,23.7 2013,3,5,Tue,2,3.9 2013,3,6,Wed,1,50.8 2013,3,7,Thu,1,81.2 2013,3,8,Fri,3,71.9 2013,3,9,Sat,1,43.8 2013,3,10,Sun,3,70.9 2013,3,11,Mon,1,45.8 2013,3,12,Tue,1,19.7 2013,3,13,Wed,2,71.9 2013,3,14,Thu,1,97.9 2013,3,15,Fri,1,26.8 2013,3,16,Sat,3,77.9 2013,3,17,Sun,2,21.4 2013,3,18,Mon,1,8.4 2013,3,19,Tue,1,96.3 2013,3,20,Wed,2,5.8 2013,3,21,Thu,3,24.3 2013,3,22,Fri,1,23.3 2013,3,23,Sat,2,61.7 2013,3,24,Sun,2,67.1 2013,3,25,Mon,2,54.3 2013,3,26,Tue,3,76.4 2013,3,27,Wed,3,70.5 2013,3,28,Thu,2,81.6 2013,3,29,Fri,2,45 2013,3,30,Sat,2,78.4 2013,3,31,Sun,1,44.5 2013,4,1,Mon,3,85.8 2013,4,2,Tue,2,20.7 2013,4,3,Wed,1,51.3 2013,4,4,Thu,1,33.9 2013,4,5,Fri,2,12.3 2013,4,6,Sat,3,1.7 2013,4,7,Sun,1,91.3 2013,4,8,Mon,2,28.3 2013,4,9,Tue,2,73.2 2013,4,10,Wed,2,32.4 2013,4,11,Thu,3,67.1 2013,4,12,Fri,1,25.6 2013,4,13,Sat,3,26.1 2013,4,14,Sun,2,9.1 2013,4,15,Mon,3,44.7 2013,4,16,Tue,2,97.1 2013,4,17,Wed,2,59.6 2013,4,18,Thu,2,4.4 2013,4,19,Fri,2,78.6 2013,4,20,Sat,2,97.1 2013,4,21,Sun,3,45.1 2013,4,22,Mon,2,67.5 2013,4,23,Tue,3,59.2 2013,4,24,Wed,3,33.9 2013,4,25,Thu,1,56.5 2013,4,26,Fri,3,74.1 2013,4,27,Sat,2,66.5 2013,4,28,Sun,1,31.9 2013,4,29,Mon,1,36.1 2013,4,30,Tue,2,99 2013,5,1,Wed,1,71.7 2013,5,2,Thu,2,25.6 2013,5,3,Fri,2,2.6 2013,5,4,Sat,3,85.8 2013,5,5,Sun,3,13.7 2013,5,6,Mon,2,41.7 2013,5,7,Tue,1,0.9 2013,5,8,Wed,1,62.3 2013,5,9,Thu,3,7.9 2013,5,10,Fri,1,39.6 2013,5,11,Sat,3,79.4 2013,5,12,Sun,1,34.3 2013,5,13,Mon,2,38.5 2013,5,14,Tue,3,53.6 2013,5,15,Wed,3,48.7 2013,5,16,Thu,3,24.4 2013,5,17,Fri,1,77 2013,5,18,Sat,1,82.5 2013,5,19,Sun,1,57.7 2013,5,20,Mon,1,45.4 2013,5,21,Tue,3,7.1 2013,5,22,Wed,3,64.5 2013,5,23,Thu,3,87.6 2013,5,24,Fri,1,38.3 2013,5,25,Sat,2,37.6 2013,5,26,Sun,3,66.4 2013,5,27,Mon,1,34.2 2013,5,28,Tue,1,70.1 2013,5,29,Wed,2,62.6 2013,5,30,Thu,1,59 2013,5,31,Fri,3,24.8 2013,6,1,Sat,3,96.5 2013,6,2,Sun,2,73.6 2013,6,3,Mon,2,18 2013,6,4,Tue,3,87.9 2013,6,5,Wed,1,37.9 2013,6,6,Thu,3,30.4 2013,6,7,Fri,1,88.1 2013,6,8,Sat,2,55 2013,6,9,Sun,3,82.9 2013,6,10,Mon,3,55.1 2013,6,11,Tue,1,44 2013,6,12,Wed,3,67.1 2013,6,13,Thu,1,82.2 2013,6,14,Fri,1,73.9 2013,6,15,Sat,3,54 2013,6,16,Sun,1,3.1 2013,6,17,Mon,1,74.1 2013,6,18,Tue,2,27.7 2013,6,19,Wed,3,98.3 2013,6,20,Thu,1,80.2 2013,6,21,Fri,3,25.3 2013,6,22,Sat,2,23.8 2013,6,23,Sun,3,49.1 2013,6,24,Mon,2,53.5 2013,6,25,Tue,1,8.9 2013,6,26,Wed,1,88.7 2013,6,27,Thu,2,59 2013,6,28,Fri,3,48.3 2013,6,29,Sat,3,38.1 2013,6,30,Sun,2,42.9 2013,7,1,Mon,1,61.9 2013,7,2,Tue,2,56.9 2013,7,3,Wed,1,4 2013,7,4,Thu,1,18.2 2013,7,5,Fri,2,31.7 2013,7,6,Sat,1,36.3 2013,7,7,Sun,3,9.2 2013,7,8,Mon,1,67.3 2013,7,9,Tue,3,12.8 2013,7,10,Wed,2,72.1 2013,7,11,Thu,1,44.8 2013,7,12,Fri,2,79.9 2013,7,13,Sat,1,15.9 2013,7,14,Sun,3,87.7 2013,7,15,Mon,3,43.8 2013,7,16,Tue,3,5.3 2013,7,17,Wed,2,80.2 2013,7,18,Thu,2,9.4 2013,7,19,Fri,3,62.6 2013,7,20,Sat,3,35.6 2013,7,21,Sun,3,26 2013,7,22,Mon,1,7.6 2013,7,23,Tue,3,82.1 2013,7,24,Wed,2,7 2013,7,25,Thu,1,54.8 2013,7,26,Fri,2,84.6 2013,7,27,Sat,1,71.3 2013,7,28,Sun,2,29 2013,7,29,Mon,2,84.6 2013,7,30,Tue,2,88.7 2013,7,31,Wed,1,0.1 2013,8,1,Thu,2,27.8 2013,8,2,Fri,3,98.4 2013,8,3,Sat,3,1.9 2013,8,4,Sun,2,71.2 2013,8,5,Mon,1,75.4 2013,8,6,Tue,1,27.4 2013,8,7,Wed,3,93.8 2013,8,8,Thu,3,30.6 2013,8,9,Fri,1,9.1 2013,8,10,Sat,1,61.6 2013,8,11,Sun,1,52.2 2013,8,12,Mon,3,69.6 2013,8,13,Tue,1,9 2013,8,14,Wed,1,11.9 2013,8,15,Thu,3,8.9 2013,8,16,Fri,2,47.3 2013,8,17,Sat,3,27.4 2013,8,18,Sun,3,63.5 2013,8,19,Mon,3,9 2013,8,20,Tue,1,91.3 2013,8,21,Wed,1,52.5 2013,8,22,Thu,3,92.6 2013,8,23,Fri,3,32.5 2013,8,24,Sat,3,35.6 2013,8,25,Sun,2,23.2 2013,8,26,Mon,1,81.6 2013,8,27,Tue,3,97.8 2013,8,28,Wed,2,36.5 2013,8,29,Thu,3,78.3 2013,8,30,Fri,3,58.7 2013,8,31,Sat,1,75.3 2013,9,1,Sun,1,33.1 2013,9,2,Mon,3,67.6 2013,9,3,Tue,2,90.2 2013,9,4,Wed,1,68.4 2013,9,5,Thu,3,67.7 2013,9,6,Fri,3,91.7 2013,9,7,Sat,1,86.3 2013,9,8,Sun,3,2.4 2013,9,9,Mon,1,47.1 2013,9,10,Tue,3,93.2 2013,9,11,Wed,2,87.7 2013,9,12,Thu,1,47.7 2013,9,13,Fri,3,99 2013,9,14,Sat,2,20.5 2013,9,15,Sun,2,16 2013,9,16,Mon,2,68.1 2013,9,17,Tue,3,48.1 2013,9,18,Wed,3,50.5 2013,9,19,Thu,1,58.2 2013,9,20,Fri,1,21.6 2013,9,21,Sat,2,77.1 2013,9,22,Sun,1,77.7 2013,9,23,Mon,2,29.4 2013,9,24,Tue,2,39.5 2013,9,25,Wed,1,96.1 2013,9,26,Thu,1,70.8 2013,9,27,Fri,2,89.6 2013,9,28,Sat,3,4.4 2013,9,29,Sun,2,88.6 2013,9,30,Mon,2,63.9 2013,10,1,Tue,1,51.1 2013,10,2,Wed,1,37.6 2013,10,3,Thu,3,36.4 2013,10,4,Fri,1,19.7 2013,10,5,Sat,2,98 2013,10,6,Sun,3,88.5 2013,10,7,Mon,2,59.3 2013,10,8,Tue,3,42.5 2013,10,9,Wed,2,87.5 2013,10,10,Thu,3,57.1 2013,10,11,Fri,3,22.2 2013,10,12,Sat,3,99 2013,10,13,Sun,1,76 2013,10,14,Mon,1,55.8 2013,10,15,Tue,2,84.9 2013,10,16,Wed,3,89.1 2013,10,17,Thu,3,29.6 2013,10,18,Fri,2,43.3 2013,10,19,Sat,2,9.7 2013,10,20,Sun,1,42.5 2013,10,21,Mon,3,1.7 2013,10,22,Tue,3,98.8 2013,10,23,Wed,3,55.4 2013,10,24,Thu,2,85.6 2013,10,25,Fri,1,7.8 2013,10,26,Sat,2,27.4 2013,10,27,Sun,2,40.9 2013,10,28,Mon,3,25.9 2013,10,29,Tue,1,13.2 2013,10,30,Wed,1,47.6 2013,10,31,Thu,2,4.9 2013,11,1,Fri,3,51.1 2013,11,2,Sat,2,18.5 2013,11,3,Sun,1,39.7 2013,11,4,Mon,1,48.5 2013,11,5,Tue,1,11.2 2013,11,6,Wed,1,68.9 2013,11,7,Thu,2,20.4 2013,11,8,Fri,2,71.3 2013,11,9,Sat,2,31.8 2013,11,10,Sun,1,55 2013,11,11,Mon,1,19.4 2013,11,12,Tue,2,83.6 2013,11,13,Wed,3,0.3 2013,11,14,Thu,2,85.8 2013,11,15,Fri,3,12.9 2013,11,16,Sat,2,21.9 2013,11,17,Sun,1,46.7 2013,11,18,Mon,1,11.4 2013,11,19,Tue,3,53.7 2013,11,20,Wed,2,82.8 2013,11,21,Thu,3,39.3 2013,11,22,Fri,2,88 2013,11,23,Sat,2,19.6 2013,11,24,Sun,3,5 2013,11,25,Mon,3,38.8 2013,11,26,Tue,1,24.6 2013,11,27,Wed,2,26.6 2013,11,28,Thu,3,84.7 2013,11,29,Fri,1,76.7 2013,11,30,Sat,1,29.8 2013,12,1,Sun,2,93.4 2013,12,2,Mon,2,51.2 2013,12,3,Tue,2,68.3 2013,12,4,Wed,1,99.6 2013,12,5,Thu,1,40.3 2013,12,6,Fri,1,37.8 2013,12,7,Sat,3,24.7 2013,12,8,Sun,1,23.9 2013,12,9,Mon,3,28.2 2013,12,10,Tue,3,77.2 2013,12,11,Wed,1,1.8 2013,12,12,Thu,3,64.5 2013,12,13,Fri,1,24.2 2013,12,14,Sat,1,34.1 2013,12,15,Sun,2,51.5 2013,12,16,Mon,2,77.5 2013,12,17,Tue,3,35.7 2013,12,18,Wed,3,85.8 2013,12,19,Thu,1,32.9 2013,12,20,Fri,1,22.3 2013,12,21,Sat,2,3.3 2013,12,22,Sun,3,75.4 2013,12,23,Mon,2,1.4 2013,12,24,Tue,2,29.2 2013,12,25,Wed,1,8.2 2013,12,26,Thu,2,28.3 2013,12,27,Fri,2,2.4 2013,12,28,Sat,2,78.6 2013,12,29,Sun,2,20.8 2013,12,30,Mon,1,59.9 2013,12,31,Tue,2,19.2 \ No newline at end of file diff --git a/test/fixtures/ss_02.ods b/test/fixtures/ss_02.ods new file mode 100644 index 0000000..e8e4354 Binary files /dev/null and b/test/fixtures/ss_02.ods differ diff --git a/test/fixtures/ss_02.xls b/test/fixtures/ss_02.xls new file mode 100644 index 0000000..eb62aba Binary files /dev/null and b/test/fixtures/ss_02.xls differ diff --git a/test/fixtures/ss_02.xlsx b/test/fixtures/ss_02.xlsx new file mode 100644 index 0000000..8ff6ffe Binary files /dev/null and b/test/fixtures/ss_02.xlsx differ diff --git a/test/fixtures/ss_04.xlsx b/test/fixtures/ss_04.xlsx new file mode 100644 index 0000000..3fcf961 Binary files /dev/null and b/test/fixtures/ss_04.xlsx differ diff --git a/test/test_row.rb b/test/test_row.rb new file mode 100644 index 0000000..7f68f56 --- /dev/null +++ b/test/test_row.rb @@ -0,0 +1,23 @@ +require 'test/unit' +require 'goodsheet' + +class TestRow < Test::Unit::TestCase + + def test_column_names + assert_raise RuntimeError do + Goodsheet::Row.column_names(6) + end + + Goodsheet::Row.column_names([:a, nil, :b, :c]) + assert_equal(Goodsheet::Row.keys, {0 => :a, 2 => :b, 3 => :c}) + + Goodsheet::Row.column_names(:a => 0, :b => 2, :c => 3) + assert_equal(Goodsheet::Row.keys, {0 => :a, 2 => :b, 3 => :c}) + + + Goodsheet::Row.column_names(0 => :a, 2 => :b, 3 => :c) + assert_equal(Goodsheet::Row.keys, {0 => :a, 2 => :b, 3 => :c}) + end + + +end diff --git a/test/test_spreadsheet_01.rb b/test/test_spreadsheet_01.rb new file mode 100644 index 0000000..fe27e0c --- /dev/null +++ b/test/test_spreadsheet_01.rb @@ -0,0 +1,150 @@ +require 'test/unit' +require 'goodsheet' + +class TestSpreadsheet_01 < Test::Unit::TestCase + + def setup + filepath = File.dirname(__FILE__) + "/fixtures/ss_01.xls" + @ss = Goodsheet::Spreadsheet.new(filepath) + end + + def test_sheets + assert_equal(%w(Sheet1 Sheet2 Sheet3 Sheet4), @ss.sheets) + end + + def test_failed_sheet_selection + assert_raise Goodsheet::SheetNotFoundError do + @ss.sheet(4) + end + assert_raise Goodsheet::SheetNotFoundError do + @ss.sheet("Sheet999") + end + end + + def test_sheet_selection_and_name + # by default the first sheet will be selected + assert_equal("Sheet1", @ss.name) + + @ss.sheet(0) + assert_equal("Sheet1", @ss.name) + + @ss.sheet(1) + assert_equal("Sheet2", @ss.name) + + @ss.sheet(2) + assert_equal("Sheet3", @ss.name) + + @ss.sheet(3) + assert_equal("Sheet4", @ss.name) + end + + def test_get_header_wo_options + @ss.sheet(0) + assert_equal(%w(A B C D), @ss.get_header) + end + + def test_get_header_w_options + @ss.sheet("Sheet3", :header_row => 1) + assert_equal(%w(K J), @ss.get_header) + end + + def test_rows + assert_equal(5, @ss.total_rows) + assert_equal(4, @ss.rows) + + @ss.sheet(0, :skip => 0) + assert_equal(5, @ss.total_rows) + assert_equal(5, @ss.rows) + + @ss.sheet(0, :skip => 1) + assert_equal(5, @ss.total_rows) + assert_equal(4, @ss.rows) + end + + def test_validate_no_errors + validation_errors = @ss.validate do + column_names :year => 0 + validates :year, :presence => true, :numericality => { :only_integer => false, :greater_than_or_equal_to => 2000, :less_than_or_equal_to => 2100 } + end + assert(validation_errors.empty?) + end + + + def test_validate_four_errors + @ss.sheet(1) + validation_errors = @ss.validate do + column_names 0 => :a1, 1 => :a2, 2 => :sum, 3 => :str + validates :a1, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :a2, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :sum, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :str, :presence => true, :inclusion => { :in => %w(A B D) } + end + assert_equal(4, validation_errors.size) + + + # limit the validation errors to 2 + validation_errors = @ss.validate(:max_errors => 2) do + column_names 0 => :a1, 1 => :a2, 2 => :sum, 3 => :str + validates :a1, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :a2, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :sum, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :str, :presence => true, :inclusion => { :in => %w(A B D) } + end + assert_equal(2, validation_errors.size) + + + # read only 3 rows + validation_errors = @ss.validate(:max_errors => 0, :row_limit => 3) do + column_names 0 => :a1, 1 => :a2, 2 => :sum, 3 => :str + validates :a1, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :a2, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :sum, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 6.0 } + validates :str, :presence => true, :inclusion => { :in => %w(A B D) } + end + assert_equal(3, validation_errors.size) + end + + + def test_read_sheet4 + @ss.sheet(3) + + result = @ss.read(:row_limit => 5) do + column_names 0 => :qty, 1 => :price, 2 => :tot + validates :qty, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0 } + validates :price, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0 } + validates :tot, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0 } + end + assert_equal(3, result.values.size) + result.values.each do |k, vv| + assert_equal(5, vv.size) + end + assert_equal([:qty, :price, :tot], result.values.keys) + + + + result = @ss.read(:force_nil => 0.0) do + column_names 0 => :qty, 1 => :price, 2 => :tot + validates :qty, :allow_nil => true, :numericality => { :greater_than_or_equal_to => 0.0 } + validates :price, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0 } + validates :tot, :allow_nil => true, :numericality => { :greater_than_or_equal_to => 0.0 } + end + assert_equal(3, result.values.size) + result.values.each do |k, vv| + assert_equal(6, vv.size) + end + + result = @ss.read(:force_nil => 0.0, :skip => 6) do + column_names 0 => :qty, 1 => :price, 2 => :tot + validates :qty, :allow_nil => true, :numericality => { :greater_than_or_equal_to => 0.0 } + validates :price, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0 } + validates :tot, :allow_nil => true, :numericality => { :greater_than_or_equal_to => 0.0 } + end + assert_equal(3, result.values.size) + result.values.each do |k, vv| + assert_equal(1, vv.size) + end + + + end + +end diff --git a/test/test_spreadsheet_02.rb b/test/test_spreadsheet_02.rb new file mode 100644 index 0000000..a3b6abd --- /dev/null +++ b/test/test_spreadsheet_02.rb @@ -0,0 +1,111 @@ +require 'test/unit' +require 'goodsheet' + +class TestSpreadsheet_02 < Test::Unit::TestCase + + def get_filepath(filename) + File.dirname(__FILE__) + "/fixtures/#{filename}" + end + + def test_xls_validation + validate(get_filepath("ss_02.xls")) + end + + def test_xls_reading + read(get_filepath("ss_02.xls")) + end + + def test_xlsx_validation + validate(get_filepath("ss_02.xlsx")) + end + + def test_xlsx_reading + read(get_filepath("ss_02.xlsx")) + end + + # in CSV files all numbers are converted to strings, so the validation will not pass... + # def test_csv_validation + # validate(get_filepath("ss_02.csv")) + # end + + # def test_csv_reading + # read(get_filepath("ss_02.csv")) + # end + + # parsing of '.ods' file is very slow for "large" files (a spredsheet with 366 lines take 65'' to be parsing on my computer...) + # def test_ods_validation + # validate(get_filepath("ss_02.ods")) + # end + + # parsing of '.ods' file is very slow for "large" files (a spredsheet with 366 lines take 65'' to be parsing on my computer...) + # def test_ods_reading + # read(get_filepath("ss_02.ods")) + # end + + # def test_google_ss_validation + # validate("0Ao3aUE9UFTaPdHBsYVhpU1FCaEVKMndkN1AzOVFYUUE") + # end + + # def test_google_ss_reading + # read("0Ao3aUE9UFTaPdHBsYVhpU1FCaEVKMndkN1AzOVFYUUE") + # end + + + def validate(filepath) + ss = Goodsheet::Spreadsheet.new(filepath) + ss.sheet(0) + errors = ss.validate(:skip => 1) do + column_names 0 => :year, 1 => :month, 2 => :day, 3 => :wday, 4 => :num, 5 => :v + validates :year, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 2000, :less_than_or_equal_to => 2020 } + validates :month, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 1, :less_than_or_equal_to => 12 } + validates :day, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 1, :less_than_or_equal_to => 31 } + validates :wday, inclusion: { in: %w(Mon Tue Wed Thu Fri Sat Sun) } + validates :num, inclusion: { in: [1, 2, 3] } + validates :v, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 100.0 } + end + + assert_equal(0, errors.size) + end + + def read(filepath) + ss = Goodsheet::Spreadsheet.new(filepath) + ss.sheet(0) + result = ss.read(:skip => 1) do + column_names 0 => :year, 1 => :month, 2 => :day, 3 => :wday, 4 => :num, 5 => :v + validates :year, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 2000, :less_than_or_equal_to => 2020 } + validates :month, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 1, :less_than_or_equal_to => 12 } + validates :day, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 1, :less_than_or_equal_to => 31 } + validates :wday, inclusion: { in: %w(Mon Tue Wed Thu Fri Sat Sun) } + validates :num, inclusion: { in: [1, 2, 3] } + validates :v, :allow_nil => false, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 100.0 } + end + + assert_equal(0, result.errors.size) + assert_equal(6, result.values.size) + result.values.each do |k, v| + assert_equal(365, v.size) + end + + result.values[:year].each do |y| + assert_equal(2013, y) # y is 2013.0 + end + result.values[:month].each do |v| + assert(v.between?(1,12)) + end + result.values[:day].each do |v| + assert(v.between?(1,31)) + end + wdays = %w(Mon Tue Wed Thu Fri Sat Sun) + result.values[:wday].each do |v| + assert wdays.include? v + end + result.values[:num].each do |v| + assert [1,2,3].include? v + end + result.values[:v].each do |v| + assert(v.between?(0, 100.0)) + end + end + + +end diff --git a/test/test_spreadsheet_03.rb b/test/test_spreadsheet_03.rb new file mode 100644 index 0000000..f487438 --- /dev/null +++ b/test/test_spreadsheet_03.rb @@ -0,0 +1,9 @@ +# test_spreadsheet_01.rb + +require 'test/unit' +require 'goodsheet' + +class TestSpreadsheet_03 < Test::Unit::TestCase + + +end \ No newline at end of file