简体   繁体   English

Linq2Sql-转换代码-Visual Studio 2012-单元测试

[英]Linq2Sql - Converting Code - Visual Studio 2012 - Unit Tests

I have been trying to convert the code below. 我一直在尝试转换下面的代码。 I have attempted it and I will post the attempt beneath the code below. 我已经尝试过了,并将在下面的代码下面发布尝试。 I am confused when it comes to 我很困惑

Code That I have to convert: 我必须转换的代码:


[TestMethod]
        public void SRSTransactiondataCleanTest()
        {
            using (SqlConnection sqlConn = new
               SqlConnection(@"Data Source=localhost;Initial Catalog=CorporateDWTest;Integrated Security=SSPI;"))
            {

                StringBuilder str = new StringBuilder();
                str.Append(@"SELECT * FROM [dbo].[SRS_Ticket_Transaction_Stage_Cleaned]");
                sqlConn.Open();
                SqlDataAdapter da = new SqlDataAdapter(str.ToString(), sqlConn);
                DataTable dResults = new DataTable();
                DataTable dAudit = new DataTable();
                da.Fill(dResults);

                da.SelectCommand.CommandText = @"SELECT [Table_Name],[Package_Name],[Execution_Start_Time],[Execution_End_Time],[Processing_Successful], Audit_Key," +
                    @"[Table_Initial_Row_Count],[Table_Final_Row_Count] FROM [SRS_Dim_Audit] WHERE Package_Name LIKE 'SAPSRSDataToStageClean'";

                da.Fill(dAudit);
                var r = dAudit.Rows[0][2].ToString();

                Assert.AreEqual(1, dAudit.Rows.Count);
                Assert.AreEqual("SRS_Ticket_Transaction_Stage_Cleaned", dAudit.Rows[0][0].ToString());
                Assert.AreEqual("SAPSRSDataToStageClean", dAudit.Rows[0][1].ToString());
                Assert.AreEqual(bool.TrueString, dAudit.Rows[0][4].ToString());
                Assert.IsTrue(int.Parse(dAudit.Rows[0][7].ToString()) > 10);
                Assert.IsTrue(dResults.Rows.Count > 50);
                Assert.AreEqual(int.Parse(dAudit.Rows[0][7].ToString()), dResults.Rows.Count);
                Assert.AreEqual(int.Parse(dAudit.Rows[0][5].ToString()), int.Parse(dResults.Rows[0][2].ToString()))
                sqlConn.Close();
            }
       }

What I have so far: 到目前为止,我有:

[TestMethod]
        public void SRSTransactiondataCleanTest()
        {
            using (var context = new CorporateDWTestEntities4())
            {
                var stageCleaned = context.SRS_Ticket_Transaction_Stage_Cleaned;
                var auditRecords = context.SRS_Dim_Audit.Where(s => s.Package_Name == "SAPSRSDataToStageClean");
                var auditRecord = auditRecords.FirstOrDefault();

                Assert.AreEqual(1, stageCleaned.Count());
                Assert.AreEqual(auditRecord.Table_Final_Row_Count, stageCleaned.Count());
                Assert.AreEqual(52, auditRecords.Count());
                Assert.AreEqual("SRS_Ticket_Transaction_Stage_Cleaned", auditRecord.Table_Name);
                Assert.AreEqual("SAPSRSDataToStageClean", auditRecord.Package_Name);
                Assert.AreEqual(true, auditRecord.Processing_Successful);
                Assert.IsTrue(auditRecord.Table_Final_Row_Count > 10);

            }
        }

I am fairly new at this code conversion stuff.. Am I on the right track? 在这个代码转换方面,我还很陌生。我走对了吗? When I run this I recieve a message that states: 运行此命令时,我收到一条消息,指出:

"Assert.AreEqual failed. Expected <1>. Actual <9461>."

When I change it to 9461, the entire test passes. 当我将其更改为9461时,整个测试通过。 Why is that? 这是为什么? Am I using the wrong variable? 我使用了错误的变量吗? I would just like to know if the code that I wrote is actually mirroring the code that I was to convert. 我只想知道我编写的代码是否实际上在镜像要转换的代码。 I know with programming syntax must be meticulous. 我知道用编程语法一定要一丝不苟。 I was wondering if someone out there could review this and modify IF necessary. 我想知道是否有人可以对此进行审查并修改必要的IF。

Ps. PS。 the line: 该行:

  Assert.AreEqual(int.Parse(dAudit.Rows[0][5].ToString()), int.Parse(dResults.Rows[0][2].ToString()));

confused the hell out of me, What is that even asking of me? 弄得我心烦意乱,什至问我呢?

Your first test should be 您的第一个测试应该是

 Assert.AreEqual(1, auditRecords.Count());

rather than 而不是

Assert.AreEqual(1, stageCleaned.Count());

You also have a test 你也有考试

Assert.AreEqual(52, auditRecords.Count());

I can't see where you get 52 free. 我看不到您可以免费获得52。 If this is supposed to be the equivalent of 如果这应该等于

Assert.IsTrue(dResults.Rows.Count > 50);

then it should be 那应该是

Assert.IsTrue(stageCleaned.Count() > 50);

Not sure why you are confused by the line 不知道为什么你会被这条线弄糊涂

 Assert.AreEqual(int.Parse(dAudit.Rows[0][5].ToString()), int.Parse(dResults.Rows[0][2].ToString()));

You must be very close as you have already worked out what all the other dAudit.Rows[0][...] values are, so presumably you know that int.Parse(dAudit.Rows[0][5].ToString()) represents auditRecord.Audit_Key (assuming this is defined as an int). 您必须非常接近,因为您已经计算出所有其他dAudit.Rows[0][...]值是什么,所以大概您知道int.Parse(dAudit.Rows[0][5].ToString())代表auditRecord.Audit_Key (假设将此定义为int)。 Similarly dResults.Rows[0][2] will be the the third column of the first row from the SRS_Ticket_Transaction_Stage_Cleaned table. 类似地, dResults.Rows[0][2]将是SRS_Ticket_Transaction_Stage_Cleaned表中第一行的第三列。

So presumably this represents something like 所以大概这代表了

var firstStageRecord = stageCleaned.First();
Assert.IsTrue(auditRecord.Audit_Key , firstStageRecord.xxx); 

where xxx is the name of the third column. 其中xxx是第三列的名称。

There are a few other issues to point out. 还有其他一些问题需要指出。 If you have several calls to stageCleaned.Count(), it will issue a sql command each time is is called. 如果您多次调用stageCleaned.Count(),则每次调用它都会发出一个sql命令。

The original code was hard-coding the sqlConnection, whereas your new code is either using the default or picking up a connection from a configuration file, so you need to check to ensure you are talking to the correct database. 原始代码是对sqlConnection进行硬编码的,而新代码是使用默认代码或使用配置文件中的连接,因此您需要检查以确保与正确的数据库通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM