view · edit · sidebar · attach · print · history

20101122-testcase-grant_download-command

<< Masa.20101124-update-rpdf2txt-debug-import_gkv | 2010 | Masa.20101119-create-grant_download-command-de_oddb_org >>


  1. List up updated methods
  2. Make testcases
  3. Study Selenium
  4. Practice SeleniumIDE

Goal
  • Testcase grant_download / 100%
Milestones
  1. List up updated methods 8:45
  2. Make testcases one by one
  3. Study Selenium
  4. Make testcases one by one again
  5. Debug test/export/server.rb
Summary
Commits
ToDo Tomorrow
Keep in Mind
  1. Debug testcases in test/export/test_server.rb de.oddb.org
  2. A bug import_gkv Tue Nov 16 02:00:10 2010: de.oddb.org Zubef (PDF)
  3. Compression (refer to lib/oddb/export/server.rb), Test cases (grant_download, Logging, Reporting)
  4. Log Error: on production server, de.oddb.org/log/import_dimdi, import_pharmnet
  5. On Ice
  6. emerge --sync

List up updated methods

Refereces

Updated methods

  1. lib/oddb/business/grant_download.rb#grant_download, expired?
  2. lib/oddb/util/server.rb#grant_download
  3. lib/oddb/html/state/global.rb#grant_download
  4. lib/oddb/html/view/download.rb#to_html
  5. lib/oddb/export/server.rb#Server.report_download
  6. lib/oddb/export/server.rb#Server.on_weekday

Make testcases

1.lib/oddb/business/grant_download.rb#grant_download, expired?

test/business/test_grant_download.rb

#!/usr/bin/env ruby
# Business::TestGrantDownload -- de.oddb.org -- 22.11.2011 -- mhatakeyama@ywesee.com


$: << File.expand_path('../../lib', File.dirname(__FILE__))

require 'flexmock'
require 'test/unit'
require 'oddb/business/grant_download'

module ODDB
  module Business
    class TestGrantDownload < Test::Unit::TestCase
      include FlexMock::TestCase
      def test_grant_download
      end
      def test_expired?
      end
    end
  end
end

Confirm test pass

masa@masa ~/ywesee/de.oddb.org $ ruby test/business/test_grant_download.rb 
Loaded suite test/business/test_grant_download
Started
..
Finished in 0.000161 seconds.
2 tests, 0 assertions, 0 failures, 0 errors

Update testcases

#!/usr/bin/env ruby
# Business::TestGrantDownload -- de.oddb.org -- 22.11.2011 -- mhatakeyama@ywesee.com


$: << File.expand_path('../../lib', File.dirname(__FILE__))

require 'test/unit'
require 'oddb/business/grant_download'

module ODDB
  module Business
    class TestGrantDownload < Test::Unit::TestCase
      def setup
        @user = GrantDownload.new('aaa@bbb.ccc')
        @user.grant_download('test1.dat', Time.local(1999,12,31))
        @user.grant_download('test2.dat', Time.local(2999,12,31))
      end
      def test_grant_download
        assert_equal(@user.grant_list.length, 2)
        assert_equal(@user.grant_list['test1.dat'], Time.local(1999,12,31))
      end
      def test_expired?
        assert_equal(@user.expired?('test1.dat'), true)
        assert_equal(@user.expired?('test2.dat'), false)
        assert_equal(@user.expired?('test3.dat'), true)
      end
    end
  end
end

Test

masa@masa ~/ywesee/de.oddb.org $ ruby test/business/test_grant_download.rb 
Loaded suite test/business/test_grant_download
Started
..
Finished in 0.000289 seconds.

2 tests, 5 assertions, 0 failures, 0 errors

2. lib/oddb/util/server.rb#grant_download

test/util/test_server.rb

...
def test_grant_download
end
...

Check test pass

masa@masa ~/ywesee/de.oddb.org $ ruby test/util/test_server.rb 
/home/masa/ywesee/de.oddb.org/lib/oddb/html/view/drugs/package.rb:373: warning: parenthesize argument(s) for future version
Loaded suite test/util/test_server
Started
....
Finished in 0.014745 seconds.

4 tests, 9 assertions, 0 failures, 0 errors

