【问题标题】:Ruby: parsing over a csv file and expecting some textRuby:解析 csv 文件并期待一些文本
【发布时间】:2021-10-21 09:29:13
【问题描述】:

我有一些日志需要分析,以检查日志是否没有异常并且可以说是不正确的形式。

我已经为它生成了一个 CSV 文件:

"timestamp","source","message
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 +0800] 10.102.141.82 - GET /deviceManager/rest/
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 +0800] 10.102.141.82 - GET /deviceManager/rest/
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 +0800] 10.102.141.82 - GET /deviceManager/rest/
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 +0800] 10.102.141.82 - GET /deviceManager/rest/

我使用 CSV gem 来解析/读取这个文件并使用 RSpec 测试来期望一些值/文本/时间格式等。我已经编写了下面的代码。例如,它需要从 8 到 12 的行,我希望在这些行中有一个名为“Huawei”f.e 的文本。

    RSpec.describe "Log parsing" do
    it 'returns the source' do
      table = CSV.read("Messages_result.csv")
      puts arr = table.values_at(8..12)
      arr.each do |rows|
         expect(rows).to include('Huawei')
      end
    end
end

我遇到的问题是它总是执行第一行的期望,但我想解析/迭代整个 CSV 文件,并且还应该为每一行显示一个结果。我的期望信息当然会改变,但我只想先检查一下像华为这样的基本文本。有人可以说明我做错了什么,因为理论上每一个都应该通过完整的行并为每行抛出一个期望?

【问题讨论】:

  • 您的 CSV 文件被截断,请添加缺少的行尾
  • arr 是否有可能为空?在expect(arr).not_to be_empty之前打勾
  • 你只选择CSV文件的第8到12行(考虑到标题是第0行)有什么原因吗?此外,您将整个 CSV 文件加载到内存中,而您只是逐行检查它。
  • @Anthony 不是空的,我期待 ["2021-10-18T09:11:24.000Z", "Storage", "Storage apache: [18/Oct/2021:09:11: 24 +0800] 10.102.141.82 -.../license/feature HTTP/1.1 python-requests/2.21.0 - - application/json - / gzip, deflate 200 49 0"] 以包括"华为”,所以它肯定给了我第一个元素的错误
  • @Fravadona 只是为了在我浏览整个 csv 文件(大约 500 行)之前测试我的代码中的某些元素

标签: ruby csv rspec


【解决方案1】:

我想解析/遍历整个 csv 文件,并且还应该为每一行显示一个结果。

这是不可能的,请参阅Add a configuration option to continue on failure

也就是说,通过稍微修改代码,您可以让 Rspec 检查整个文件并显示所有未通过expect 的行:

RSpec.describe "Log parsing" do
  CSV.foreach("Messages_result.csv", :headers => true) do |row|
    it 'returns the source' do
      expect(row.to_h.values).to include("Huawei")
    end
  end
end

输出:

FF

Failures:

  1) Log parsing returns the source
     Failure/Error: expect(row.to_h.values).to include("Huawei")
       expected ["2021-10-18T09:10:29.000Z", "Storage", "Storage apache: [18/Oct/2021:09:10:29 +0800] 10.102.141.82 - GET /deviceManager/rest/"] to include "Huawei"
     # ./Messages_result.rb:10:in `block (3 levels) in <main>'

  2) Log parsing returns the source
     Failure/Error: expect(row.to_h.values).to include("Huawei")
       expected ["2021-10-18T09:11:24.000Z", "Storage", "Storage apache: [18/Oct/2021:09:11:24 +0800] 10.102.141.82 -...../license/feature HTTP/1.1 python-requests/2.21.0 - - application/json - / gzip, deflate 200 49 0"] to include "Huawei"
     # ./Messages_result.rb:10:in `block (3 levels) in <main>'

  3) ...

如果您真的想为 CSV 的每一行显示一条消息,那么您别无选择,只能自己打印。例如:

# get the output stream that Rspec is currently using
ostream = RSpec.configure { |c| c.output_stream }

# define a few colorization helpers
if ostream.tty?
  def red str; "\e[31m#{str}\e[0m"; end
  def green str; "\e[32m#{str}\e[0m"; end
else
  def red str; str; end
  def green str; str; end
end

RSpec.describe "Log parsing" do
  it 'returns the source' do
    ostream.puts
    ostream.puts "  -) Log parsing returns the source - details"
    expected = "Huawei"
    success = true
    CSV.foreach("Messages_result.csv", :headers => true) do |row|
      values = row.to_h.values
      detail = "expected #{values} to include #{expected.inspect}"
      ostream.print ' ' * 5
      if values.include?(expected)
        ostream.puts green("PASSED: #{detail}")
      else
        ostream.puts red("FAILED: #{detail}")
        success = false
      end
    end
    ostream.puts
    ostream.flush
    expect(success).to be(true)
  end
end

【讨论】:

  • 我已经尝试过了,它产生了相同的结果。
  • @Khan_saab 检查我的更新答案。
  • 非常感谢您的帮助 :) 这些效果很好。不,我不打算将它用作记录器,只是解析它
  • @khan_saab 不客气;-)。顺便说一句,你可以accept我的答案,如果这是你要找的;-)
  • 哦,对不起,新来的 :D 再次感谢 :)
猜你喜欢
  • 2011-12-23
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多