flat_kit 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +1 -2
  3. data/HISTORY.md +9 -0
  4. data/Manifest.txt +3 -42
  5. data/{bin → exe}/fk +2 -1
  6. data/flat_kit.gemspec +33 -0
  7. data/lib/flat_kit/cli.rb +46 -32
  8. data/lib/flat_kit/command/cat.rb +34 -32
  9. data/lib/flat_kit/command/merge.rb +37 -36
  10. data/lib/flat_kit/command/sort.rb +37 -37
  11. data/lib/flat_kit/command/stats.rb +41 -39
  12. data/lib/flat_kit/command.rb +10 -11
  13. data/lib/flat_kit/descendant_tracker.rb +9 -6
  14. data/lib/flat_kit/error.rb +4 -0
  15. data/lib/flat_kit/event_emitter.rb +5 -2
  16. data/lib/flat_kit/field_stats.rb +31 -26
  17. data/lib/flat_kit/field_type/boolean_type.rb +9 -5
  18. data/lib/flat_kit/field_type/date_type.rb +19 -17
  19. data/lib/flat_kit/field_type/float_type.rb +15 -9
  20. data/lib/flat_kit/field_type/guess_type.rb +9 -6
  21. data/lib/flat_kit/field_type/integer_type.rb +6 -4
  22. data/lib/flat_kit/field_type/null_type.rb +5 -1
  23. data/lib/flat_kit/field_type/string_type.rb +8 -6
  24. data/lib/flat_kit/field_type/timestamp_type.rb +11 -10
  25. data/lib/flat_kit/field_type/unknown_type.rb +12 -8
  26. data/lib/flat_kit/field_type.rb +52 -44
  27. data/lib/flat_kit/format.rb +11 -5
  28. data/lib/flat_kit/input/file.rb +11 -9
  29. data/lib/flat_kit/input/io.rb +18 -21
  30. data/lib/flat_kit/input.rb +8 -7
  31. data/lib/flat_kit/internal_node.rb +22 -19
  32. data/lib/flat_kit/jsonl/format.rb +6 -2
  33. data/lib/flat_kit/jsonl/reader.rb +7 -4
  34. data/lib/flat_kit/jsonl/record.rb +15 -18
  35. data/lib/flat_kit/jsonl/writer.rb +8 -10
  36. data/lib/flat_kit/jsonl.rb +8 -4
  37. data/lib/flat_kit/leaf_node.rb +6 -5
  38. data/lib/flat_kit/log_formatter.rb +20 -0
  39. data/lib/flat_kit/logger.rb +12 -19
  40. data/lib/flat_kit/merge.rb +21 -18
  41. data/lib/flat_kit/merge_tree.rb +5 -6
  42. data/lib/flat_kit/output/file.rb +13 -9
  43. data/lib/flat_kit/output/io.rb +40 -35
  44. data/lib/flat_kit/output.rb +8 -7
  45. data/lib/flat_kit/position.rb +3 -4
  46. data/lib/flat_kit/reader.rb +8 -8
  47. data/lib/flat_kit/record.rb +12 -12
  48. data/lib/flat_kit/sentinel_internal_node.rb +6 -5
  49. data/lib/flat_kit/sentinel_leaf_node.rb +4 -1
  50. data/lib/flat_kit/sort.rb +8 -9
  51. data/lib/flat_kit/stat_type/nominal_stats.rb +13 -7
  52. data/lib/flat_kit/stat_type/numerical_stats.rb +18 -18
  53. data/lib/flat_kit/stat_type/ordinal_stats.rb +8 -13
  54. data/lib/flat_kit/stat_type.rb +18 -13
  55. data/lib/flat_kit/stats.rb +12 -14
  56. data/lib/flat_kit/writer.rb +5 -6
  57. data/lib/flat_kit/xsv/format.rb +6 -2
  58. data/lib/flat_kit/xsv/reader.rb +8 -6
  59. data/lib/flat_kit/xsv/record.rb +21 -15
  60. data/lib/flat_kit/xsv/writer.rb +13 -10
  61. data/lib/flat_kit/xsv.rb +7 -4
  62. data/lib/flat_kit.rb +31 -26
  63. metadata +20 -158
  64. data/Rakefile +0 -21
  65. data/examples/stream-active-record-to-csv.rb +0 -42
  66. data/tasks/default.rake +0 -242
  67. data/tasks/extension.rake +0 -38
  68. data/tasks/man.rake +0 -7
  69. data/tasks/this.rb +0 -208
  70. data/test/device_dataset.rb +0 -117
  71. data/test/field_type/test_boolean_type.rb +0 -65
  72. data/test/field_type/test_date_type.rb +0 -71
  73. data/test/field_type/test_float_type.rb +0 -56
  74. data/test/field_type/test_guess_type.rb +0 -14
  75. data/test/field_type/test_integer_type.rb +0 -52
  76. data/test/field_type/test_null_type.rb +0 -41
  77. data/test/field_type/test_string_type.rb +0 -18
  78. data/test/field_type/test_timestamp_type.rb +0 -108
  79. data/test/field_type/test_unknown_type.rb +0 -35
  80. data/test/input/test_file.rb +0 -73
  81. data/test/input/test_io.rb +0 -93
  82. data/test/jsonl/test_format.rb +0 -22
  83. data/test/jsonl/test_reader.rb +0 -49
  84. data/test/jsonl/test_record.rb +0 -61
  85. data/test/jsonl/test_writer.rb +0 -86
  86. data/test/output/test_file.rb +0 -60
  87. data/test/output/test_io.rb +0 -104
  88. data/test/run +0 -23
  89. data/test/stat_type/test_nominal_stats.rb +0 -69
  90. data/test/stat_type/test_numerical_stats.rb +0 -118
  91. data/test/stat_type/test_ordinal_stats.rb +0 -92
  92. data/test/test_conversions.rb +0 -45
  93. data/test/test_event_emitter.rb +0 -89
  94. data/test/test_field_stats.rb +0 -134
  95. data/test/test_field_type.rb +0 -34
  96. data/test/test_format.rb +0 -24
  97. data/test/test_helper.rb +0 -26
  98. data/test/test_merge.rb +0 -40
  99. data/test/test_merge_tree.rb +0 -64
  100. data/test/test_version.rb +0 -11
  101. data/test/xsv/test_format.rb +0 -22
  102. data/test/xsv/test_reader.rb +0 -61
  103. data/test/xsv/test_record.rb +0 -69
  104. data/test/xsv/test_writer.rb +0 -89
