Created
December 10, 2014 09:10
-
-
Save ZhouMeichen/ab54edf288ba8f393279 to your computer and use it in GitHub Desktop.
接口测试脚本(WebService+CSV in Windows)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1、安装Rubyinstaller for windows | |
| 前往“http://rubyinstaller.org/downloads/”网站下载RubyInstaller for Windows。一般,请下载1.9.3以上版本。按照提示安装即可,其中需要勾选“Add Ruby executables to your PATH”选项。 | |
| ##################################################################### | |
| 2、安装gem | |
| 此工具所需gem包含:tiny_tds。可直接在命令行中联网安装,打开“cmd”,输入“gem install tiny_tds”。若联网安装失败,可使用已下载的“tiny_tds-0.6.2.gem”,在命令行中输入“gem install 路径/tiny_tds-0.6.2.gem”进行安装。 | |
| ##################################################################### | |
| 3、根据具体测试环境修改conf文件夹中的config.yml | |
| (1)method1: | |
| connect_username:数据库登陆名 | |
| connect_password:数据库登陆密码 | |
| connect_host:数据库所在服务器ip | |
| connect_database:数据库名称 | |
| connect_encode:数据库编码 | |
| telnet_host:.bat文件所在服务器 | |
| telnet_port:.bat文件所在服务器的telnet端口号,一般为3001 | |
| telnet_user:.bat文件所在服务器登陆用户名 | |
| telnet_pwd:.bat文件所在服务器登陆密码 | |
| (2)method2: | |
| source_server: | |
| connect_username:源数据库登陆名 | |
| connect_password:源数据库登陆密码 | |
| connect_host:源数据库所在服务器ip | |
| connect_database:源数据库名称 | |
| connect_encode:源数据库编码 | |
| linked_server: | |
| connect_username:连接数据库登陆名 | |
| connect_password:连接数据库登陆密码 | |
| connect_host:连接数据库所在服务器ip | |
| connect_database:连接数据库名称 | |
| connect_encode:连接数据库编码 | |
| ##################################################################### | |
| 4、根据具体需求编写run_XX.rb文件 | |
| 其中,需要定制的包括:sql、共享文件夹路径、.bat文件路径、csv格式规则 | |
| ##################################################################### | |
| 5、在命令行中运行编写的文件,打开“cmd”,输入“ruby 路径/run_XX.rb”。也可以使用Eclipse、NetBeans和Aptana等IDE运行。 | |
| ##################################################################### | |
| 该脚本共包含2个功能: | |
| 1、从数据库选取所需数据,生成csv文件,并保存在指定远程共享文件夹中,接着运行远程目录下的.bat文件自动启动接口。 | |
| 2、不同服务器上不同数据库之间的数据交互 (提供两种方案:a、直接sql脚本建立linked server,方法简单,建议使用;b、使用我提供的类和方法)。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| method1: | |
| connect_username: sa | |
| connect_password: pwd | |
| connect_host: 172.29.140.157 | |
| connect_database: DB_Test | |
| connect_encode: GBK | |
| telnet_host: 172.29.131.102 | |
| telnet_port: 3001 | |
| telnet_user: zhou_meichen | |
| telnet_pwd: pwd | |
| method2: | |
| source_server: | |
| connect_username: sa | |
| connect_password: pwd | |
| connect_host: 172.29.140.157 | |
| connect_database: DB_Test | |
| connect_encode: GBK | |
| linked_server: | |
| connect_username: sa | |
| connect_password: pwd | |
| connect_host: 172.29.140.156 | |
| connect_database: DB | |
| connect_encode: GBK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #encoding: utf-8 | |
| require "method" | |
| m = Method1.new | |
| #begin-选取数据,并生成csv | |
| m.sql = "select Balance,LEName,LECode from T_BK_Account a join T_BK_AccountBalance b on a.ID = b.AccountID" | |
| #保存CSV至共享文件夹,路径需自行定制 | |
| CSV.open("//172.29.140.157/test/csv.csv", "w") do |csv| #perem: a add | |
| m.csvResult.each do |row| | |
| csv << ["#{row['Balance'].to_f.to_i}","#{row['LECode'].to_s.encode("GBK")}","#{row['LEName'].to_s.encode("GBK") }"] #定制CSV展示格式 | |
| end | |
| end | |
| #end-选取数据,并生成csv | |
| #begin-执行远程目录下的bat文件 | |
| m.bat_path = "C:/test/test.bat" #远程目录,host等信息由文件配置 | |
| m.batExecute | |
| #end-执行远程目录下的bat文件 | |
| #关闭数据库连接 | |
| m.dbClose |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #encoding: utf-8 | |
| require "method" | |
| m = Method2.new | |
| m.sql = "select TOP 1 * from T_UserTest" | |
| result = m.execute("source") | |
| result.each do |row| | |
| m.sql = "insert into T_UserTest values(NEWID(),'#{row['UserCode'].to_s.encode('UTF-8')}','#{row['UserName'].to_s.encode('UTF-8')}','#{row['City'].to_s.encode('UTF-8')}','#{row['Year'].to_s.encode('UTF-8')}','#{row['Password'].to_s.encode('UTF-8')}','#{row['Title'].to_s.encode('UTF-8')}','#{row['Department'].to_s.encode('UTF-8')}','#{row['Notes'].to_s.encode('UTF-8')}',GETDATE())" | |
| r = m.execute("linked") | |
| r.insert | |
| end | |
| m.dbClose |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*create linked server*/ | |
| exec sp_addlinkedserver 'linkedserver', ' ', 'SQLOLEDB', 'IP address' | |
| exec sp_addlinkedsrvlogin 'linkedserver', 'false ',null, 'username', 'password' | |
| /*insert bulk data*/ | |
| insert into linkedserver.[database].dbo.[tablename] select * from dbo.[tablename] | |
| /*delete linked server*/ | |
| exec sp_dropserver 'linkedserver', 'droplogins' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #encoding: utf-8 | |
| require 'CSV' #standard | |
| require 'tiny_tds' #db connect | |
| require 'yaml' #standard | |
| require 'net/telnet' #standard | |
| #Fix Telnet Bug | |
| class Net::Telnet | |
| def print(string) | |
| string = string.gsub(/#{IAC}/no, IAC + IAC) if @options["Telnetmode"] | |
| if @options["Binmode"] | |
| self.write(string) | |
| else | |
| if @telnet_option["BINARY"] and @telnet_option["SGA"] | |
| self.write(string.gsub(/\n/n, CR)) | |
| elsif @telnet_option["SGA"] | |
| self.write(string.gsub(/\n/n, EOL)) ### fix here. replace CR+NULL by EOL | |
| else | |
| self.write(string.gsub(/\n/n, EOL)) | |
| end | |
| end | |
| end | |
| end | |
| class Method1 | |
| attr_accessor :sql | |
| attr_accessor :bat_path | |
| def initialize | |
| config = YAML.load_file(File.expand_path("../../conf/config.yml",__FILE__)) | |
| @connect_username = config["method1"]["connect_username"] | |
| @connect_password = config["method1"]["connect_password"] | |
| @connect_host = config["method1"]["connect_host"] | |
| @connect_database = config["method1"]["connect_database"] | |
| @connect_encode = config["method1"]["connect_encode"] | |
| @telnet_host = config["method1"]["telnet_host"] | |
| @telnet_port = config["method1"]["telnet_port"] | |
| @telnet_user = config["method1"]["telnet_user"] | |
| @telnet_pwd = config["method1"]["telnet_pwd"] | |
| end | |
| def dbConnect | |
| #1)connect | |
| #sql server: GBK | |
| @db_client = TinyTds::Client.new(:username => @connect_username, :password => @connect_password, :host => @connect_host, :database => @connect_database,:encode=>@connect_encode) | |
| return @db_client | |
| end | |
| def csvResult | |
| #2)execute | |
| return dbConnect.execute(@sql) | |
| end | |
| def dbClose | |
| @db_client.close | |
| end | |
| def batExecute | |
| #5)execute .bat | |
| #TODO: telnet | |
| server = Net::Telnet::new("Host" => @telnet_host,"Prompt"=> /C:.*>/) | |
| server.login(@telnet_user,@telnet_pwd){|c| print c} | |
| server.cmd("#{@bat_path}"){|c| print c} | |
| server.close | |
| end | |
| end | |
| #if sql doesn't work, please use this class | |
| class Method2 | |
| attr_accessor :sql | |
| def initialize | |
| config = YAML.load_file(File.expand_path("../../conf/config.yml",__FILE__)) | |
| @source_connect_username = config["method2"]["source_server"]["connect_username"] | |
| @source_connect_password = config["method2"]["source_server"]["connect_password"] | |
| @source_connect_host = config["method2"]["source_server"]["connect_host"] | |
| @source_connect_database = config["method2"]["source_server"]["connect_database"] | |
| @source_connect_encode = config["method2"]["source_server"]["connect_encode"] | |
| @linked_connect_username = config["method2"]["linked_server"]["connect_username"] | |
| @linked_connect_password = config["method2"]["linked_server"]["connect_password"] | |
| @linked_connect_host = config["method2"]["linked_server"]["connect_host"] | |
| @linked_connect_database = config["method2"]["linked_server"]["connect_database"] | |
| @linked_connect_encode = config["method2"]["linked_server"]["connect_encode"] | |
| end | |
| def dbConnect(connect_flag) | |
| if connect_flag == "source" | |
| @db_client = TinyTds::Client.new(:username => @source_connect_username, :password => @source_connect_password, :host => @source_connect_host, :database => @source_connect_database,:encode=>@source_connect_encode) | |
| elsif connect_flag == "linked" | |
| @db_client = TinyTds::Client.new(:username => @linked_connect_username, :password => @linked_connect_password, :host => @linked_connect_host, :database => @linked_connect_database,:encode=>@linked_connect_encode) | |
| end | |
| return @db_client | |
| end | |
| def dbClose() | |
| @db_client.close | |
| end | |
| def execute(connect_flag) | |
| return dbConnect(connect_flag).execute(@sql) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment