testdata_generater_for_mysql 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +7 -0
- data/README.md +7 -0
- data/Rakefile +1 -0
- data/example/sample1.rb +115 -0
- data/lib/testdata_generater_for_mysql.rb +5 -0
- data/lib/testdata_generater_for_mysql/core.rb +180 -0
- data/lib/testdata_generater_for_mysql/version.rb +3 -0
- data/test/test.rb +186 -0
- data/test/test_helper.rb +5 -0
- data/testdata_generater_for_mysql.gemspec +24 -0
- metadata +59 -0
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/README.md
    ADDED
    
    
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            require "bundler/gem_tasks"
         | 
    
        data/example/sample1.rb
    ADDED
    
    | @@ -0,0 +1,115 @@ | |
| 1 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'rubygems'
         | 
| 4 | 
            +
            require 'testdata_generater_for_mysql'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            #
         | 
| 7 | 
            +
            # localhostにtestdata_generater_for_mysql_testというデータベースを作成し
         | 
| 8 | 
            +
            # rootのパスワードなしでアクセスできるようにしてあるとして
         | 
| 9 | 
            +
            #
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            # 取り敢えずおまじない
         | 
| 12 | 
            +
            include TestdataGeneraterForMysql
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            # データベースへのアクセス情報を設定します(Mysql2::Client.newの引数です)
         | 
| 15 | 
            +
            setup_mysql_client :host => "127.0.0.1", :username => "root",:database=>'testdata_generater_for_mysql_test'
         | 
| 16 | 
            +
            # マルチプルインサートの実行単位を指定します(以下だと200行ずつインサート実行。defaultは100)
         | 
| 17 | 
            +
            insert_per_rows 200
         | 
| 18 | 
            +
            # プログレスバーを非表示にしたければ以下をコメントアウト
         | 
| 19 | 
            +
            #hide_progress_bar
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            # 取り敢えず必要なテーブルを作成します(すでに存在する場合は消します)
         | 
| 22 | 
            +
            query "DROP TABLE IF EXISTS tests;"
         | 
| 23 | 
            +
            query "
         | 
