仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 922|回复: 7
打印 上一主题 下一主题

[学习教程] MSSQL网页设计TSQLUnit简介(翻译)

[复制链接]
飘灵儿 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:25:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
对于insert和delete,event中包含了插入/删除的记录的所有字段的值(太爽了。。)
IntroducingTSQLUnit
TSQLUnit简介
TSQLUnitisanopensourceunittestingframeworkforT-SQLwrittenbyHenrikEkelundandavailablefromhttp://sourceforge.net/projects/tsqlunit.HeresanexampleofhowIveusedit.

MyTSQLUnitteststakeasimilarpatternofthreeparts:
1)unittestsetup,测试设置
2)executionofthetargetprocedure,and实行测试方针存储历程.
3)checkingresults.反省了局.

Intheunittestsetup,IoftenchecktomakesuresomeonehasntdonebadthingstomydatawhenIwasntlooking:
在单位测试的设置中我常常反省其别人没有损坏我但愿的数据:

DECLARE@nIdINT,@nNewIdINT—-@nNewIdisforlater
SELECT@nId=[ID]FROMMyTableWHEREMyField=whatever
IF@nIdISNULL--or@@ROWCOUNT=0
EXECtsu_failureThedatahaschanged.whatevercouldntbefound

TheIFblockchecksfortheexpectedrecord.Ifitcouldntbefound,thetestfailsandwillgenerateanerrormessage.Thetestframeworkmovesontothenextunittest.Youdontneedtousethenameoftheunittestinthefailuremessagestring,becauseTSQLUnitwillnameitforyouwhenthetestfails.
IF块反省希冀的纪录.假如没法找到,测试失利并天生一个毛病动静.测试框架挪动到下一个单位测试.你不必在失利的动静中利用测试的名字,由于TSQLUnit会在测试失利时定名的.
NowIcallthestoredprocedureImabouttowrite:
如今我挪用我要写的存储历程:
EXECCreateMyTableNewRec@nId,@nNewIdOUTPUT

Asyoucansee,IvedeterminedthatIneedanoutputparameterfromthisnewprocedure.Incheckingtheresults,Imakesuretheoutputparameterreallyisfilledwithsomething:
你看,我将反省存储过程当中必要前往的参数.在反省了局中,要确认输入参数真的被添补了.
IF@nNewIdISNULL
EXECtsu_failureAnewrecordwasnotcreatedfortableMyTable.

IcouldfurthercheckthevaluetoseeifthenewrecordwascreatedinthewayIwantedittobecreated.
我能够反省更深条理的值,假如我请求的新纪录被创立了.
EachTSQLUnittestisitselfastoredprocedure.Listing1showswhatonelookslikewhenallofthepiecesareputtogether:
每一个TSQLUnit测试都是一个存储历程.列表1显现了一切测试段落在一同的情形.
Listing1.AcompleteunittestforT-SQL.

CREATEPROCEDUREut_MyTable_NewRec
AS
--==Setup==--
DECLARE@nIDINT,@nNewIdINT
SELECT@nId=IDFROMMyTable
WHEREMyField=whatever
IF@nIdISNULL--or@@ROWCOUNT=0
EXECtsu_failureThedatahaschanged.
Whatevercouldntbefound
--==Execute==--
EXECCreateMyTableNewRec@nId,@nNewIdOUTPUT
--==Check==--
IF@nNewIdISNULL
EXECtsu_failureAnewrecordwasnotcreated
fortableMyTable.
GO

Notethethree-partnameofthestoredprocedure,ut_MyTable_NewRec.Theprefix"ut_"alertsTSQLUnitthatthisisaunittestitshouldrun.Ifyoualreadyusethisprefixut_forotherpurposes,TSQLUnitletsyousetittosomethingelse."MyTable"isthenameofagroupofrelatedunittests,knownasasuiteoftests.Forinstance,youcouldaddanotherunittestcalledut_MyTable_DeleteRec.TheMyTablesuitewouldtestbothaddinganddeletingarecordtoMyTable.Thesuitecanberunseparatelyfromothertestsuites.Thethirdpartofthename–"NewRec"or"DeleteRec"–uniquelyidentifiesthisunittest.
三个段落的存储历程,前缀ut_是告知TSQLUnit要运转的一个单位测试.假如你已利用ut_前缀作其他的用处,TSQLUnit请求你修正为其他的名字.名字的第二个段落,显现了测试的组,比方,你能够到场一个单位测试名字为ut_MyTable_DeleteRec.这个组会测试增加和删除一个纪录到MyTable,组也能够分隔到其他的测试组中.名字的第三个段落,是测试的独一标示.
NotethatyoudontneedBEGINTRANandROLLBACKineachunittest;TSQLUnittakescareofthisforyou.
你也不在必要BEGINTRAN和ROLLBACK在每一个单位测试.TSQLUnit卖力为你处置

