sunat_books 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../helper"
4
+ require_relative "../../lib/books/pages_utils"
5
+
6
+ include PagesUtils
7
+
8
+ setup do
9
+ @company = Company.new(ruc: Faker::Number.number(11), name: Faker::Name.name)
10
+ end
11
+
12
+ test "render pdf, have a parseable pdf" do
13
+ tickets = []
14
+ view = Object.new.tap { |o| o.extend(Prawn::View) }
15
+ pdf = Books::Buys.new(@company, tickets, view, 2, 3015)
16
+ page_counter = PDF::Inspector::Page.analyze(pdf.render)
17
+ assert pdf.page_count == 1
18
+ assert page_counter.pages.size == 1
19
+ end
20
+
21
+ test "have correct text in header" do
22
+ tickets = []
23
+ view = Object.new.tap { |o| o.extend(Prawn::View) }
24
+ pdf = Books::Buys.new(@company, tickets, view, 2, 3015)
25
+ reader = PDF::Reader.new(StringIO.new(pdf.render))
26
+ assert reader.pages.first.text.include?("REGISTRO DE COMPRAS")
27
+ end
28
+
29
+ test "#page_not_full return a page" do
30
+ pages = []
31
+ setup_pages(pages, 20, 5)
32
+ first = pages.at(1)
33
+ page = page_not_full(first, pages, 20)
34
+ assert_equal page.class, Books::Page
35
+ end
36
+
37
+ test "#page_not_full return last page when length is less than page_max" do
38
+ pages = []
39
+ setup_pages(pages, 20, 5)
40
+ first_page = pages.at(1)
41
+ page = page_not_full(first_page, pages, 5)
42
+ assert_equal page, first_page
43
+ end
44
+
45
+ test "#page_not_full return new page when last page is full" do
46
+ pages = []
47
+ setup_pages(pages, 20, 5)
48
+ first_page = pages.at(1)
49
+ first_page.length += 5
50
+ current_page = pages.at(2)
51
+ page = page_not_full(first_page, pages, 5)
52
+ assert_equal page, current_page
53
+ assert_equal page.page_number, 2
54
+ end
55
+
56
+ test "#row_data prepare data that will be include in table's rows" do
57
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../helper"
4
+ require_relative "../../lib/books/page"
5
+
6
+ setup do
7
+ @page = Books::Page.new(1, 1)
8
+ end
9
+
10
+ test "#update_data" do
11
+ end
12
+
13
+ test "#update_fields should return nil if not source is provided" do
14
+ assert_equal @page.update_fields([]), nil
15
+ end
16
+
17
+ test "#update_fields should handle if undefined method" do
18
+ other_page = Books::Page.new(2, 1)
19
+ assert_equal @page.update_fields([:foo], other_page), nil
20
+
21
+ other_page.bi_sum += BigDecimal(10)
22
+ @page.update_fields([:bi_sum], other_page)
23
+ assert_equal @page.bi_sum, other_page.bi_sum
24
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../helper"
4
+ require_relative "../../lib/books/pages_utils"
5
+ require_relative "../../lib/books/page"
6
+
7
+ include PagesUtils
8
+
9
+ test "#setup_pages generate new pages to fill all elements" do
10
+ pages = []
11
+ setup_pages(pages, 20, 10)
12
+ # since array index is used to match page position for buys, and is same as
13
+ # page_number, we have an extra index
14
+ assert_equal pages.count, 3
15
+
16
+ page = pages.last
17
+ assert_equal page.page_number, 2
18
+ assert_equal page.bi_sum, BigDecimal(0)
19
+
20
+ other = []
21
+ setup_pages(other, 29, 27)
22
+ assert_equal other.count, 3
23
+ end
24
+
25
+ test "#setup_pages generate new pages without extra page" do
26
+ pages = []
27
+ setup_pages(pages, 20, 10, 0)
28
+ assert_equal pages.count, 2
29
+ end
30
+
31
+ test "#setup_new_page copy page sums into current page" do
32
+ pages = []
33
+ setup_pages(pages, 52, 27)
34
+ page = pages[1]
35
+ page.bi_sum += BigDecimal(20)
36
+ new_page = setup_new_page(pages, page, 1)
37
+ assert_equal new_page.bi_sum, page.bi_sum
38
+ end
39
+
40
+ test "#page_index should get a page index according index and page_max" do
41
+ assert_equal page_index(20, 20), 1
42
+ assert_equal page_index(30, 20), 2
43
+ assert_equal page_index(20, 7), 3
44
+ assert_equal page_index(0, 7), 1
45
+ end
46
+
47
+ test "#split_data, separates data in groups according max_column desired" do
48
+ row = []
49
+ (1..29).map { row << "foo" }
50
+ pages = split_data([row], 20)
51
+ assert_equal pages.class, Array
52
+ assert_equal pages.count, 2
53
+ assert_equal pages[0].data.first.count, 19
54
+ assert_equal pages[1].data.first.count, 11
55
+ end
56
+
57
+ test "#split_data, split data for more than one array" do
58
+ data = []
59
+ (1..3).map do
60
+ row = []
61
+ (1..29).map { row << "foo" }
62
+ data << row
63
+ end
64
+ pages = split_data(data, 20)
65
+ assert_equal pages.count, 2
66
+ assert_equal pages[0].data.last.count, 19
67
+ assert_equal pages[1].data.last.count, 11
68
+ assert_equal pages[0].data.first.count, 20
69
+ assert_equal pages[1].data.first.count, 11
70
+ end
71
+
72
+ test "#split_data, split in more than 2 arrays" do
73
+ row = []
74
+ (1..49).map { row << "foo" }
75
+ pages = split_data([row], 20)
76
+ assert_equal pages.count, 3
77
+ assert pages[2].data.count.positive?
78
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../helper"
4
+ require_relative "../../lib/books/utils"
5
+
6
+ include Utils
7
+
8
+ setup do
9
+ @widths = [{ xyz: [20, 23], abc: [41] }]
10
+ @column_widths = get_column_widths(@widths, :xyz)
11
+ end
12
+
13
+ test "#get_column_widths return an empty obj when dont match key" do
14
+ column_widths = get_column_widths(@widths, :xyc)
15
+ assert column_widths.is_a?(Hash)
16
+ end
17
+
18
+ test "#get_column_widths return an array with widths when match key" do
19
+ assert @column_widths.is_a?(Array)
20
+ end
21
+
22
+ test "#add_widths set column width in options" do
23
+ opt = { cell_style: {} }
24
+ add_widths(@column_widths, opt, 15)
25
+ assert opt[:cell_style].empty?
26
+ assert opt[:column_widths]
27
+ end
28
+
29
+ test "#add_widths set cell width in options" do
30
+ opt = { cell_style: {} }
31
+ add_widths({}, opt, 15)
32
+ assert opt[:cell_style][:width] == 15
33
+ end
34
+
35
+ test "#sum_count" do
36
+ end
37
+
38
+ test "#order_data_row" do
39
+ end
40
+
41
+ test "#get_row_sums" do
42
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../helper"
4
+ require_relative "../../lib/csv_books/option_error"
5
+
6
+ setup do
7
+ @tickets = []
8
+ 5.times do
9
+ @tickets << Ticket.new(field: SecureRandom.hex(10))
10
+ end
11
+ @base = CsvBooks::Base.new(@tickets, layout: ["field"])
12
+ end
13
+
14
+ test "require a layout array" do
15
+ assert_raise(CsvBooks::OptionError) { CsvBooks::Base.new(@tickets) }
16
+ end
17
+
18
+ test "should generate csv" do
19
+ assert File.exist?(@base.file)
20
+ end
21
+
22
+ test "should allow set custom filename" do
23
+ custom_file = CsvBooks::Base.new(@tickets, layout: [], filename: "name.csv")
24
+ assert custom_file.file.include?("name.csv")
25
+ assert File.exist?(custom_file.file)
26
+ end
27
+
28
+ test "should handle undefined layout method" do
29
+ undefined_method = CsvBooks::Base.new(@tickets, layout: %w[field bar])
30
+ assert File.exist?(undefined_method.file)
31
+ end
32
+
33
+ test "csv should have the correct row's count" do
34
+ csv = CSV.open(@base.file, "r+")
35
+ csv_rows = csv.readlines
36
+ assert_equal csv_rows.count, 6
37
+ end
38
+
39
+ test "csv should contain headers in first row" do
40
+ csv = CSV.open(@base.file, "r+")
41
+ csv_rows = csv.readlines
42
+ assert_equal csv_rows.first, ["field"]
43
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Base
4
+ def initialize(hash)
5
+ hash.each do |key, value|
6
+ define_singleton_method key.to_s do
7
+ value
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ class Company < Base
5
+ end
@@ -1,11 +1,6 @@
1
- require 'pry'
1
+ # frozen_string_literal: true
2
2
 