| 24 | 
            +
            CREATE TABLE tests (
         | 
| 25 | 
            +
              `id`          int(11) NOT NULL auto_increment,
         | 
| 26 | 
            +
              `brand_id`    int(11) NOT NULL,
         | 
| 27 | 
            +
              `shop_id`     int(11) NOT NULL,
         | 
| 28 | 
            +
              `user_id`     int(11) NOT NULL,
         | 
| 29 | 
            +
              `name`        varchar(20) NOT NULL,
         | 
| 30 | 
            +
              `value1`      int(11) NOT NULL,
         | 
| 31 | 
            +
              `value_nil`   int(11) ,
         | 
| 32 | 
            +
              `value_func`  varchar(20),
         | 
| 33 | 
            +
              `value_true`  tinyint(1) ,
         | 
| 34 | 
            +
              `value_time`  datetime ,
         | 
| 35 | 
            +
              `created_at`  datetime ,
         | 
| 36 | 
            +
              PRIMARY KEY  (`id`),
         | 
| 37 | 
            +
              KEY `idx01` USING BTREE (`brand_id`,`shop_id`)
         | 
| 38 | 
            +
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
         | 
| 39 | 
            +
            "
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            #
         | 
| 42 | 
            +
            # ■データ作成
         | 
| 43 | 
            +
            #
         | 
| 44 | 
            +
            # まずはループ情報を設定します。
         | 
| 45 | 
            +
            # テストデータを作る際は以下な感じで行うと思いますが
         | 
| 46 | 
            +
            #
         | 
| 47 | 
            +
            # (1..10).each do |brand_id|
         | 
| 48 | 
            +
            #   (1..3).each do |shop_id|
         | 
| 49 | 
            +
            #     (1..15).each do |user_id|
         | 
| 50 | 
            +
            #       # do_someting here with brand_id,show_id,user_id
         | 
| 51 | 
            +
            #     end
         | 
| 52 | 
            +
            #   end
         | 
| 53 | 
            +
            # end
         | 
| 54 | 
            +
            #
         | 
| 55 | 
            +
            # これを以下で表現しています。
         | 
| 56 | 
            +
            #
         | 
| 57 | 
            +
            CNT_BRAND      =    21
         | 
| 58 | 
            +
            SHOP_PER_BRAND =    15
         | 
| 59 | 
            +
            USER_PER_SHOP  = 1_003
         | 
| 60 | 
            +
            loops = [
         | 
| 61 | 
            +
              [:brand_id, (1..CNT_BRAND)],
         | 
| 62 | 
            +
              [:shop_id,  (1..SHOP_PER_BRAND)],
         | 
| 63 | 
            +
              ['user_id', (1..USER_PER_SHOP)] # 文字列でキーを指定も
         | 
| 64 | 
            +
            ]
         | 
| 65 | 
            +
            #
         | 
| 66 | 
            +
            # 次に各列の処理を設定します。
         | 
| 67 | 
            +
            # データはハッシュにて設定します。設定したいカラムの名前をkeyにて設定、
         | 
| 68 | 
            +
            # valueはProcインスタンスにて設定します。Procのイニシャライザのブロック引数に
         | 
| 69 | 
            +
            # loopsで設定した値が引き渡されます(上記ループ解説の# do_somethingの箇所の値が
         | 
| 70 | 
            +
            # ハッシュにて引き渡されます)。
         | 
| 71 | 
            +
            # 基本的にvalueは実行時にエスケープされシングルクォーテーションで囲まれます(数値でも同様)。
         | 
| 72 | 
            +
            # 「NOW()」等関数を指定したい場合は"NOW()".to_funcと指定すると値にエスケープ及び
         | 
| 73 | 
            +
            # シングルクォーテーションでの囲みがかからなくなります
         | 
| 74 | 
            +
            #
         | 
| 75 | 
            +
            procs = {
         | 
| 76 | 
            +
              :brand_id    => Proc.new{|v|v[:brand_id]},
         | 
| 77 | 
            +
              :shop_id     => Proc.new{|v|v[:shop_id]},
         | 
| 78 | 
            +
              :user_id     => Proc.new{|v|v['user_id']},
         | 
| 79 | 
            +
              :name        => Proc.new{|v|"#{v[:brand_id]}_#{v[:shop_id]}_#{v['user_id']}'s name"},
         | 
| 80 | 
            +
              :value1      => Proc.new{rand(10000)},
         | 
| 81 | 
            +
              :value_nil   => Proc.new{nil},
         | 
| 82 | 
            +
              :value_func  => Proc.new{"CONCAT('My', 'S', 'QL')".to_func}, #関数指定時はString#to_funcで
         | 
| 83 | 
            +
              :value_true  => Proc.new{true}, # true は 1 false は 0
         | 
| 84 | 
            +
              :value_time  => Proc.new{Time.mktime(2001,2,3,4,35,6)}, # Time,DateTime,Date
         | 
| 85 | 
            +
              :created_at  => Proc.new{'NOW()'.to_func},
         | 
| 86 | 
            +
            }
         | 
| 87 | 
            +
            #
         | 
| 88 | 
            +
            # 実際にテストデータを作成します
         | 
| 89 | 
            +
            # 引数はテーブル名、ループ、列に対する処理、になります
         | 
| 90 | 
            +
            #
         | 
| 91 | 
            +
            create_rows(
         | 
| 92 | 
            +
              'tests',
         | 
| 93 | 
            +
              loops,
         | 
| 94 | 
            +
              procs
         | 
| 95 | 
            +
            )
         | 
| 96 | 
            +
             | 
| 97 | 
            +
             | 
| 98 | 
            +
            # 以下作成結果のサンプルを出力しています
         | 
| 99 | 
            +
            puts '=' * 60
         | 
| 100 | 
            +
            puts query("SELECT count(id) AS cnt FROM tests").first['cnt'].to_s + "rows created"
         | 
| 101 | 
            +
            puts 'sample:'
         | 
| 102 | 
            +
            p query("SELECT * FROM tests WHERE brand_id = #{CNT_BRAND} AND shop_id = #{SHOP_PER_BRAND} AND user_id = #{USER_PER_SHOP}").first
         | 
| 103 | 
            +
             | 
| 104 | 
            +
            __END__
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            出力結果は以下な感じになります
         | 
| 107 | 
            +
             | 
| 108 | 
            +
            $ ruby example/sample1.rb
         | 
| 109 | 
            +
            ================   create rows for tests   =================
         | 
| 110 | 
            +
            100% |oooooooooooooooooooooooooooooooooooo| Time:   0:00:23
         | 
| 111 | 
            +
            ============================================================
         | 
| 112 | 
            +
            315945rows created
         | 
| 113 | 
            +
            sample:
         | 
| 114 | 
            +
            {"id"=>315945, "brand_id"=>21, "shop_id"=>15, "user_id"=>1003, "name"=>"21_15_1003's name", "value1"=>6704, "value_nil"=>nil, "value_func"=>"MySQL", "value_true"=>1, "value_time"=>2001-02-03 04:35:06 +0900, "created_at"=>2012-10-07 16:06:13 +0900}
         | 
| 115 | 
            +
             | 
| @@ -0,0 +1,180 @@ | |
| 1 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'progressbar'
         | 
| 4 | 
            +
            require 'mysql2'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            #
         | 
| 7 | 
            +
            # TODO 以下の削除 サンプルの作成
         | 
| 8 | 
            +
            #
         | 
| 9 | 
            +
            # 使い方
         | 
| 10 | 
            +
            #
         | 
| 11 | 
            +
            # ・まずは本ファイルをrequiresします
         | 
| 12 | 
            +
            #
         | 
| 13 | 
            +
            # ・setup_mysql_settingsでmysql2のclientを内部的に作成(@__clientで作成します)
         | 
| 14 | 
            +
            # ・あとはcreate_rowsメソッドを使ってください
         | 
| 15 | 
            +
            # ・基本的にインサート文のすべてのvalueにシングルクォーテーションで囲まれます。が、列名が「***_at」の場合は除く(もし特定の日時を入れたい場合は Proc.new{"'2001-10-14'"}のようにシングルクォーテーションを含めて設定してください
         | 
| 16 | 
            +
            # ・insert__per_rowsにてマルチプルインサートの単位が設定できます(デフォルトは100)
         | 
| 17 | 
            +
            #
         | 
| 18 | 
            +
            #  #
         | 
| 19 | 
            +
            #  # テーブル名
         | 
| 20 | 
            +
            #  # ループ達(配列で)
         | 
| 21 | 
            +
            #  # 各列の値の作成方法
         | 
| 22 | 
            +
            #  #
         | 
| 23 | 
            +
            #  create_rows(
         | 
| 24 | 
            +
            #    'm_pkey_or_index__index',
         | 
| 25 | 
            +
            #    [
         | 
| 26 | 
            +
            #      [:brand_id,(1..13)],
         | 
| 27 | 
            +
            #      [:user_id,(1..100)]
         | 
| 28 | 
            +
            #    ],
         | 
| 29 | 
            +
            #    :brand_id=>Proc.new{|v|v[:brand_id]},
         | 
| 30 | 
            +
            #    :user_id=>Proc.new{|v|v[:user_id]},
         | 
| 31 | 
            +
            #    :name=>Proc.new{|v|"#{v[:brand_id]}_#{v[:user_id]}_name"},
         | 
| 32 | 
            +
            #    :value1=>Proc.new{rand(10000)},
         | 
| 33 | 
            +
            #    :created_at=>Proc.new{'NOW()'},
         | 
| 34 | 
            +
            #  )
         | 
| 35 | 
            +
            #
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            class String
         | 
| 38 | 
            +
              def to_func
         | 
| 39 | 
            +
                @__is_function = true
         | 
| 40 | 
            +
                self
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              def function?
         | 
| 44 | 
            +
                if @__is_function
         | 
| 45 | 
            +
                  true
         | 
| 46 | 
            +
                else
         | 
| 47 | 
            +
                  false
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
            end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            module TestdataGeneraterForMysql
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              INSERT_PER_ROWS = 100
         | 
| 55 | 
            +
             | 
| 56 | 
            +
              def setup_mysql_client(hash)
         | 
| 57 | 
            +
                @__client = Mysql2::Client.new(hash)
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              def insert_per_rows(v)
         | 
| 61 | 
            +
                @__insert_per_rows = v
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
              def get_insert_per_rows
         | 
| 65 | 
            +
                @__insert_per_rows ||= INSERT_PER_ROWS
         | 
| 66 | 
            +
              end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
              def hide_progress_bar
         | 
| 69 | 
            +
                @__disable_progress_bar = true
         | 
| 70 | 
            +
              end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              def query(q)
         | 
| 73 | 
            +
                @__client.query(q)
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              def create_rows(table_name,loops,col_procs)
         | 
| 77 | 
            +
                if loops.size == 0
         | 
| 78 | 
            +
                  raise 'loops size must be bigger than 0'
         | 
| 79 | 
            +
                end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                total_rows = 0
         | 
| 82 | 
            +
                loops.each_with_index do |l,i|
         | 
| 83 | 
            +
                  if i == 0
         | 
| 84 | 
            +
                    total_rows = l[1].count
         | 
| 85 | 
            +
                  else
         | 
| 86 | 
            +
                    total_rows *= l[1].count
         | 
| 87 | 
            +
                  end
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
                @__table_name = table_name
         | 
| 90 | 
            +
                raise 'something wrong' if @__insert_values && @__insert_values.size > 0
         | 
| 91 | 
            +
                @__insert_values = []
         | 
| 92 | 
            +
                @__inserted_rows = 0
         | 
| 93 | 
            +
                @__col_procs = col_procs
         | 
| 94 | 
            +
                unless @__disable_progress_bar
         | 
| 95 | 
            +
                  title = "create rows for #{table_name}"
         | 
| 96 | 
            +
                  width = 60
         | 
| 97 | 
            +
                  $stderr.puts title.center(title.size + 6,' ').center(width,'=')
         | 
| 98 | 
            +
                  @__pbar = ProgressBar.new('', total_rows, $stderr)
         | 
| 99 | 
            +
                  @__pbar.format_arguments = [:percentage, :bar, :stat] 
         | 
| 100 | 
            +
                  @__pbar.format = "%3d%% %s %s"
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                looping(loops,0)
         | 
| 104 | 
            +
                do_insert # あまりの作成
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                if @__pbar
         | 
| 107 | 
            +
                  @__pbar.finish
         | 
| 108 | 
            +
                end
         | 
| 109 | 
            +
              end
         | 
| 110 | 
            +
             | 
| 111 | 
            +
              def looping(ar,index,values={})
         | 
| 112 | 
            +
                ar[index][1].each do |i|
         | 
| 113 | 
            +
                  values[ar[index][0]] = i
         | 
| 114 | 
            +
                  next_index = index + 1
         | 
| 115 | 
            +
                  if ar.size > next_index
         | 
| 116 | 
            +
                    looping(ar,next_index,values)
         | 
| 117 | 
            +
                  elsif ar.size == next_index
         | 
| 118 | 
            +
                    hash = {}
         | 
| 119 | 
            +
                    @__col_procs.each do |key,proc|
         | 
| 120 | 
            +
                      hash[key] = proc.call(values)
         | 
| 121 | 
            +
                    end
         | 
| 122 | 
            +
                    set_insert_values(hash)
         | 
| 123 | 
            +
                  end
         | 
| 124 | 
            +
                end
         | 
| 125 | 
            +
              end
         | 
| 126 | 
            +
             | 
| 127 | 
            +
              def set_insert_values(hash)
         | 
| 128 | 
            +
                @__insert_values ||= []
         | 
| 129 | 
            +
                @__insert_values << hash
         | 
| 130 | 
            +
                if @__insert_values.size > get_insert_per_rows
         | 
| 131 | 
            +
                  do_insert
         | 
| 132 | 
            +
                end
         | 
| 133 | 
            +
              end
         | 
| 134 | 
            +
             | 
| 135 | 
            +
              def do_insert
         | 
| 136 | 
            +
                return if @__insert_values.size == 0
         | 
| 137 | 
            +
                @__inserted_rows += @__insert_values.size
         | 
| 138 | 
            +
                query = <<"EOS"
         | 
| 139 | 
            +
            INSERT INTO `#{@__client.escape(@__table_name)}`
         | 
| 140 | 
            +
            (#{@__col_procs.keys.map{|o|"`#{@__client.escape(o.to_s)}`"}.join(',')})
         | 
| 141 | 
            +
            VALUES
         | 
| 142 | 
            +
            #{
         | 
| 143 | 
            +
              @__insert_values.map do |row|
         | 
| 144 | 
            +
              "(#{
         | 
| 145 | 
            +
                row.map do |key,value|
         | 
| 146 | 
            +
                  case value
         | 
| 147 | 
            +
                  when nil
         | 
| 148 | 
            +
                    "NULL"
         | 
| 149 | 
            +
                  when TrueClass,FalseClass
         | 
| 150 | 
            +
                    if value
         | 
| 151 | 
            +
                      "'1'"
         | 
| 152 | 
            +
                    else
         | 
| 153 | 
            +
                      "'0'"
         | 
| 154 | 
            +
                    end
         | 
| 155 | 
            +
                  # TODO when datetime time
         | 
| 156 | 
            +
                  when Time,DateTime
         | 
| 157 | 
            +
                    "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
         | 
| 158 | 
            +
                  when Date
         | 
| 159 | 
            +
                    "'#{value.strftime("%Y-%m-%d")}'"
         | 
| 160 | 
            +
                  else
         | 
| 161 | 
            +
                    s = value
         | 
| 162 | 
            +
                    s = s.to_s unless s.kind_of?(String)
         | 
| 163 | 
            +
                    if s.respond_to?(:function?) && s.function?
         | 
| 164 | 
            +
                      "#{value.to_s}"
         | 
| 165 | 
            +
                    else
         | 
| 166 | 
            +
                      "'#{@__client.escape(value.to_s)}'"
         | 
| 167 | 
            +
                    end
         | 
| 168 | 
            +
                  end
         | 
| 169 | 
            +
                end.join(',')
         | 
| 170 | 
            +
              })"
         | 