Update testcases

      def test_grant_download__1
        # Case: Usage 
        usage = "Usage:\n" +
          "  Set  grant: grant_download 'email address', 'file', Time.local(20yy,mm,dd)\n" +
          "  Show grant: grant_download 'email address'\n"
        assert_equal(usage, @server.grant_download)
      end
      def test_grant_download__2
        # Case: No registration
        flexstub(ODDB::Business::GrantDownload).should_receive(:find_by_email).and_return(nil)
        assert_equal('No registration for aaa@bbb.ccc', @server.grant_download('aaa@bbb.ccc'))
      end
      def test_grant_download__3
        # Case: Find a registration case
        hash = {
          'test1.dat' => Time.local(2000,1,1),
          'test2.dat' => Time.local(2001,1,1)
        }
        flexstub(ODDB::Business::GrantDownload).should_receive(:find_by_email).and_return do
          flexmock do |user|
            user.should_receive(:grant_list).and_return(hash)
            user.should_receive(:uid).and_return('123')
          end
        end
        result = "grant list(total:2): odba_id: 123\n20010101, test2.dat\n20000101, test1.dat"
        assert_equal(result, @server.grant_download('aaa@bbb.ccc'))
      end
      def test_grant_download__3
        # Case: Registration
        email = 'aaa@bbb.ccc'
        file  = 'test.dat'
        expiry_time = Time.local(2010,12,31)
        flexstub(ODDB::Business::GrantDownload) do |klass|
          klass.should_receive(:find_by_email).with(email)
          klass.should_receive(:new).with(email).and_return do
            flexmock('grant_download object') do |user|
              user.should_receive(:grant_download).with(file, expiry_time)
              user.should_receive(:save)
            end
          end
        end
        ODDB.config.http_server = 'http://de.oddb.org'
        result = "http://de.oddb.org/de/temp/grant_download/email/aaa@bbb.ccc/file/test.dat"
        assert_equal(result, @server.grant_download('aaa@bbb.ccc', 'test.dat', Time.local(2010,12,31)))
      end

Test

masa@masa ~/ywesee/de.oddb.org $ ruby test/util/test_server.rb 
/home/masa/ywesee/de.oddb.org/lib/oddb/html/view/drugs/package.rb:373: warning: parenthesize argument(s) for future version
Loaded suite test/util/test_server
Started
......
Finished in 0.022312 seconds.

6 tests, 12 assertions, 0 failures, 0 errors

3. lib/oddb/export/server.rb#Server.on_weekday

test/export/test_server.rb

      def test_on_weekday
      end

Confirm test pass

masa@masa ~/ywesee/de.oddb.org $ ruby test/export/test_server.rb 
/home/masa/ywesee/de.oddb.org/lib/oddb/export/yaml.rb:119: undefined method `export' for ODDB::Text::Document:Class (NoMethodError)
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from /home/masa/ywesee/de.oddb.org/lib/oddb/persistence/odba/export.rb:5
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from /home/masa/ywesee/de.oddb.org/lib/oddb/export/server.rb:7
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from test/export/test_server.rb:9

Note

  • Error!!

Search when this error happened for the first time

There is no error

masa@masa ~/work/de.oddb.org $ git checkout a430198f322d681c36894c1331c655763a8d36d8
Previous HEAD position was 8053d91... Export Fachinfos to yaml
HEAD is now at a430198... Correct command-line arguments for pharmnet-updater job.
masa@masa ~/work/de.oddb.org $ ruby test/export/test_server.rb 
[DEPRECATED] By requiring 'spreadsheet/excel' you are loading a Compatibility
             layer which provides a drop-in replacement for Spreadsheet::Excel
             versions <= 0.3.5.1. This code will be removed in Spreadsheet
             version 1.0.0

Loaded suite test/export/test_server
Started
.....
Finished in 0.006692 seconds.

5 tests, 18 assertions, 0 failures, 0 errors

Error happens

masa@masa ~/work/de.oddb.org $ git checkout 8053d913db718b1506afc0f9d81d762cc2b7023a
Previous HEAD position was a430198... Correct command-line arguments for pharmnet-updater job.
HEAD is now at 8053d91... Export Fachinfos to yaml
masa@masa ~/work/de.oddb.org $ ruby test/export/test_server.rb 
[DEPRECATED] By requiring 'spreadsheet/excel' you are loading a Compatibility
             layer which provides a drop-in replacement for Spreadsheet::Excel
             versions <= 0.3.5.1. This code will be removed in Spreadsheet
             version 1.0.0

/home/masa/work/de.oddb.org/lib/oddb/export/yaml.rb:119: undefined method `export' for ODDB::Text::Document:Class (NoMethodError)
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from /home/masa/work/de.oddb.org/lib/oddb/export/server.rb:8
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from test/export/test_server.rb:9

suspend

Focus only on test_on_weekday

      def test_on_weekday
      end

Confirm test pass

masa@masa ~/ywesee/de.oddb.org $ ruby test/export/test_server.rb 
[DEPRECATED] By requiring 'spreadsheet/excel' you are loading a Compatibility
             layer which provides a drop-in replacement for Spreadsheet::Excel
             versions <= 0.3.5.1. This code will be removed in Spreadsheet
             version 1.0.0

Loaded suite test/export/test_server
Started
.
Finished in 0.000221 seconds.

