1 # -*- coding: utf-8 -*-
2 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
7 describe ServiceCooperation do
8 fixtures :service_cooperations
11 describe "validateのテスト" do
13 @service = service_cooperations(:one)
16 @service.should be_valid
20 @service.should_not be_valid
22 it "サービス名称の文字数チェック" do
23 @service.name = "a"*200
24 @service.should be_valid
25 @service.name = "a"*201
26 @service.should_not be_valid
29 @service.url_file_name = nil
30 @service.should_not be_valid
32 it "URL取得用文字列の文字数チェック" do
33 @service.url_file_name = 'a'*30
34 @service.should be_valid
35 @service.url_file_name = 'a'*31
36 @service.should_not be_valid
38 valid_url_file_name = [
48 valid_url_file_name.each do |words|
49 it "許可ファイル名のテスト:"+words do
50 @service.url_file_name = words
51 @service.should be_valid
54 non_valid_url_file_name = [
64 non_valid_url_file_name.each do |words|
65 it "禁止ファイル名のテスト:"+words do
66 @service.url_file_name = words
67 @service.should_not be_valid
70 it "URL取得用文字列に使えるのは英数字と'_'です" do
71 @service.url_file_name = "abc_123"
72 @service.should be_valid
73 @service.url_file_name = "あいうえお"
74 @service.should_not be_valid
76 it "ファイル形式の範囲外参照チェック" do
77 @service.file_type = -1
78 @service.should_not be_valid
80 it "エンコードタイプの範囲外参照チェック" do
82 @service.should_not be_valid
84 it "改行文字の範囲外参照チェック" do
85 @service.newline_character = -1
86 @service.should_not be_valid
90 @service.should_not be_valid
94 "select alterer from anonymouse",
95 "select altalter from anonymouse",
96 "select alter_id from anonymouse",
97 "select alter,delete from anonymouse",
98 "select alter,delete,create form anonymouse",
99 "select alter,delete,create,drop,analyze,commit,copy,end form anolyze",
100 "SELECT actual from non_actual order by id",
101 "select test from alterer",
102 "select test from tes_alterer",
103 "select delete_at from products",
104 "SELECT create_at FROM non_actual",
105 "SELECT test from non_actual order by id",
106 "SELECT name,id FROM products WHERE id > 5 ORDER BY id",
107 "select delete,create,alter,end from non_actual where id < 10 order by id",
108 "select commit_table,newline_character FROM test",
109 "SELECT template_name,service_name,url_file_name,file_type,encode,newline_character,sql,field_items FROM service_cooperations_templates"
112 valid_sql_words.each do |words|
113 it "SQL許可コマンドテスト:"+words do
115 @service.should be_valid
119 non_valid_sql_words = [
120 "ALTER INDEX name RENAME TO super_name FROM products",
121 "alter index name rename to super_name from products",
122 "CREATE TABLE actual",
123 "create table actual",
124 "CREATE ROLE jonathan LOGIN",
125 "create role jonathan login",
126 "CREATE UNIQUE INDEX title_idx ON films(title)",
127 "create unique index code_idx ON films(code) TABLESPACE indexspace",
128 "CREATE superman name SUPREUSER",
129 "create superman name superuser",
131 "drop function sqrt(integer)",
132 "DROP ROLE jonathan",
135 "analyze non_actual",
136 "ANALYZE non_actual",
138 "COMMIT PREPARED 'foober'",
140 "COPY name FROM products ORDER BY id",
141 "copy name from products order by id"
144 non_valid_sql_words.each do |words|
145 it "SQL禁止コマンドテスト:"+words do
147 @service.should_not be_valid
152 @service.sql = 'a'*2000
153 @service.should be_valid
156 @service.field_items = nil
157 @service.should_not be_valid
159 it "アイテムフィールドの文字数チェック" do
160 @service.field_items = "a"*300
161 @service.should be_valid
166 it "select_file_type メソッドのチェック" do
167 output = [ ["CSV", 0],["TSV",1] ]
168 ServiceCooperation.select_file_type.sort{ |a,b|
172 it "select_encode メソッドのチェック" do
173 output = [ ["UTF-8", 0],["SHIFT-JIS",1],["EUC",2],["JIS",3] ]
174 ServiceCooperation.select_encode.sort{ |a,b|
178 it "select_newline_character メソッドのチェック" do
179 output = [ ["CR",0], ["LF",1], ["CR+LF",2] ]
180 ServiceCooperation.select_newline_character.sort{ |a,b|
185 describe "get_filename メソッド" do
187 @service = service_cooperations(:one)
190 @service.url_file_name = "test"
191 @service.file_type = 0
192 @service.get_filename.should == "test.CSV"
195 describe "file_generate メソッド" do
197 @service = service_cooperations(:one)
201 @service.field_items = "name,id"
202 @service.file_type = 0
203 @service.newline_character = 1
208 @service.sql = "SELECT * FROM no_actual"
209 @service.file_generate.should be_nil
211 @service.sql = "SELECT anonymouse FROM admin_users"
212 @service.file_generate.should be_nil
214 @service.sql = "SEEEELCT id,name FROM products"
215 @service.file_generate.should be_nil
219 describe "CSV-TSV" do
221 @service.sql = "SELECT name,id FROM products WHERE id < 3 ORDER BY id"
222 @service.field_items = "name,id"
223 @service.file_type = 0
224 @service.newline_character = 1
226 open(File.dirname(__FILE__) + "/../csv/service_cooperations_sample.csv") { |f|
227 @service.file_generate.should eql(NKF.nkf('-w8', f.read))
233 @service.sql = "SELECT name,id FROM products WHERE id < 3 ORDER BY id"
234 @service.field_items = "name,id"
235 @service.file_type = 0
236 @service.newline_character = 1
239 Tempfile.open("test") do |temp|
240 temp.puts @service.file_generate
243 open(File.dirname(__FILE__) + "/../csv/service_cooperations_sample.csv") do |f|
244 Digest::MD5.new.update(temp.read).should == (Digest::MD5.new.update(NKF.nkf('-w8',f.read)))
252 @service.sql = "SELECT name,id FROM products WHERE id < 3 ORDER BY id"
253 @service.field_items = "name,id"
254 @service.file_type = 1
255 @service.newline_character = 2
257 Tempfile.open("test") do |temp|
258 temp.puts @service.file_generate
261 open(File.dirname(__FILE__) + "/../csv/service_cooperations_sample.tsv") do |f|
262 Digest::MD5.new.update(temp.read).should == (Digest::MD5.new.update(NKF.nkf('-s',f.read)))
269 it "EUCに正しく変換できるか" do
271 NKF.guess(@service.file_generate).should eql(NKF::EUC)
273 it "UTF-8に正しく変換できるか" do
275 NKF.guess(@service.file_generate).should eql(NKF::UTF8)
277 it "Shift-JISに正しく変換できるか" do
279 NKF.guess(@service.file_generate).should eql(NKF::SJIS)
282 it "CR+LFの改行コードのテスト" do
283 @service.newline_character = 2
284 @service.file_generate.should match(/\r\n/)
287 @service.newline_character = 1
288 @service.file_generate.should_not match(/\r\n/)
289 @service.file_generate.should_not match(/\r/)
290 @service.file_generate.should match(/\n/)
293 @service.newline_character = 0
294 @service.file_generate.should_not match(/\r\n/)
295 @service.file_generate.should match(/\r/)
296 @service.file_generate.should_not match(/\n/)