| 171 | 
            +
              end.join(',')
         | 
| 172 | 
            +
            }
         | 
| 173 | 
            +
            EOS
         | 
| 174 | 
            +
                @__client.query(query)
         | 
| 175 | 
            +
                if @__pbar
         | 
| 176 | 
            +
                  @__pbar.inc(@__insert_values.size)
         | 
| 177 | 
            +
                end
         | 
| 178 | 
            +
                @__insert_values = []
         | 
| 179 | 
            +
              end
         | 
| 180 | 
            +
            end
         | 
    
        data/test/test.rb
    ADDED
    
    | @@ -0,0 +1,186 @@ | |
| 1 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            $: << File.dirname(__FILE__)
         | 
| 4 | 
            +
            require 'test_helper'
         | 
| 5 | 
            +
            require 'test/unit'
         | 
| 6 | 
            +
            require 'Fileutils'
         | 
| 7 | 
            +
            require 'date'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            #
         | 
| 10 | 
            +
            # localhostにtestdata_generater_for_mysql_testというデータベースを作成し
         | 
| 11 | 
            +
            # rootのパスワードなしでアクセスできるようにしておいてください
         | 
| 12 | 
            +
            #
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            include TestdataGeneraterForMysql
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            class TestTestdataGeneraterForMysql < Test::Unit::TestCase
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              CNT_BRAND      =  5
         | 