3
- class Ticket
4
- def initialize hash
5
- hash.each do |key, value|
6
- define_singleton_method "#{key}" do
7
- value
8
- end
9
- end
10
- end
3
+ require_relative "base"
4
+
5
+ class Ticket < Base
11
6
  end
data/test/helper.rb CHANGED
@@ -1,24 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "cutest"
2
4
  require "pry"
5
+ require "faker"
3
6
  require_relative "../lib/sunat_books"
4
7
  require_relative "fixtures/ticket"
8
+ require_relative "fixtures/company"
9
+
10
+ Prawn.debug = true
5
11
 
6
- def get_line array, object
12
+ require "pdf/inspector"
13
+
14
+ def get_line(array, object)
7
15
  str = ""
8
- array.each {|f| str += object.send("#{f}") + "|"}
16
+ array.each { |f| str += object.send(f.to_s) + "|" }
9
17
  str
10
18
  end
11
19
 
12
- def random_string array
20
+ def random_string(array)
13
21
  array.slice(SecureRandom.random_number(array.count - 1))
14
22
  end
15
-
16
- # to search
17
- # Array
18
- # tap
19
- # chunk
20
- # transpose
21
- # trust
22
- # untaint
23
- # Hash
24
- # dup
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../helper"
4
+
5
+ setup do
6
+ tickets = [{}]
7
+ ruc = "102392839213"
8
+ @ple_buys = PleBooks::Buys.new(ruc, tickets, 10, 2013)
9
+ end
10
+
11
+ test "generate txt file" do
12
+ assert File.exist?(@ple_buys.file)
13
+ end
14
+
15
+ test "tickets empty" do
16
+ ple_buys = PleBooks::Buys.new("10293827481", {}, 10, 2011)
17
+ assert File.exist?(ple_buys.file)
18
+ end
19
+
20
+ scope "custom layout" do
21
+ test "allow custom file for layout" do
22
+ dir = File.dirname(__FILE__)
23
+ yml = "#{dir}/../fixtures/custom_layout.yml"
24
+ tickets = []
25
+ field_value = SecureRandom.hex(10)
26
+ tickets << Ticket.new(custom_field: field_value)
27
+ ple_buys = PleBooks::Buys.new("10293827481", tickets, 10, 2011,
28
+ yml: yml)
29
+ file = ple_buys.file
30
+ assert File.exist?(file)
31
+
32
+ txt = File.read(file)
33
+ assert txt.include?(field_value)
34
+ end
35
+
36
+ test "allow change individual field" do
37
+ tickets = []
38
+ tickets << Ticket.new(period: "20151000", operation_day: "20/10/2015")
39
+ ple_buys = PleBooks::Buys.new("10293827481", tickets, 10, 2015,
40
+ layout: {
41
+ operation_date: "operation_day"
42
+ })
43
+ file = ple_buys.file
44
+ assert File.exist?(file)
45
+
46
+ txt = File.read(file)
47
+ assert txt.include?("20/10/2015")
48
+ end
49
+ end
@@ -1,96 +1,47 @@
1
- require_relative 'helper'
1
+ # frozen_string_literal: true
2
2
 
