ラベル test の投稿を表示しています。 すべての投稿を表示
ラベル test の投稿を表示しています。 すべての投稿を表示

2010/10/06

Test::Fixture::DBI::Console

作成の動機

Test::Fixture::DBIには既存のDBからテーブル定義と中身をdumpするツールが付属しています.

これを使うとさくっとテスト用のfixtureが生成できます.

生成したFixureを書き換えて使ってもいいのですが,yamlだと量が増えたときに閲覧性が低いと感じました.


そこで使い慣れたmysqlのconsoleっぽく見れると便利なんじゃないかと思い,Test::Fixutre::DBI::Consoleを作成しました.


追記
これは内部でTest::mysqldを使ってfixutre作成専門のmysqldを立ち上げます.


配布場所

現在はgithubのみです.PAUSE IDが貰えたらCPANも考えます.

Test::Fixutre::DBI::Console 

使い方

Test::Fixture::DBI::Consoleをinstallすると"test_fixture_console"コマンドもinstallされます.

このコマンドでshellに入ります.


まずはテーブル定義とデータを生成します.
yoshi@mb yoshi% test_fixture_console mysql
fixture> create table hoge (id int primary key auto_increment); 
Affected 0 rows
fixture> show tables;
+----------------+
| Tables_in_test |
+----------------+
| hoge           |
+----------------+
Affected 1 rows
fixture> insert hoge (id) values (1),(2),(3);
Affected 3 rows
fixture> select * from hoge;
+----+
| id |
+----+
| 1  |
| 2  |
| 3  |
+----+
Affected 3 rows

# DBと中身をyamlに書き出し
fixture> make_database test1.yaml
Create database schema file: test1.yaml
fixture> make_fixture test1_fixture.yaml
Create fixture file: test1_fixture.yaml


先ほど書き出したyamlの中身
yoshi@mb yoshi% cat test1.yaml
---
- 
  data: "CREATE TABLE `hoge` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  PRIMARY KEY (`id`)\n) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1"
  schema: hoge
yoshi@mb yoshi% cat test1_fixture.yaml
---
- 
  data:
    id: 1
  name: 1
  schema: hoge
- 
  data:
    id: 2
  name: 2
  schema: hoge
- 
  data:
    id: 3
  name: 3
  schema: hoge

test1.yamlとtest1_fixture.yamlをloadしてみます.
yoshi@mb yoshi% test_fixture_console mysql
fixture> show tables;
+----------------+
| Tables_in_test |
+----------------+
+----------------+
Affected 0 rows
fixture> construct_database test1.yaml
Load database schema from test1.yaml
fixture> show tables;
+----------------+
| Tables_in_test |
+----------------+
| hoge           |
+----------------+
Affected 1 rows
fixture> construct_fixture test1_fixture.yaml
Load fixture from test1_fixture.yaml
fixture> select * from hoge;
+----+
| id |
+----+
| 1  |
| 2  |
| 3  |
+----+
Affected 3 rows

TODO

  • テスト足りなすぎ
  • PODもちゃんと書く
  • SQLiteでも動かしたい
  • モジュール名はこれでいいのか
  • 明らかにリファクタリングが必要

2010/08/24

HTTP::Request::AsCGIでCGIのテスト

HTTP::Request::AsCGIとHTTP::Requestを使うとCGIのテスト時に環境変数等をよしなにセットアップしてくれる。
前に使ったことがあったはずなのに、モジュール名が全然思い出せなかったのでメモ。

以下、使い方
use strict;
use warnings;
use HTTP::Request::AsCGI;
use HTTP::Request::Common;
use CGI;
use Test::More;
{
    my $req = POST 'http://example.com/',
        Content_Type => 'form-data',
        Content      => [ file => ['t/test.gif'] ];
    my $c = HTTP::Request::AsCGI->new($req)->setup;
    my $q = CGI->new;
    my $filename = $q->param('file');
    is( $filename, 't/test.gif', 'filename ok' );
}

done_testing;