| 19 | 
            +
              SHOP_PER_BRAND =  6
         | 
| 20 | 
            +
              USER_PER_SHOP  = 22
         | 
| 21 | 
            +
              STAMP_PER_USER =  3
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              def setup
         | 
| 24 | 
            +
                hide_progress_bar
         | 
| 25 | 
            +
                setup_mysql_client :host => "127.0.0.1", :username => "root",:database=>'testdata_generater_for_mysql_test'
         | 
| 26 | 
            +
                insert_per_rows 29
         | 
| 27 | 
            +
                query "DROP TABLE IF EXISTS tests;"
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              def assert_count(should_be ,where=nil, message=nil)
         | 
| 31 | 
            +
                where = where ? "WHERE #{where}" : ''
         | 
| 32 | 
            +
                where_st = where
         | 
| 33 | 
            +
                where_st = 'COUNT ALL' if where.size == 0
         | 
| 34 | 
            +
                count = query("SELECT COUNT(*) AS cnt FROM tests #{where}").first['cnt']
         | 
| 35 | 
            +
                message = build_message message, "'#{where}' should be #{should_be},but #{count}", where
         | 
| 36 | 
            +
                assert_block message do
         | 
| 37 | 
            +
                  count == should_be
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              def test_2_loops
         | 
| 42 | 
            +
                # テーブル作成
         | 