3
- scope do
4
- setup do
5
- @base = PleBooks::Base.new
6
- end
7
-
8
- test 'book_code' do
9
- assert @base.book_code('8.1') == '080100'
10
- assert @base.book_code('3.1') == '030100'
11
- end
12
-
13
- test 'ple_book_name' do
14
- name = @base.ple_book_name('8.1', '10201902912', 2015, 11)
15
- assert name.length == 33
3
+ require_relative "helper"
16
4
 
17
- name = @base.ple_book_name('8.1', '10201902912', 2015, 11, nil, 0)
18
- assert name.length == 33
19
- assert name[30] == '0'
5
+ setup do
6
+ @base = PleBooks::Base.new
7
+ end
20
8
 
21
- name = @base.ple_book_name('8.1', '10201902912', 2015, 11, nil, nil, 2)
22
- assert name.length == 33
23
- assert name[31] == '2'
24
- end
9
+ test "book_code" do
10
+ assert @base.book_code("8.1") == "080100"
11
+ assert @base.book_code("3.1") == "030100"
12
+ end
25
13
 
26
- test 'get_file' do
27
- fields = []
28
- 3.times { fields << random_string(String.public_methods) }
14
+ test "ple_book_name" do
15
+ name = @base.ple_book_name("8.1", "10201902912", 2015, 11)
16
+ assert name.length == 33
29
17
 