@@ -1,108 +0,0 @@
1
- require_relative '../test_helper'
2
- require 'set'
3
-
4
- module TestFieldType
5
- class TestTimestampType < ::Minitest::Test
6
- def test_time
7
- assert(FlatKit::FieldType::TimestampType.matches?(Time.now))
8
- end
9
-
10
- def test_date
11
- refute(FlatKit::FieldType::TimestampType.matches?(Date.today))
12
- end
13
-
14
- def test_date_time
15
- refute(FlatKit::FieldType::TimestampType.matches?(DateTime.now))
16
- end
17
-
18
- def test_builtin_formats
19
- stock_formats = [
20
- Time.now.httpdate,
21
- Time.now.utc.httpdate,
22
- Time.now.iso8601,
23
- Time.now.utc.iso8601,
24
- Time.now.rfc2822,
25
- Time.now.utc.rfc2822,
26
- ]
27
-
28
- stock_formats.each do |t|
29
- assert(FlatKit::FieldType::TimestampType.matches?(t), "#{t} should match timestamp")
30
- assert_instance_of(Time, FlatKit::FieldType::TimestampType.coerce(t), "#{t} should convert to timestamp")
31
- end
32
- end
33
-
34
- def test_no_duplicate_formats
35
- parse_formats = ::FlatKit::FieldType::TimestampType.parse_formats
36
-
37
- assert_equal(parse_formats.size, parse_formats.sort.uniq.size)
38
- end
39
-
40
- def test_parse_formats
41
- parse_formats = ::FlatKit::FieldType::TimestampType.parse_formats.dup
42
- extra_formats = [
43
- "%Y-%m-%dT%H:%M:%S.%N%z",
44
- "%Y-%m-%d %H:%M:%S.%NZ",
45
- "%Y-%m-%d %H:%M:%S.%N",
46
- "%Y-%m-%dT%H:%M:%S.%3N%z",
47
- "%Y-%m-%d %H:%M:%S.%3NZ",
48
- "%Y-%m-%d %H:%M:%S.%3N",
49
- "%Y-%m-%dT%H:%M:%S.%6N%z",
50
- "%Y-%m-%d %H:%M:%S.%6NZ",
51
- "%Y-%m-%d %H:%M:%S.%6N",
52
- "%Y-%m-%dT%H:%M:%S.%9N%z",
53
- "%Y-%m-%d %H:%M:%S.%9NZ",
54
- "%Y-%m-%d %H:%M:%S.%9N",
55
- ]
56
- parse_formats.concat(extra_formats)
57
-
58
- parse_formats.each do |format|
59
- now = Time.now
60
- str = now.strftime(format)
61
-
62
- assert(FlatKit::FieldType::TimestampType.matches?(str), "#{str} should match timestamp")
63
- coerced = FlatKit::FieldType::TimestampType.coerce(str)
64
-
65
- assert_instance_of(Time, coerced)
66
- end
67
- end
68
-
69
- def test_other_class_does_not_match
70
- [ 42, Object.new, true, false ].each do |x|
71
- refute(FlatKit::FieldType::TimestampType.matches?(x), "#{x} should not be date")
72
- end
73
- end
74
-
75
- def test_N_number_does_not_match
76
- x = "N89362"
77
- refute(FlatKit::FieldType::TimestampType.matches?(x), "#{x} should not be date")
78
- end
79
-
80
- def test_coerce_passthrough_time
81
- t = Time.now
82
- assert_equal(t, FlatKit::FieldType::TimestampType.coerce(t))
83
- end
84
-
85
- def test_coerce_do_not_passthrough_date
86
- t = Date.today
87
- assert_equal(::FlatKit::FieldType::CoerceFailure, FlatKit::FieldType::TimestampType.coerce(t))
88
- end
89
-
90
- def test_date_coerce_passthrough_datetime
91
- t = Time.now
92
- assert_equal(t, FlatKit::FieldType::TimestampType.coerce(t))
93
- end
94
-
95
- def test_date_only_does_not_parse
96
- t = Time.now.strftime("%Y-%m-%d")
97
- assert_equal(::FlatKit::FieldType::CoerceFailure, FlatKit::FieldType::TimestampType.coerce(t))
98
- end
99
-
100
- def test_number_coerce_failure
101
- assert_equal(::FlatKit::FieldType::CoerceFailure, FlatKit::FieldType::TimestampType.coerce(42))
102
- end
103
-
104
- def test_number_coerce_failure_bad_parse
105
- assert_equal(::FlatKit::FieldType::CoerceFailure, FlatKit::FieldType::TimestampType.coerce("1234 56 78 90"))
106
- end
107
- end
108
- end
@@ -1,35 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestFieldType
4
- class TestUnknownType < ::Minitest::Test
5
-
6
- def unknown_items
7
- [ 'na', 'n/a', 'unk', 'unknown']
8
- end
9
-
10
- def test_unknown_items
11
- unknown_items.each do |u|
12
- assert(FlatKit::FieldType::UnknownType.matches?(u), "#{u} should be unknown")
13
- end
14
- end
15
-
16
- def test_other_class_does_not_match
17
- [ 42, Object.new, true, false, "whatever" ].each do |x|
18
- refute(FlatKit::FieldType::UnknownType.matches?(x), "#{x} should not unknown ")
19
- end
20
- end
21
-
22
- def test_coerce_unknown
23
- unknown_items.each do |u|
24
- assert_equal(u, FlatKit::FieldType::UnknownType.coerce(u), "#{u} should be unknown")
25
- end
26
- end
27
-
28
- def test_other_class_does_not_coerce
29
- [ 42, Object.new, true, false, "whatever" ].each do |x|
30
- assert_equal(::FlatKit::FieldType::CoerceFailure, FlatKit::FieldType::UnknownType.coerce(x))
31
- end
32
- end
33
-
34
- end
35
- end
@@ -1,73 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestInput
4
- class TestFile < ::Minitest::Test
5
- def test_does_not_handle_stdin_text
6
- ::FlatKit::Input::IO::STDINS.each do |e|
7
- refute(::FlatKit::Input::File.handles?(e), "#{e} is not stdin text")
8
- end
9
- end
10
-
11
- def test_handles_existing_file
12
- test_path = "tmp/test_handles_existing_file.txt"
13
- begin
14
- IO.write(test_path,"test handles existing file")
15
- assert(::FlatKit::Input::File.handles?(test_path))
16
- ensure
17
- File.unlink(test_path) if File.exist?(test_path)
18
- end
19
- end
20
-
21
- def test_only_handles_string
22
- refute(::FlatKit::Input::File.handles?(Object.new))
23
- end
24
-
25
- def test_raises_error_if_not_readable
26
- assert_raises(FlatKit::Error) { ::FlatKit::Input::File.new("tmp/does-not-exist") }
27
- end
28
-
29
- def test_init_from_path
30
- test_path = "tmp/test_init_from_path.txt"
31
- begin
32
- IO.write(test_path,"nothing to see here")
33
- io = ::FlatKit::Input::File.new(test_path)
34
- assert_equal(test_path, io.name)
35
- assert_instance_of(::File, io.io)
36
- ensure
37
- File.unlink(test_path) if File.exist?(test_path)
38
- end
39
- end
40
-
41
- def test_reads_from_file
42
- test_path = "tmp/test_reads_from_file.txt"
43
- begin
44
- text = "test_reads_from_file"
45
- IO.write(test_path,text)
46
-
47
- input = ::FlatKit::Input::File.new(test_path)
48
- content = input.io.read
49
- assert_equal(text, content)
50
-
51
- input.close
52
- ensure
53
- File.unlink(test_path) if File.exist?(test_path)
54
- end
55
- end
56
-
57
- def test_reads_from_gzfile
58
- test_path = "tmp/test_reads_from_gzfile.txt.gz"
59
- begin
60
- text = "this is something to read"
61
- system("echo '#{text}' | gzip > #{test_path}")
62
-
63
- input = ::FlatKit::Input::File.new(test_path)
64
- content = input.io.read
65
- assert_equal(text + "\n", content)
66
-
67
- input.close
68
- ensure
69
- File.unlink(test_path) if File.exist?(test_path)
70
- end
71
- end
72
- end
73
- end
@@ -1,93 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestInput
4
- class NullIO < ::IO
5
- def initialize()
6
- end
7
- end
8
-
9
- class TestIO < ::Minitest::Test
10
-
11
- def test_handles_stdin_text
12
- ::FlatKit::Input::IO::STDINS.each do |e|
13
- assert(::FlatKit::Input::IO.handles?(e), "#{e} is not stdin text")
14
- end
15
- end
16
-
17
- def test_handles_stdin_io
18
- x = $stdin
19
- assert(::FlatKit::Input::IO.handles?(x), "is not stdin")
20
- end
21
-
22
- def test_handles_stringio
23
- assert(::FlatKit::Input::IO.handles?(StringIO.new))
24
- end
25
-
26
- def test_does_not_handle_other
27
- x = Object.new
28
- refute(::FlatKit::Input::IO.handles?(x))
29
- end
30
-
31
- def test_init_from_dash
32
- io = ::FlatKit::Input::IO.new("-")
33
- assert_equal("<STDIN>", io.name)
34
- assert_equal(::STDIN, io.io)
35
- end
36
-
37
- def test_init_from_file_object
38
- test_path = "tmp/test_init_from_file_object.txt"
39
- begin
40
- File.open(test_path, "w+") do |f|
41
- io = ::FlatKit::Input::IO.new(f)
42
- assert_equal(test_path, io.name)
43
- assert_instance_of(::File, io.io)
44
- end
45
- ensure
46
- File.unlink(test_path) if File.exist?(test_path)
47
- end
48
- end
49
-
50
- def test_init_from_string_io_object
51
- sio = StringIO.new
52
- io = ::FlatKit::Input::IO.new(sio)
53
- assert_match(/StringIO/, io.name)
54
- assert_instance_of(::StringIO, io.io)
55
- end
56
-
57
- def test_init_from_io_object
58
- null_io = NullIO.new
59
- io = ::FlatKit::Input::IO.new(null_io)
60
- assert_match(/NullIO/, io.name)
61
- assert_instance_of(::TestInput::NullIO, io.io)
62
- end
63
-
64
- def test_init_from_stdin
65
- io = ::FlatKit::Input::IO.new($stdin)
66
- assert_equal("<STDIN>", io.name)
67
- assert_equal(::STDIN, io.io)
68
- end
69
-
70
- def test_init_from_invalid
71
- assert_raises(::FlatKit::Error) { ::FlatKit::Input::IO.new(Object.new) }
72
- end
73
-
74
- def test_reads_from_io
75
- test_path = "tmp/test_reads_from_io.txt"
76
- begin
77
- line = "This is a line to read"
78
- File.open(test_path, "w+") do |f|
79
- f.write(line)
80
- f.rewind
81
-
82
- io = ::FlatKit::Input::IO.new(f)
83
- content = io.io.read
84
- io.close
85
- assert_equal(content, line)
86
- end
87
- ensure
88
- File.unlink(test_path) if File.exist?(test_path)
89
- end
90
- end
91
-
92
- end
93
- end
@@ -1,22 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestJsonl
4
- class TestFormat < ::Minitest::Test
5
-
6
- def test_handles_json
7
- assert(::FlatKit::Jsonl::Format.handles?("data.json.gz"))
8
- end
9
-
10
- def test_handles_jsonl
11
- assert(::FlatKit::Jsonl::Format.handles?("data.jsonl"))
12
- end
13
-
14
- def test_handles_ndjson
15
- assert(::FlatKit::Jsonl::Format.handles?("log.ndjson"))
16
- end
17
-
18
- def test_does_not_handle_csv
19
- refute(::FlatKit::Jsonl::Format.handles?("data.csv"))
20
- end
21
- end
22
- end
@@ -1,49 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestJsonl
4
- class TestReader < ::Minitest::Test
5
- def setup
6
- @count = 20
7
- @dataset = DeviceDataset.new(count: @count)
8
- @compare_fields = @dataset.compare_fields
9
- @test_path = "tmp/test_reads_from_io.jsonl"
10
-
11
- File.open(@test_path, "wb") do |f|
12
- f.write(@dataset.records_as_jsonl)
13
- end
14
- end
15
-
16
- def teardown
17
- File.unlink(@test_path) if File.exist?(@test_path)
18
- end
19
-
20
- def test_raises_error_on_invalid_source
21
- assert_raises(::FlatKit::Error) {
22
- ::FlatKit::Jsonl::Reader.new(source: Object.new, compare_fields: nil)
23
- }
24
- end
25
-
26
- def test_reads_from_pathname
27
- reader = ::FlatKit::Jsonl::Reader.new(source: @test_path, compare_fields: @compare_fields)
28
- all = reader.to_a
29
- assert_equal(@count, reader.count)
30
- assert_equal(@count, all.size)
31
- end
32
-
33
- def test_reads_from_io
34
- File.open(@test_path) do |f|
35
- reader = ::FlatKit::Jsonl::Reader.new(source: f, compare_fields: @compare_fields)
36
- all = reader.to_a
37
- assert_equal(@count, reader.count)
38
- assert_equal(@count, all.size)
39
- end
40
- end
41
-
42
- def test_raises_error_on_io_error
43
- s = StringIO.new
44
- s.close_read
45
- reader = ::FlatKit::Jsonl::Reader.new(source: s, compare_fields: @compare_fields)
46
- assert_raises(::FlatKit::Error) { reader.to_a }
47
- end
48
- end
49
- end
@@ -1,61 +0,0 @@
1
- require_relative '../test_helper'
2
- require 'faker'
3
- require 'byebug'
4
-
5
- module TestJsonl
6
- class TestRecord < ::Minitest::Test
7
- def setup
8
- @one_row_dataset = DeviceDataset.new(count: 1)
9
- @src_record = @one_row_dataset.records.first
10
- @src_json = JSON.generate(@src_record)
11
- @compare_fields = @one_row_dataset.compare_fields
12
- end
13
-
14
- def test_initializes_from_data
15
- record = FlatKit::Jsonl::Record.new(data: @src_json, compare_fields: @compare_fields)
16
- @compare_fields.each do |k|
17
- assert_equal(@src_record[k], record[k])
18
- end
19
- end
20
-
21
- def test_ignores_non_compare_fields_values
22
- record = FlatKit::Jsonl::Record.new(data: @src_json, compare_fields: @compare_fields)
23
-
24
- refute(record["version"])
25
- end
26
-
27
- def test_is_sortable
28
- dataset = DeviceDataset.new(count: 20)
29
- fk_records = Array.new.tap do |a|
30
- dataset.records.each do |r|
31
- data = JSON.generate(r)
32
- record = FlatKit::Jsonl::Record.new(data: data, compare_fields: @compare_fields)
33
- a << record
34
- end
35
- end
36
-
37
- sorted = fk_records.sort
38
-
39
- sio = StringIO.new
40
- sorted.each do |r|
41
- sio.puts(r.to_s)
42
- end
43
-
44
- sorted_string = sio.string
45
- assert_equal(dataset.sorted_records_as_jsonl, sorted_string)
46
- end
47
-
48
- def test_converts_to_hash
49
- record = FlatKit::Jsonl::Record.new(data: @src_json, compare_fields: @compare_fields)
50
- h = record.to_hash
51
-
52
- assert_equal(@src_record, h)
53
- end
54
-
55
- def test_converts_from_record
56
- rec1 = FlatKit::Jsonl::Record.new(data: @src_json, compare_fields: @compare_fields)
57
- rec2 = FlatKit::Jsonl::Record.from_record(rec1)
58
- assert_equal(rec1, rec2)
59
- end
60
- end
61
- end
@@ -1,86 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestJsonl
4
- class TestWriter < ::Minitest::Test
5
- def setup
6
- @count = 20
7
- @dataset = DeviceDataset.new(count: @count)
8
- @compare_fields = @dataset.compare_fields
9
- @write_path = "tmp/test_writes_to_io.jsonl"
10
- @read_path = "tmp/test_read.jsonl"
11
-
12
- File.open(@read_path, "wb") do |f|
13
- f.write(@dataset.records_as_jsonl)
14
- end
15
-
16
- @reader = ::FlatKit::Jsonl::Reader.new(source: @read_path, compare_fields: @compare_fields)
17
- @records = @reader.to_a
18
- end
19
-
20
- def teardown
21
- File.unlink(@write_path) if File.exist?(@write_path)
22
- File.unlink(@read_path) if File.exist?(@read_path)
23
- end
24
-
25
- def test_raises_error_on_invalid_destination
26
- assert_raises(::FlatKit::Error) {
27
- ::FlatKit::Jsonl::Writer.new(destination: Object.new)
28
- }
29
- end
30
-
31
- def test_writes_to_pathname
32
- writer = ::FlatKit::Jsonl::Writer.new(destination: @write_path)
33
- @records.each do |r|
34
- writer.write(r)
35
- end
36
- writer.close
37
- assert_equal(@count, writer.count)
38
-
39
- expected = @dataset.records_as_jsonl
40
- actual = IO.read(@write_path)
41
- assert_equal(expected, actual)
42
- end
43
-
44
- def test_postion
45
- File.open(@write_path, "w+") do |f|
46
- writer = ::FlatKit::Jsonl::Writer.new(destination: f)
47
-
48
- byte_offset = 0
49
- @records.each_with_index do |r, idx|
50
- record_length = r.data.bytesize
51
-
52
- position = writer.write(r)
53
-
54
- # make sure write stores the last_position api and returns that value
55
- assert_equal(position, writer.last_position)
56
-
57
- assert_equal(idx, position.index)
58
- assert_equal(byte_offset, position.offset)
59
- assert_equal(record_length, position.bytesize)
60
-
61
- byte_offset += record_length
62
-
63
- current_position = writer.current_position
64
- assert_equal(idx+1, current_position.index)
65
- assert_equal(byte_offset, current_position.offset)
66
- assert_equal(0, current_position.bytesize)
67
-
68
- end
69
- writer.close
70
-
71
- assert_equal(@count, writer.count)
72
-
73
- expected = @dataset.records_as_jsonl
74
- actual = IO.read(@write_path)
75
- assert_equal(expected, actual)
76
- end
77
- end
78
-
79
- def test_raises_error_on_io_error
80
- s = StringIO.new
81
- s.close_write
82
- writer = ::FlatKit::Jsonl::Writer.new(destination: s)
83
- assert_raises(::FlatKit::Error) { writer.write(@records.first) }
84
- end
85
- end
86
- end
@@ -1,60 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestOutput
4
- class TestFile < ::Minitest::Test
5
- def test_does_not_handle_stderr_text
6
- ::FlatKit::Output::IO::STDERRS.each do |e|
7
- refute(::FlatKit::Output::File.handles?(e), "#{e} is not stderr text")
8
- end
9
- end
10
-
11
- def test_only_handles_string
12
- refute(::FlatKit::Output::File.handles?(Object.new))
13
- end
14
-
15
- def test_doest_not_handles_stdout_text
16
- ::FlatKit::Output::IO::STDOUTS.each do |e|
17
- refute(::FlatKit::Output::File.handles?(e), "#{e} is not stdout text")
18
- end
19
- end
20
-
21
- def test_init_from_path
22
- test_path = "tmp/test_init_from_path.txt"
23
- begin
24
- io = ::FlatKit::Output::File.new(test_path)
25
- assert_equal(test_path, io.name)
26
- assert_instance_of(::File, io.io)
27
- ensure
28
- File.unlink(test_path) if File.exist?(test_path)
29
- end
30
- end
31
-
32
- def test_writes_to_file
33
- test_path = "tmp/test_writes_to_file.txt"
34
- begin
35
- output = ::FlatKit::Output::File.new(test_path)
36
- assert_equal(test_path, output.name)
37
- output.io.write("test_writes_to_file output")
38
- output.close
39
- t = IO.read(test_path)
40
- assert_equal("test_writes_to_file output", t)
41
- ensure
42
- File.unlink(test_path) if File.exist?(test_path)
43
- end
44
- end
45
-
46
- def test_writes_to_gzfile
47
- test_path = "tmp/test_writes_to_gzfile.txt.gz"
48
- begin
49
- output = ::FlatKit::Output::File.new(test_path)
50
- assert_equal(test_path, output.name)
51
- output.io.write("test_writes_to_gzfile output")
52
- output.close
53
- t = %x[ gunzip -c #{test_path} ]
54
- assert_equal("test_writes_to_gzfile output", t)
55
- ensure
56
- File.unlink(test_path) if File.exist?(test_path)
57
- end
58
- end
59
- end
60
- end
@@ -1,104 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module TestOutput
4
- class NullIO < ::IO
5
- def initialize()
6
- end
7
- end
8
-
9
- class TestIO < ::Minitest::Test
10
- def test_handles_stderr_text
11
- ::FlatKit::Output::IO::STDERRS.each do |e|
12
- assert(::FlatKit::Output::IO.handles?(e), "#{e} is not stderr text")
13
- end
14
- end
15
-
16
- def test_handles_stderr_io
17
- x = $stderr
18
- assert(::FlatKit::Output::IO.handles?(x), "is not stderr")
19
- end
20
-
21
- def test_handles_stdout_text
22
- ::FlatKit::Output::IO::STDOUTS.each do |e|
23
- assert(::FlatKit::Output::IO.handles?(e), "#{e} is not stdout text")
24
- end
25
- end
26
-
27
- def test_handles_stdout_io
28
- x = $stderr
29
- assert(::FlatKit::Output::IO.handles?(x), "is not stdout")
30
- end
31
-
32
- def test_handles_stringio
33
- assert(::FlatKit::Output::IO.handles?(StringIO.new))
34
- end
35
-
36
- def test_does_not_handle_other
37
- x = Object.new
38
- refute(::FlatKit::Output::IO.handles?(x))
39
- end
40
-
41
- def test_init_from_dash
42
- io = ::FlatKit::Output::IO.new("-")
43
- assert_equal("<STDOUT>", io.name)
44
- assert_equal(::STDOUT, io.io)
45
- end
46
-
47
- def test_init_from_stderr_text
48
- io = ::FlatKit::Output::IO.new("stderr")
49
- assert_equal("<STDERR>", io.name)
50
- assert_equal(::STDERR, io.io)
51
- end
52
-
53
- def test_init_from_file_object
54
- test_path = "tmp/test_init_from_file_object.txt"
55
- begin
56
- File.open(test_path, "w") do |f|
57
- io = ::FlatKit::Output::IO.new(f)
58
- assert_equal(test_path, io.name)
59
- assert_instance_of(::File, io.io)
60
- end
61
- ensure
62
- File.unlink(test_path) if File.exist?(test_path)
63
- end
64
- end
65
-
66
- def test_init_from_stdout
67
- io = ::FlatKit::Output::IO.new($stdout)
68
- assert_equal("<STDOUT>", io.name)
69
- assert_equal(::STDOUT, io.io)
70
- end
71
-
72
- def test_init_from_string_io_object
73
- sio = StringIO.new
74
- io = ::FlatKit::Output::IO.new(sio)
75
- assert_match(/StringIO/, io.name)
76
- assert_instance_of(::StringIO, io.io)
77
- end
78
-
79
- def test_init_from_io_object
80
- null_io = NullIO.new
81
- io = ::FlatKit::Output::IO.new(null_io)
82
- assert_match(/NullIO/, io.name)
83
- assert_instance_of(::TestOutput::NullIO, io.io)
84
- end
85
-
86
- def test_writes_to_io
87
- test_path = "tmp/test_writes_to_io.txt"
88
- begin
89
- File.open(test_path, "w") do |f|
90
- io = ::FlatKit::Output::IO.new(f)
91
- assert_equal(test_path, io.name)
92
- assert_instance_of(::File, io.io)
93
- io.io.write("test_writes_to_io output")
94
- io.close
95
- end
96
- t = IO.read(test_path)
97
- assert_equal("test_writes_to_io output", t)
98
- ensure
99
- File.unlink(test_path) if File.exist?(test_path)
100
- end
101
- end
102
-
103
- end
104
- end