Runningtheunittest
运转单位测试
InordertoruntheunittestinListing1,youneedtosetuptheframework.FromQueryAnalyzer,runtsqlunit.sqlonyourdevelopmentdatabase.Youneeddothisonlyonceforthedatabase.Next,createprocedureut_MyTable_NewRec,ifyouhaventalready.Nowyoureset.Simplyexecutetheunittest:
为了运转列表1中的单位测试,你要设置测试框架.在查询剖析器中,运转TSQLUnit.SQL在你的开辟数据库中..你仅仅要实行一次在数据库中.创立ut_myTable_newRec,如今你能够复杂的实行单位测试了:

--ThiswillrunalltestsforsuiteMyTable,这将运转MyTable组的一切测试
EXECtsu_RunTestsMyTable

Fixtures设备
SupposeIwantnumerousrecordstobeavailableforalltheunittestsofasuite.Idontwanttowritethesamesetupcodeforeachtest.TSQLUnitsolvestheproblemwithasetupfixture.Thecodeinthefixturewillberunbeforeeachunittest.
在一个单位测试组中,我请求良多纪录无效,我不想每次都写一样的设置代码,TSQLUnit利用了一个SetUp的标识设备:

Forinstance,thesetupfixtureforthepreviousMyTablesuitewouldbenamedut_MyTable_setup.Thethirdpartofthename"setup"alertsTSQLUnittotreattheprocedureasasetupfixtureforthesuite.Itwilllooksomethinglikethis:
比方,前缀为MyTable的组可使用ut_MyTable_Setup的设备:定名为Setup会让TSQLUnit以为这是个启动的设备.代码以下:
CREATEPROCEDUREut_MyTable_setup
AS
INSERTINTOMyTable([Description])
VALUES(something)
--(morerecordsinsertedhere
GO

TheSQLServercommunityowesahugedebtofgratitudetoHenrikEkelundandhisemployerformakingTSQLUnitopensource.
SQLServer社区极年夜的感谢HenrikEkelund和他的人员让TSQLUNnit开放源码.

Linktohttp://sourceforge.net/projects/tsqlunit

Linktohttp://tsqlunit.sourceforge.net/tsqlunit_cookbook.htm(documentation)

Archive非常适合存储大量的独立的,作为历史记录的数据。因为它们不经常被读取。Archive拥有高效的插入速度,但其对查询的支持相对较差
乐观 该用户已被删除
沙发
发表于 2015-1-19 11:23:13 来自手机 | 只看该作者
其实可以做一下类比,Oracle等数据库产品老早就支持了java编程,而且提供了java池参数作为用户配置接口。但是现在有哪些系统大批使用了java存储过程?!连Oracle自己的应用都不用为什么?!
莫相离 该用户已被删除
板凳
发表于 2015-1-27 10:45:27 | 只看该作者
理解了存储结构,再阅读下性能优化的章节基本上会对sqlserver有个清晰地认识
地板
发表于 2015-2-11 08:18:53 | 只看该作者
至于淘汰的问题,只能说在你的项目周期之内,微软应该都不会倒闭。
灵魂腐蚀 该用户已被删除
5#
发表于 2015-3-2 02:44:27 | 只看该作者
至于淘汰的问题,只能说在你的项目周期之内,微软应该都不会倒闭。
愤怒的大鸟 该用户已被删除
6#
发表于 2015-3-11 02:03:06 | 只看该作者
不好!如果出了错;不好调试;不好处理!其实web开发将代码分为3层:web层;业务逻辑层和数据访问层;一般对数据库的操作都在数据访问层来做;这样便于调试和维护!而且将来如果是换了数据库的话;你只需要改数据层的代码;其他层的基本可以不变!要是你在jsp中直接调用sql数据库;那么如果换了数据库呢?岂不都要改?如果报了异常呢?怎么做异常处理?
小魔女 该用户已被删除
7#
发表于 2015-3-17 19:02:40 | 只看该作者
多走走一此相关论坛,多看一些实例开发,多交流0经验,没什么的,我也是刚学没多久!加油
若相依 该用户已被删除
8#
发表于 2015-3-24 19:56:07 | 只看该作者
而SQLServer如果能像Oracle一样可以为登陆分配如:5%的cpu,10%的内存。就可以解决这个漏洞。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-3 19:34

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表