30
- tickets = []
31
- 3.times do
32
- hash = {}
33
- fields.each { |field| hash.merge!("#{field}": SecureRandom.hex(2)) }
34
- tickets << Ticket.new(hash)
35
- end
18
+ name = @base.ple_book_name("8.1", "10201902912", 2015, 11, nil, 0)
19
+ assert name.length == 33
20
+ assert name[30] == "0"
36
21
 
37
- filename = "#{@base.path}/some_file.txt"
38
- @base.get_file(tickets, fields, filename)
22
+ name = @base.ple_book_name("8.1", "10201902912", 2015, 11, nil, nil, 2)
23
+ assert name.length == 33
24
+ assert name[31] == "2"
25
+ end
39
26
 
40
- assert File.exists?(filename)
27
+ test "get_file" do
28
+ fields = []
29
+ 3.times { fields << random_string(String.public_methods) }
41
30
 
42
- file_str = File.read(filename)
43
- assert file_str.count("\n") == 3
44
- assert file_str.split("\n").first == get_line(fields, tickets.first)
45
- assert file_str.split("\n").last == get_line(fields, tickets.last)
31
+ tickets = []
32
+ 3.times do
33
+ hash = {}
34
+ fields.each { |field| hash.merge!("#{field}": SecureRandom.hex(2)) }
35
+ tickets << Ticket.new(hash)
46
36
  end
47
37
 
48
- scope 'buys' do
49
-
50
- setup do
51
- tickets = [{}]
52
- ruc = '102392839213'
53
- @ple_buys = PleBooks::Buys.new(ruc, tickets, 10, 2013)
54
- end
55
-
56
- test 'generate txt file' do
57
- assert File.exists?(@ple_buys.file)
58
- end
59
-
60
- test 'tickets empty' do
61
- ple_buys = PleBooks::Buys.new('10293827481', {}, 10, 2011)
62
- assert File.exists?(ple_buys.file)
63
- end
38
+ filename = "#{@base.path}/some_file.txt"
39
+ @base.get_file(tickets, fields, filename)
64
40
 
65
- scope 'custom layout' do
66
- test 'allow custom file for layout' do
67
- dir = File.dirname(__FILE__)
68
- yml = "#{dir}/fixtures/custom_layout.yml"
69
- tickets = []
70
- field_value = SecureRandom.hex(10)
71
- tickets << Ticket.new(custom_field: field_value)
72
- ple_buys = PleBooks::Buys.new('10293827481', tickets, 10, 2011,
73
- {yml: yml})
74
- file = ple_buys.file
75
- assert File.exists?(file)
41
+ assert File.exist?(filename)
76
42
 
77
- txt = File.read(file)
78
- assert txt.include?(field_value)
79
- end
80
-
81
- test 'allow change individual field' do
82
- tickets = []
83
- tickets << Ticket.new(period: '20151000', operation_day: '20/10/2015')
84
- ple_buys = PleBooks::Buys.new('10293827481', tickets, 10, 2015,
85
- { layout: {
86
- operation_date: 'operation_day'
87
- }})
88
- file = ple_buys.file
89
- assert File.exists?(file)
90
-
91
- txt = File.read(file)
92
- assert txt.include?('20/10/2015')
93
- end
94
- end
95
- end
43
+ file_str = File.read(filename)
44
+ assert file_str.count("\n") == 3
45
+ assert file_str.split("\n").first == get_line(fields, tickets.first)
46
+ assert file_str.split("\n").last == get_line(fields, tickets.last)
96
47
  end