1 tests, 0 assertions, 0 failures, 0 errors

Update testcase

      def test_on_weekday
        called = false
        Server.on_weekday(0, Date.new(2010,11,22)) {
          called = true
        }
        assert_equal(false, called, "Should not have called the block: 0 != 1")
        Server.on_weekday(0, Date.new(2010,11,21)) {
          called = true
        }
        assert_equal(true, called, "Should have called the block: 0 == 0")
        called = false
        Server.on_weekday(Date.today.wday) {
          called = true
        }
        assert_equal(true, called,
                     "Should have called the block: defaults to today")
      end

Test

masa@masa ~/ywesee/de.oddb.org $ ruby test/export/test_server.rb 
[DEPRECATED] By requiring 'spreadsheet/excel' you are loading a Compatibility
             layer which provides a drop-in replacement for Spreadsheet::Excel
             versions <= 0.3.5.1. This code will be removed in Spreadsheet
             version 1.0.0

Loaded suite test/export/test_server
Started
.
Finished in 0.000702 seconds.

1 tests, 3 assertions, 0 failures, 0 errors

4. lib/oddb/export/server.rb#Server.report_download

test/export/test_server.rb

def def test_report_download
end

Confirm test pass

masa@masa ~/ywesee/de.oddb.org $ ruby test/export/test_server.rb 
[DEPRECATED] By requiring 'spreadsheet/excel' you are loading a Compatibility
             layer which provides a drop-in replacement for Spreadsheet::Excel
             versions <= 0.3.5.1. This code will be removed in Spreadsheet
             version 1.0.0

Loaded suite test/export/test_server
Started
..
Finished in 0.000752 seconds.

2 tests, 3 assertions, 0 failures, 0 errors

Update testcase

      def test_report_download__1
        # Case: there is no log file
        flexstub(File) do |klass|
          klass.should_receive(:join)
          klass.should_receive(:readlines).and_raise(StandardError)
        end
        subject = sprintf("de.ODDB.org Report - Download-Statistics - %s", Time.now.strftime('%m/%Y'))
        lines = /Nothing/
        flexstub(Util::Mail).should_receive(:notify_admins).with(subject, lines)
        assert_nothing_raised do
          Server.report_download
        end

      end
      def test_report_download__2
        # Case: there is a log file
        subject = sprintf("de.ODDB.org Report - Download-Statistics - %s", Time.now.strftime('%m/%Y'))
        lines = 'test report'
        flexstub(File) do |klass|
          klass.should_receive(:join)
          klass.should_receive(:readlines).and_return(lines)
        end
        flexstub(Util::Mail).should_receive(:notify_admins).with(subject, lines)
        assert_nothing_raised do
          Server.report_download
        end
      end

Test

masa@masa ~/ywesee/de.oddb.org $ ruby test/export/test_server.rb 
[DEPRECATED] By requiring 'spreadsheet/excel' you are loading a Compatibility
             layer which provides a drop-in replacement for Spreadsheet::Excel
             versions <= 0.3.5.1. This code will be removed in Spreadsheet
             version 1.0.0

Loaded suite test/export/test_server
Started
...
Finished in 0.003566 seconds.

3 tests, 5 assertions, 0 failures, 0 errors

Commit

5. lib/oddb/html/state/global.rb#grant_download

suspend

Study Selenium

References

sun-jdk install

$ sudo vim /etc/make.conf

ACCEPT_LICENSE="dlj-1.1"

$ sudo vim /etc/portage/package.keywords

dev-java/sun-jdk ~amd64
dev-java/java-config ~amd64
dev-java/java-config-wrapper ~amd64

Install

masa@masa ~/work $ emerge sun-jdk sun-jre-bin -vp

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N    ] dev-java/java-config-wrapper-0.16  8 kB
[ebuild  N    ] dev-java/java-config-2.1.11-r2  60 kB
[ebuild  N    ] dev-java/sun-jdk-1.6.0.22  USE="X alsa -derby -doc -examples -jce -nsplugin -odbc" 81,887 kB
[ebuild  N    ] virtual/jdk-1.6.0  0 kB
[ebuild  N    ] virtual/jre-1.6.0  0 kB
[ebuild  N    ] dev-java/sun-jre-bin-1.6.0.22  USE="X alsa -jce -nsplugin -odbc" 0 kB

Total: 6 packages (6 new), Size of downloads: 81,954 kB

$ sudo emerge sun-jdk sun-jre-bin

masa@masa ~/work $ java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

Tutorials (Japanese)

Install Selenium-IDE

How to use Selenium-IDE (Japanese)

Practice SeleniumIDE

1. Start Seleniumu-IDE on Firefox

 Tool -> Selenium-IDE

2. Record

3. Re-play

4. Save as Ruby code

 Option -> Format -> Ruby (Text/Unit) - Selenium RC
 File -> Save As