| 43 | 
            +
                query "
         | 
| 44 | 
            +
            CREATE TABLE tests (
         | 
| 45 | 
            +
              `id`          int(11) NOT NULL auto_increment,
         | 
| 46 | 
            +
              `brand_id`    int(11) NOT NULL,
         | 
| 47 | 
            +
              `shop_id`     int(11) NOT NULL,
         | 
| 48 | 
            +
              `name`        varchar(20) NOT NULL,
         | 
| 49 | 
            +
              `value1`      int(11) NOT NULL,
         | 
| 50 | 
            +
              `value2`      int(11) ,
         | 
| 51 | 
            +
              `value3`      int(11) ,
         | 
| 52 | 
            +
              `value_nil`   int(11) ,
         | 
| 53 | 
            +
              `value_func`  varchar(20),
         | 
| 54 | 
            +
              `value_true`  tinyint(1) ,
         | 
| 55 | 
            +
              `value_false` tinyint(1) ,
         | 
| 56 | 
            +
              `value_date`  date ,
         | 
| 57 | 
            +
              `value_dtime` datetime ,
         | 
| 58 | 
            +
              `value_time`  datetime ,
         | 
| 59 | 
            +
              `created_at`  datetime ,
         | 
| 60 | 
            +
              PRIMARY KEY  (`id`),
         | 
| 61 | 
            +
              KEY `idx01` USING BTREE (`brand_id`,`shop_id`)
         | 
| 62 | 
            +
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
         | 
| 63 | 
            +
            "
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                # データ作成
         | 
| 66 | 
            +
                loops = [
         | 
| 67 | 
            +
                  [:brand_id, (1..CNT_BRAND)],
         | 
| 68 | 
            +
                  [:shop_id,  (1..SHOP_PER_BRAND)]
         | 
| 69 | 
            +
                ]
         | 
| 70 | 
            +
                procs = {
         | 
| 71 | 
            +
                  :brand_id    => Proc.new{|v|v[:brand_id]},
         | 
| 72 | 
            +
                  :shop_id     => Proc.new{|v|v[:shop_id]},
         | 
| 73 | 
            +
                  :name        => Proc.new{|v|"#{v[:brand_id]}_#{v[:shop_id]}'s name"},
         | 
| 74 | 
            +
                  :value1      => Proc.new{rand(10000)},
         | 
| 75 | 
            +
                  :value2      => Proc.new{rand(10000)},
         | 
| 76 | 
            +
                  :value_nil   => Proc.new{nil},
         | 
| 77 | 
            +
                  :value_func  => Proc.new{"CONCAT('My', 'S', 'QL')".to_func},
         | 
| 78 | 
            +
                  :value_true  => Proc.new{true},
         | 
| 79 | 
            +
                  :value_false => Proc.new{false},
         | 
| 80 | 
            +
                  :value_date  => Proc.new{Date.new(2001,2,3)},
         | 
| 81 | 
            +
                  :value_dtime => Proc.new{DateTime.new(2001,2,3,4,35,6)},
         | 
| 82 | 
            +
                  :value_time  => Proc.new{Time.mktime(2001,2,3,4,35,6)},
         | 
| 83 | 
            +
                  :created_at  => Proc.new{'NOW()'.to_func},
         | 
| 84 | 
            +
                }
         | 
| 85 | 
            +
                create_rows(
         | 
| 86 | 
            +
                  'tests',
         | 
| 87 | 
            +
                  loops,
         | 
| 88 | 
            +
                  procs
         | 
| 89 | 
            +
                )
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                # 作成データのチェック
         | 
| 92 | 
            +
                cnt_all = CNT_BRAND * SHOP_PER_BRAND
         | 
| 93 | 
            +
                assert_count cnt_all
         | 
| 94 | 
            +
                assert_count SHOP_PER_BRAND, "brand_id = 3"
         | 
| 95 | 
            +
                assert_count CNT_BRAND,      "shop_id = 3"
         | 
| 96 | 
            +
                assert_count 0,              "value2 IS NULL"
         | 
| 97 | 
            +
                assert_count cnt_all,        "value3 IS NULL"
         | 
| 98 | 
            +
                assert_count cnt_all,        "value_nil IS NULL"
         | 
| 99 | 
            +
                assert_count cnt_all,        "value_func = 'MySQL'"
         | 
| 100 | 
            +
                assert_count 0,              "created_at IS NULL"
         | 
| 101 | 
            +
                assert_count 1,              "name = '1_1''s name'"
         | 
| 102 | 
            +
                assert_count cnt_all,        "value_true = 1"
         | 
| 103 | 
            +
                assert_count cnt_all,        "value_false = 0"
         | 
| 104 | 
            +
                assert_count cnt_all,        "value_date = '2001-02-03'"
         | 
| 105 | 
            +
                assert_count cnt_all,        "value_dtime = '2001-02-03 04:35:06'"
         | 
| 106 | 
            +
                assert_count cnt_all,        "value_time = '2001-02-03 04:35:06'"
         | 
| 107 | 
            +
              end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
              def test_4_loops
         | 
| 110 | 
            +
                # テーブル作成
         | 
| 111 | 
            +
                query "
         | 
| 112 | 
            +
            CREATE TABLE tests (
         | 
| 113 | 
            +
              `id`          int(11) NOT NULL auto_increment,
         | 
| 114 | 
            +
              `brand_id`    int(11) NOT NULL,
         | 
| 115 | 
            +
              `shop_id`     int(11) NOT NULL,
         | 
| 116 | 
            +
              `user_id`     int(11) NOT NULL,
         | 
| 117 | 
            +
              `stamp_id`    int(11) NOT NULL,
         | 
| 118 | 
            +
              `name`        varchar(20) NULL,
         | 
| 119 | 
            +
              `value1`      int(11) NOT NULL,
         | 
| 120 | 
            +
              `value2`      int(11) ,
         | 
| 121 | 
            +
              `value3`      int(11) ,
         | 
| 122 | 
            +
              `value_nil`   int(11) ,
         | 
| 123 | 
            +
              `value_func`  varchar(20),
         | 
| 124 | 
            +
              `value_true`  tinyint(1) ,
         | 
| 125 | 
            +
              `value_false` tinyint(1) ,
         | 
| 126 | 
            +
              `value_date`  date ,
         | 
| 127 | 
            +
              `value_dtime` datetime ,
         | 
| 128 | 
            +
              `value_time`  datetime ,
         | 
| 129 | 
            +
              `created_at`  datetime NULL,
         | 
| 130 | 
            +
              PRIMARY KEY  (`id`),
         | 
| 131 | 
            +
              KEY `idx01` USING BTREE (`brand_id`,`shop_id`,`user_id`,`stamp_id`)
         | 
| 132 | 
            +
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
         | 
| 133 | 
            +
            "
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                # データ作成
         | 
| 136 | 
            +
                loops = [
         | 
| 137 | 
            +
                  [:brand_id, (1..CNT_BRAND)],
         | 
| 138 | 
            +
                  [:shop_id,  (1..SHOP_PER_BRAND)],
         | 
| 139 | 
            +
                  [:user_id,  (1..USER_PER_SHOP)],
         | 
| 140 | 
            +
                  [:stamp_id, (1..STAMP_PER_USER)],
         | 
| 141 | 
            +
                ]
         | 
| 142 | 
            +
                procs = {
         | 
| 143 | 
            +
                  :brand_id    => Proc.new{|v|v[:brand_id]},
         | 
| 144 | 
            +
                  :shop_id     => Proc.new{|v|v[:shop_id]},
         | 
| 145 | 
            +
                  :user_id     => Proc.new{|v|v[:user_id]},
         | 
| 146 | 
            +
                  :stamp_id    => Proc.new{|v|v[:stamp_id]},
         | 
| 147 | 
            +
                  :name        => Proc.new{|v|"#{v[:brand_id]}_#{v[:shop_id]}_#{v[:user_id]}_#{v[:stamp_id]}'s name"},
         | 
| 148 | 
            +
                  :value1      => Proc.new{rand(10000)},
         | 
| 149 | 
            +
                  :value2      => Proc.new{rand(10000)},
         | 
| 150 | 
            +
                  :value_nil   => Proc.new{nil},
         | 
| 151 | 
            +
                  :value_func  => Proc.new{"CONCAT('My', 'S', 'QL')".to_func},
         | 
| 152 | 
            +
                  :value_true  => Proc.new{true},
         | 
| 153 | 
            +
                  :value_false => Proc.new{false},
         | 
| 154 | 
            +
                  :value_date  => Proc.new{Date.new(2001,2,3)},
         | 
| 155 | 
            +
                  :value_dtime => Proc.new{DateTime.new(2001,2,3,4,35,6)},
         | 
| 156 | 
            +
                  :value_time  => Proc.new{Time.mktime(2001,2,3,4,35,6)},
         | 
| 157 | 
            +
                  :created_at  => Proc.new{'NOW()'.to_func},
         | 
| 158 | 
            +
                }
         | 
| 159 | 
            +
                create_rows(
         | 
| 160 | 
            +
                  'tests',
         | 
| 161 | 
            +
                  loops,
         | 
| 162 | 
            +
                  procs
         | 
| 163 | 
            +
                )
         | 
| 164 | 
            +
             | 
| 165 | 
            +
                # 作成データのチェック
         | 
| 166 | 
            +
                cnt_all = CNT_BRAND * SHOP_PER_BRAND * USER_PER_SHOP * STAMP_PER_USER
         | 
| 167 | 
            +
                cnt_per_brand = cnt_all / CNT_BRAND
         | 
| 168 | 
            +
                cnt_per_user  = cnt_all / USER_PER_SHOP
         | 
| 169 | 
            +
                assert_count cnt_all
         | 
| 170 | 
            +
                assert_count cnt_per_brand, "brand_id = 3"
         | 
| 171 | 
            +
                assert_count cnt_per_user,  "user_id = 3"
         | 
| 172 | 
            +
                assert_count 0,             "value2 IS NULL"
         | 
| 173 | 
            +
                assert_count cnt_all,       "value3 IS NULL"
         | 
| 174 | 
            +
                assert_count cnt_all,       "value_nil IS NULL"
         | 
| 175 | 
            +
                assert_count cnt_all,       "value_func = 'MySQL'"
         | 
| 176 | 
            +
                assert_count 0,             "created_at IS NULL"
         | 
| 177 | 
            +
                assert_count 1,             "name = '1_1_1_1''s name'"
         | 
| 178 | 
            +
                assert_count cnt_all,       "value_true = 1"
         | 
| 179 | 
            +
                assert_count cnt_all,       "value_false = 0"
         | 
| 180 | 
            +
                assert_count cnt_all,       "value_date = '2001-02-03'"
         | 
| 181 | 
            +
                assert_count cnt_all,       "value_dtime = '2001-02-03 04:35:06'"
         | 
| 182 | 
            +
                assert_count cnt_all,       "value_time = '2001-02-03 04:35:06'"
         | 
| 183 | 
            +
                row = query("SELECT * FROM tests WHERE name = '1_1_1_1''s name'").first
         | 
| 184 | 
            +
                assert row['brand_id'] == 1 && row["shop_id"] == 1 && row["user_id"] == 1 && row["stamp_id"] == 1
         | 
| 185 | 
            +
              end
         | 
| 186 | 
            +
            end
         | 
    
        data/test/test_helper.rb
    ADDED
    
    
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
            $:.push File.expand_path("../lib", __FILE__)
         | 
| 3 | 
            +
            require "testdata_generater_for_mysql/version"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Gem::Specification.new do |s|
         | 
| 6 | 
            +
              s.name        = "testdata_generater_for_mysql"
         | 
| 7 | 
            +
              s.version     = TestdataGeneraterForMysql::VERSION
         | 
| 8 | 
            +
              s.authors     = ["pacojp"]
         | 
| 9 | 
            +
              s.email       = ["paco.jp@gmail.com"]
         | 
| 10 | 
            +
              s.homepage    = "https://github.com/pacojp/testdata_generater_for_mysql"
         | 
| 11 | 
            +
              s.summary     = %q{oreore mysql test data generater}
         | 
| 12 | 
            +
              s.description = %q{oreore mysql test data generater}
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              s.rubyforge_project = "testdata_generater_for_mysql"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              s.files         = `git ls-files`.split("\n")
         | 
| 17 | 
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 18 | 
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 19 | 
            +
              s.require_paths = ["lib"]
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              # specify any dependencies here; for example:
         | 
| 22 | 
            +
              # s.add_development_dependency "rspec"
         | 
| 23 | 
            +
              # s.add_runtime_dependency "rest-client"
         | 
| 24 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,59 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: testdata_generater_for_mysql
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - pacojp
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2012-10-07 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies: []
         | 
| 14 | 
            +
            description: oreore mysql test data generater
         | 
| 15 | 
            +
            email:
         | 
| 16 | 
            +
            - paco.jp@gmail.com
         | 
| 17 | 
            +
            executables: []
         | 
| 18 | 
            +
            extensions: []
         | 
| 19 | 
            +
            extra_rdoc_files: []
         | 
| 20 | 
            +
            files:
         | 
| 21 | 
            +
            - .gitignore
         | 
| 22 | 
            +
            - Gemfile
         | 
| 23 | 
            +
            - README.md
         | 
| 24 | 
            +
            - Rakefile
         | 
| 25 | 
            +
            - example/sample1.rb
         | 
| 26 | 
            +
            - lib/testdata_generater_for_mysql.rb
         | 
| 27 | 
            +
            - lib/testdata_generater_for_mysql/core.rb
         | 
| 28 | 
            +
            - lib/testdata_generater_for_mysql/version.rb
         | 
| 29 | 
            +
            - test/test.rb
         | 
| 30 | 
            +
            - test/test_helper.rb
         | 
| 31 | 
            +
            - testdata_generater_for_mysql.gemspec
         | 
| 32 | 
            +
            homepage: https://github.com/pacojp/testdata_generater_for_mysql
         | 
| 33 | 
            +
            licenses: []
         | 
| 34 | 
            +
            post_install_message: 
         | 
| 35 | 
            +
            rdoc_options: []
         | 
| 36 | 
            +
            require_paths:
         | 
| 37 | 
            +
            - lib
         | 
| 38 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 39 | 
            +
              none: false
         | 
| 40 | 
            +
              requirements:
         | 
| 41 | 
            +
              - - ! '>='
         | 
| 42 | 
            +
                - !ruby/object:Gem::Version
         | 
| 43 | 
            +
                  version: '0'
         | 
| 44 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 45 | 
            +
              none: false
         | 
| 46 | 
            +
              requirements:
         | 
| 47 | 
            +
              - - ! '>='
         | 
| 48 | 
            +
                - !ruby/object:Gem::Version
         | 
| 49 | 
            +
                  version: '0'
         | 
| 50 | 
            +
            requirements: []
         | 
| 51 | 
            +
            rubyforge_project: testdata_generater_for_mysql
         | 
| 52 | 
            +
            rubygems_version: 1.8.23
         | 
| 53 | 
            +
            signing_key: 
         | 
| 54 | 
            +
            specification_version: 3
         | 
| 55 | 
            +
            summary: oreore mysql test data generater
         | 
| 56 | 
            +
            test_files:
         | 
| 57 | 
            +
            - test/test.rb
         | 
| 58 | 
            +
            - test/test_helper.rb
         | 
| 59 | 
            +
            has_rdoc: 
         |