eac_rails_utils 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 326ed69f1c2d044f1d4e78a7ffe7457e7a254c13
4
- data.tar.gz: d4cddf12d6717ad1fa54adde43c69290c73091ac
3
+ metadata.gz: d01772a418d7e7d21adb2fd75479b0701ea01a0e
4
+ data.tar.gz: 23ef345b02419a57baeee9b06b712bf8ffdbd050
5
5
  SHA512:
6
- metadata.gz: 3e8dbe272f8c609051fccad0a936b83e99f41e80db9b46fe963a932e3f80ba7b883a2718cb25b981d9b488642ee62ec770c7ce89d36bc8e1030217a314575001
7
- data.tar.gz: 470884c138fba21d17943de842ce79f43338bfac743e6b021f956c58aa42d23c26dc478a0e3ee6dbb4ad4165d7b675d52baf50e2a08f4b72265279a69403a916
6
+ metadata.gz: 07e9ecabd8e959715fece10e07f9412c61f6ab9d77aa80f3baffd95c6daf50f04e5b754de77621be3c49e5c7de28574f237681ca36360d78581ab9ebc307d2ed
7
+ data.tar.gz: 04e732ec09f40cac71514cb8e387ed2086588512e5d89b8b1f8c124b28468ef42c23d756072e861270e1b40ccc1c73651673108634087aa0b38bee72a4426b79
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eac
4
+ class DownloadFixtures
5
+ def initialize(prefix, download)
6
+ @prefix = prefix
7
+ @prefix = '' if @prefix.blank?
8
+ @download = download
9
+ end
10
+
11
+ def run
12
+ url_files.each do |f|
13
+ Rails.logger.info(relative_path(f))
14
+ download(url(f), target(f)) if @download
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def url_files
21
+ files = []
22
+ Dir["#{fixtures_root}/**/*.url"].map do |path|
23
+ files << path if match_pattern(path)
24
+ end
25
+ files
26
+ end
27
+
28
+ def match_pattern(path)
29
+ relative_path(path).start_with?(@prefix)
30
+ end
31
+
32
+ def fixtures_root
33
+ Rails.root.to_s
34
+ end
35
+
36
+ def download(url, target)
37
+ Rails.logger.info "Baixando \"#{url}\"..."
38
+ File.open(target, 'wb') { |file| file.write(Parsers::Base.new(url).content) }
39
+ end
40
+
41
+ def url(file)
42
+ File.read(file).strip
43
+ end
44
+
45
+ def target(file)
46
+ File.expand_path(File.basename(file, '.url') + '.source.html', File.dirname(file))
47
+ end
48
+
49
+ def relative_path(path)
50
+ path.sub(%r{^#{Regexp.quote(fixtures_root)}/}, '')
51
+ end
52
+ end
53
+ end
@@ -34,7 +34,7 @@ module Eac
34
34
  end
35
35
 
36
36
  def content_file
37
- open(@url, &:read)
37
+ open(@url.gsub(%r{\Afile://}, ''), &:read)
38
38
  end
39
39
 
40
40
  def content_get
@@ -5,23 +5,48 @@ module Eac
5
5
  module Parsers
6
6
  module FilesTest
7
7
  def test_data
8
- source_target_fixtures.source_target_files do |source_file, target_file|
9
- sd = parser_class.new(source_file).data
10
- td = YAML.load_file(target_file)
11
- assert_equal sort_results(td), sort_results(sd)
8
+ if ENV['EAC_PARSERS_FILES_TEST_WRITE'].present?
9
+ write_results
10
+ else
11
+ test_results
12
12
  end
13
13
  end
14
14
 
15
15
  protected
16
16
 
17
+ def test_results
18
+ sts = source_target_fixtures.source_target_files
19
+ assert_not_equal 0, sts.count, 'Source/target files count cannot be zero'
20
+ sts.each do |st|
21
+ assert_source_target_complete(st)
22
+ sd = parser_class.new(st.source).data
23
+ td = YAML.load_file(st.target)
24
+ assert_equal sort_results(td), sort_results(sd)
25
+ end
26
+ end
27
+
28
+ def write_results
29
+ source_target_fixtures.source_files.each do |source_file|
30
+ sd = sort_results(parser_class.new(source_file).data)
31
+ basename = ::Eac::SourceTargetFixtures.source_target_basename(source_file)
32
+ target_file = File.expand_path("../#{basename}.target.yaml", source_file)
33
+ File.write(target_file, sd.to_yaml)
34
+ end
35
+ end
36
+
17
37
  def sort_results(r)
18
38
  r
19
39
  end
20
40
 
21
41
  private
22
42
 
43
+ def assert_source_target_complete(st)
44
+ assert st.source, "Source not found (Target: #{st.target})"
45
+ assert st.target, "Target not found (Source: #{st.source})"
46
+ end
47
+
23
48
  def source_target_fixtures
24
- ::Eac::SourceTargetFixtures.new(
49
+ @source_target_fixtures ||= ::Eac::SourceTargetFixtures.new(
25
50
  File.expand_path("../#{self.class.name.demodulize.underscore}_files", test_file)
26
51
  )
27
52
  end
@@ -5,12 +5,12 @@ module Eac
5
5
  def ofx
6
6
  @ofx ||= ofx_parse
7
7
  end
8
-
8
+
9
9
  def assert_ofx
10
- fail "Not a OFX: #{url}" unless ofx.bank_account || ofx.credit_card ||
10
+ fail "Not a OFX: #{url}" unless ofx.bank_account || ofx.credit_card ||
11
11
  ofx.investment
12
12
  end
13
-
13
+
14
14
  def ofx?
15
15
  ofx.present?
16
16
  end
@@ -27,4 +27,4 @@ module Eac
27
27
  end
28
28
  end
29
29
  end
30
- end
30
+ end
@@ -4,17 +4,34 @@
4
4
  require 'yaml'
5
5
 
6
6
  module Eac
7
+ # Lists pairs of source/target files in a directory. See {Eac::Parsers::FilesTest} to see
8
+ # a use of this class.
7
9
  class SourceTargetFixtures
10
+ class << self
11
+ def source_target_basename(file)
12
+ m = /^(.+)\.(?:source|target)(?:\..+)?$/.match(File.basename(file))
13
+ m ? m[1] : nil
14
+ end
15
+ end
16
+
8
17
  attr_reader :fixtures_directory
9
18
 
10
19
  def initialize(fixtures_directory)
11
20
  @fixtures_directory = fixtures_directory
12
21
  end
13
22
 
14
- def source_target_files(&block)
15
- sources_targets_basenames.each do |test_basename|
16
- block.call(source_file(test_basename), target_file(test_basename))
23
+ def source_target_files
24
+ sources_targets_basenames.map do |basename|
25
+ OpenStruct.new(source: source_file(basename), target: target_file(basename))
26
+ end
27
+ end
28
+
29
+ def source_files
30
+ r = []
31
+ source_target_files.each do |st|
32
+ r << st.source if st.source
17
33
  end
34
+ r
18
35
  end
19
36
 
20
37
  private
@@ -33,18 +50,16 @@ module Eac
33
50
  next if item == '.' || item == '..'
34
51
  return File.expand_path(item, fixtures_directory) if item.starts_with?(prefix)
35
52
  end
36
- fail "\"#{prefix}\" not found (Basename: #{basename}, Suffix: #{suffix})"
53
+ nil
37
54
  end
38
55
 
39
56
  def sources_targets_basenames
40
57
  basenames = Set.new
41
58
  Dir.foreach(fixtures_directory) do |item|
42
59
  next if item == '.' || item == '..'
43
- if /^(.+)\.(?:source|target)(?:\..+)?$/ =~ File.basename(item)
44
- basenames << Regexp.last_match(1)
45
- end
60
+ b = self.class.source_target_basename(item)
61
+ basenames << b if b.present?
46
62
  end
47
- fail "\"#{fixtures_directory}\" não possui nenhum arquivo para teste." if basenames.empty?
48
63
  basenames
49
64
  end
50
65
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ module Eac
3
+ module TestUtils
4
+ # Add more helper methods to be used by all tests here...
5
+ def valid_invalid_column_values_test(record, column, valid_values, invalid_values)
6
+ valid_values.each do |v|
7
+ record.send("#{column}=", v)
8
+ assert record.valid?, "#{record.errors.messages}, #{column} = #{v.inspect} should be valid"
9
+ end
10
+ invalid_values.each do |v|
11
+ record.send("#{column}=", v)
12
+ assert_not record.valid?, "#{column} = #{v.inspect} should be invalid"
13
+ end
14
+ end
15
+
16
+ # Verifica falhas em campos específicos de um record
17
+ def assert_record_errors(record, fields_without_error, fields_with_error)
18
+ fields_without_error.each do |c|
19
+ assert record.errors[c].empty?, "Column: #{c} should not have errors (#{record.errors[c]})"
20
+ end
21
+ fields_with_error. each do |c|
22
+ assert_not record.errors[c].empty?, "Column: #{c} should have errors"
23
+ end
24
+ end
25
+
26
+ # Verifica, campo por campo, se invalida o registro.
27
+ def assert_column_changes(ppp, expected_valid_result, changes)
28
+ changes.each do |k, v|
29
+ ppp.send("#{k}=", v)
30
+ assert_equal expected_valid_result, ppp.valid?, "\"#{k}\" change should be " +
31
+ (expected_valid_result ? 'valid' : 'invalid')
32
+ assert_not ppp.errors[k].empty? unless expected_valid_result
33
+ ppp.restore_attributes
34
+ end
35
+ end
36
+
37
+ # Ex.: attrs = {a: 1, b: 2} resulta em
38
+ # [{a: nil, b: nil}, {a: 1, b: nil}, {a: nil, b: 2}, {a: 1, b: 2}].
39
+ def all_combinations(attrs)
40
+ combs = [{}]
41
+ attrs.each do |attr_name, value|
42
+ new_comb = []
43
+ assert_not value.nil?, "#{attr_name}=#{value}"
44
+ [nil, value].each do |vv|
45
+ combs.each do |c|
46
+ cc = c.dup
47
+ cc[attr_name] = vv
48
+ new_comb << cc
49
+ end
50
+ end
51
+ combs = new_comb
52
+ end
53
+ combs
54
+ end
55
+ end
56
+ end
@@ -21,6 +21,7 @@ module EacRailsUtils
21
21
  require 'eac/common_form_helper/form_builder/year_month_field'
22
22
  require 'eac/common_form_helper/form_builder'
23
23
  require 'eac/common_form_helper'
24
+ require 'eac/download_fixtures'
24
25
  require 'eac/htmlbeautifier'
25
26
  require 'eac/inequality_queries'
26
27
  require 'eac/listable'
@@ -36,6 +37,7 @@ module EacRailsUtils
36
37
  require 'eac/simple_cache'
37
38
  require 'eac/source_target_fixtures'
38
39
  require 'eac/tableless_model'
40
+ require 'eac/test_utils'
39
41
 
40
42
  ActionView::Base.send :include, Eac::CommonFormHelper
41
43
  ActionView::Base.send :include, Eac::FormatterHelper
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module EacRailsUtils
3
- VERSION = '0.1.11'
3
+ VERSION = '0.1.12'
4
4
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ namespace :eac do
3
+ desc 'Baixa o conteúdo remoto de fixtures'
4
+ task download_fixtures: :environment do
5
+ ::Eac::DownloadFixtures.new(ENV['PREFIX'], ENV['DOWNLOAD'].present?).run
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ require 'test_helper'
3
+
4
+ module Eac
5
+ module Parsers
6
+ class BaseStub
7
+ def initialize(source)
8
+ @source = source
9
+ end
10
+
11
+ def data
12
+ YAML.load(File.read(@source))
13
+ end
14
+ end
15
+
16
+ class Ok < BaseStub
17
+ end
18
+
19
+ class OkTest < ActiveSupport::TestCase
20
+ include ::Eac::Parsers::FilesTest
21
+
22
+ def test_file
23
+ __FILE__
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ require 'test_helper'
3
+
4
+ module Eac
5
+ class SourceTargetFixturesTest < ActionView::TestCase
6
+ def test_source_target_files
7
+ r = ::Eac::SourceTargetFixtures.new(fixtures_dir).source_target_files
8
+ assert_equal 3, r.count
9
+
10
+ a = r.find { |x| x.source && File.basename(x.source) == 'a.source.html' }
11
+ assert a
12
+ assert_equal File.join(fixtures_dir, 'a.source.html'), a.source
13
+ assert_equal File.join(fixtures_dir, 'a.target.yaml'), a.target
14
+
15
+ b = r.find { |x| x.source && File.basename(x.source) == 'b.source.html' }
16
+ assert b
17
+ assert_equal File.join(fixtures_dir, 'b.source.html'), b.source
18
+ assert_nil b.target
19
+
20
+ c = r.find { |x| x.target && File.basename(x.target) == 'c.target.yaml' }
21
+ assert c
22
+ assert_nil c.source
23
+ assert_equal File.join(fixtures_dir, 'c.target.yaml'), c.target
24
+ end
25
+
26
+ def test_source_files
27
+ r = ::Eac::SourceTargetFixtures.new(fixtures_dir).source_files
28
+ assert_equal 2, r.count
29
+
30
+ %w(a.source.html b.source.html).each do |expected|
31
+ assert r.include?(File.join(fixtures_dir, expected)), expected
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def fixtures_dir
38
+ File.expand_path('../source_target_fixtures_test_files', __FILE__)
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rails_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - E.A.C.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-08 00:00:00.000000000 Z
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -150,6 +150,7 @@ files:
150
150
  - lib/eac/common_form_helper/form_builder/select_field.rb
151
151
  - lib/eac/common_form_helper/form_builder/year_month_field.rb
152
152
  - lib/eac/cpf_validator.rb
153
+ - lib/eac/download_fixtures.rb
153
154
  - lib/eac/formatter_helper.rb
154
155
  - lib/eac/htmlbeautifier.rb
155
156
  - lib/eac/inequality_queries.rb
@@ -166,11 +167,13 @@ files:
166
167
  - lib/eac/simple_cache.rb
167
168
  - lib/eac/source_target_fixtures.rb
168
169
  - lib/eac/tableless_model.rb
170
+ - lib/eac/test_utils.rb
169
171
  - lib/eac_rails_utils.rb
170
172
  - lib/eac_rails_utils/patches/model_attribute_required.rb
171
173
  - lib/eac_rails_utils/patches/ofx_parser.rb
172
174
  - lib/eac_rails_utils/rails/engine.rb
173
175
  - lib/eac_rails_utils/version.rb
176
+ - lib/tasks/eac_rails_utils.rake
174
177
  - test/dummy/Rakefile
175
178
  - test/dummy/app/models/job.rb
176
179
  - test/dummy/app/models/user.rb
@@ -192,7 +195,17 @@ files:
192
195
  - test/lib/eac/formatter_helper_test.rb
193
196
  - test/lib/eac/listable_test.rb
194
197
  - test/lib/eac/model_test.rb
198
+ - test/lib/eac/parsers/files_test_test.rb
199
+ - test/lib/eac/parsers/ok_test_files/a.source.yaml
200
+ - test/lib/eac/parsers/ok_test_files/a.target.yaml
201
+ - test/lib/eac/parsers/ok_test_files/b.source.yaml
202
+ - test/lib/eac/parsers/ok_test_files/b.target.yaml
195
203
  - test/lib/eac/simple_cache_test.rb
204
+ - test/lib/eac/source_target_fixtures_test.rb
205
+ - test/lib/eac/source_target_fixtures_test_files/a.source.html
206
+ - test/lib/eac/source_target_fixtures_test_files/a.target.yaml
207
+ - test/lib/eac/source_target_fixtures_test_files/b.source.html
208
+ - test/lib/eac/source_target_fixtures_test_files/c.target.yaml
196
209
  - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
197
210
  - test/test_helper.rb
198
211
  homepage:
@@ -214,32 +227,42 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
227
  version: '0'
215
228
  requirements: []
216
229
  rubyforge_project:
217
- rubygems_version: 2.6.12
230
+ rubygems_version: 2.4.8
218
231
  signing_key:
219
232
  specification_version: 4
220
233
  summary: Código reutilizável para as aplicações Rails da E.A.C..
221
234
  test_files:
222
- - test/dummy/Rakefile
223
- - test/dummy/config.ru
224
- - test/dummy/config/boot.rb
225
- - test/dummy/config/database.yml
226
- - test/dummy/config/secrets.yml
227
- - test/dummy/config/locales/pt-BR.yml
228
- - test/dummy/config/application.rb
229
- - test/dummy/config/environments/test.rb
230
- - test/dummy/config/environment.rb
231
- - test/dummy/config/routes.rb
232
- - test/dummy/db/schema.rb
233
- - test/dummy/db/migrate/20160415143123_create_jobs.rb
234
- - test/dummy/db/migrate/20160415143229_add_job_to_users.rb
235
- - test/dummy/db/migrate/20160415125333_create_users.rb
236
- - test/dummy/app/models/job.rb
237
- - test/dummy/app/models/user.rb
238
- - test/test_helper.rb
239
235
  - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
236
+ - test/lib/eac/formatter_helper_test.rb
240
237
  - test/lib/eac/common_form_helper_test.rb
238
+ - test/lib/eac/source_target_fixtures_test.rb
241
239
  - test/lib/eac/cpf_validator_test.rb
240
+ - test/lib/eac/model_test.rb
241
+ - test/lib/eac/source_target_fixtures_test_files/a.source.html
242
+ - test/lib/eac/source_target_fixtures_test_files/c.target.yaml
243
+ - test/lib/eac/source_target_fixtures_test_files/a.target.yaml
244
+ - test/lib/eac/source_target_fixtures_test_files/b.source.html
242
245
  - test/lib/eac/simple_cache_test.rb
243
- - test/lib/eac/formatter_helper_test.rb
244
246
  - test/lib/eac/listable_test.rb
245
- - test/lib/eac/model_test.rb
247
+ - test/lib/eac/parsers/ok_test_files/a.source.yaml
248
+ - test/lib/eac/parsers/ok_test_files/a.target.yaml
249
+ - test/lib/eac/parsers/ok_test_files/b.target.yaml
250
+ - test/lib/eac/parsers/ok_test_files/b.source.yaml
251
+ - test/lib/eac/parsers/files_test_test.rb
252
+ - test/dummy/config.ru
253
+ - test/dummy/db/migrate/20160415143229_add_job_to_users.rb
254
+ - test/dummy/db/migrate/20160415143123_create_jobs.rb
255
+ - test/dummy/db/migrate/20160415125333_create_users.rb
256
+ - test/dummy/db/schema.rb
257
+ - test/dummy/app/models/user.rb
258
+ - test/dummy/app/models/job.rb
259
+ - test/dummy/Rakefile
260
+ - test/dummy/config/environments/test.rb
261
+ - test/dummy/config/application.rb
262
+ - test/dummy/config/boot.rb
263
+ - test/dummy/config/locales/pt-BR.yml
264
+ - test/dummy/config/secrets.yml
265
+ - test/dummy/config/database.yml
266
+ - test/dummy/config/routes.rb
267
+ - test/dummy/config/environment.rb
268
+ - test/test_helper.rb