jeudi 7 janvier 2016

TO create unit test of following Ruby code

I am working on a problem to create the unit test program of a Ruby program.

The Ruby code is as follows:

require 'sqlite3'
require 'CSV'

class Task8
  def initialize
      @f=File.open('C:/Users/aroraku/Desktop/boc-adap_td-agent.log-2.log','r')
      # @f1=CSV.open('C:/Users/aroraku/Desktop/task8_data.csv','wb')
      @db=SQLite3::Database.open('adaptive.db')
  end


 def check_line(line)
      if(line=~/deregistered adap_tdagt$/)
         return line
      end
 end

 def store_in_file
       CSV.open('C:/Users/aroraku/Desktop/task8_data.csv','wb') do |csv|
        while line=@f.gets do
            if( check_line(line)!=nil)
                line=~ (/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+\d{4}) \[info\]:\s(\S+) /)
                uuid=@db.execute(
                "Select devices.uuid
                 from devices,bas_tokens
                 where bas_tokens.device_id=devices.id AND
                 bas_tokens.token='#{$2}'")
                 uuid=uuid[0]
                 uuid=uuid[0]
              csv << [$1,uuid]
              end
        end
      end
 end
end

The unit test program I am able to write successfully is as follows:

require 'minitest/autorun'
require 'minitest/unit'
require './task8'

class TestTask8 < Minitest::Test
  def system

  end
  def test_check_line
     result=Task8.new.check_line("Nov 13 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [info]: qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d has been deregistered adap_tdagt")
     assert_equal result,"Nov 13 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [info]: qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d has been deregistered adap_tdagt",
     'Wrong Output'
  end
  def test_check_line_wrong
     result=Task8.new.check_line("Nov 13 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [info]: qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d has been deregistered")
     assert_nil result
  end
end

I am not able to create the test program for store_in_file function. If someone please can help me in this then it would be really great. Also, If I can improve my Ruby program in any way then please let me know.

Aucun commentaire:

Enregistrer un commentaire