masa@masa ~/work $ cat Testcase.rb 
require "test/unit"
require "rubygems"
gem "selenium-client"
require "selenium/client"

class Testcase < Test::Unit::TestCase

  def setup
    @verification_errors = []
    @selenium = Selenium::Client::Driver.new \
      :host => "localhost",
      :port => 4444,
      :browser => "*chrome",
      :url => "http://www.google.co.jp/",
      :timeout_in_second => 60

    @selenium.start_new_browser_session
  end

  def teardown
    @selenium.close_current_browser_session
    assert_equal [], @verification_errors
  end

  def test_case
    @selenium.open "/"
    assert_equal "Google", @selenium.get_title
    assert @selenium.is_text_present("")
    @selenium.type "q", "ywesee"
    @selenium.click "btnG"
    @selenium.wait_for_page_to_load "30000"
    assert_equal "ywesee - Google &#26908;&#32034;", @selenium.get_title
    @selenium.click "link=ywesee.com - Home"
    @selenium.wait_for_page_to_load "30000"
    assert_equal "ywesee.com - Home", @selenium.get_title
  end
end

5. Test run

masa@masa ~/work $ ruby Testcase.rb 
Loaded suite Testcase
Started
Mon Nov 22 16:03:46 +0100 2010 selenium-client received failure from selenium server:
requested:
        cmd=getNewBrowserSession
        1=*chrome
        2=http://www.google.co.jp/
        3=
        4=
received:
        ""
        called from /usr/lib64/ruby/1.8/timeout.rb:62:in `timeout'
E
Finished in 0.004025 seconds.

  1) Error:
test_case(Testcase):
Selenium::CommandError: 
    /usr/lib64/ruby/gems/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:13:in `remote_control_command'
    /usr/lib64/ruby/1.8/timeout.rb:62:in `timeout'
    /usr/lib64/ruby/1.8/timeout.rb:93:in `timeout'
    /usr/lib64/ruby/gems/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:11:in `remote_control_command'
    /usr/lib64/ruby/gems/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:19:in `string_command'
    /usr/lib64/ruby/gems/1.8/gems/selenium-client-1.2.18/lib/selenium/client/base.rb:85:in `start_new_browser_session'
    Testcase.rb:17:in `setup'

1 tests, 1 assertions, 0 failures, 1 errors

Note

  • Error

Check selenium gems

masa@masa ~/work $ gem search selenium -r

*** REMOTE GEMS ***

rubySelenium (0.0.8)
Selenium (1.1.14)
selenium (0.0.3)
Selenium-Client (0.1)
selenium-client (1.2.18)
selenium-rc (2.2.4)
seleniumrc (0.0.2)

Notes

  • There are many similar gems!!
  • What is the difference?

Try-and-error

masa@masa ~/work $ gem search selenium

*** LOCAL GEMS ***

Selenium (1.1.14)
selenium-client (1.2.18)

Testcase (File -> Save as (Ruby (Unit/Test))

masa@masa ~/work $ cat Testcase.rb 
require "test/unit"
require "rubygems"
gem "selenium-client"
require "selenium/client"

class Testcase < Test::Unit::TestCase

  def setup
    @verification_errors = []
    @selenium = Selenium::Client::Driver.new \
      :host => "localhost",
      :port => 4444,
      :browser => "*chrome",
      :url => "http://www.google.co.jp/",
      :timeout_in_second => 60

    @selenium.start_new_browser_session
  end

  def teardown
    @selenium.close_current_browser_session
    assert_equal [], @verification_errors
  end

  def test_case
    @selenium.open "/"
    assert_equal "Google", @selenium.get_title
    assert @selenium.is_text_present("")
    @selenium.type "q", "ywesee"
    @selenium.click "btnG"
    @selenium.wait_for_page_to_load "30000"
    assert_equal "ywesee - Google &#26908;&#32034;", @selenium.get_title
    @selenium.click "link=ywesee.com - Home"
    @selenium.wait_for_page_to_load "30000"
    assert_equal "ywesee.com - Home", @selenium.get_title
  end
end

Run selenium-server

masa@masa ~/work/test/selenium-server-1.0.3 $ java -jar selenium-server.jar 

Test

masa@masa ~/work $ ruby Testcase.rb 
Loaded suite Testcase
Started
F
Finished in 5.31861 seconds.

  1) Failure:
test_case(Testcase) [Testcase.rb:32]:
<"ywesee - Google \346\244\234\347\264\242"> expected but was
<"ywesee - Google-Suche">.

1 tests, 4 assertions, 1 failures, 0 errors

Note

  • Working!! and it looks good
  • Automatically firefox runs
view · edit · sidebar · attach · print · history
Page last modified on July 13, 2011, at 11:54 AM