1

I have try to test PostgreSQL [Does PostgreSQL work properly ?]. I have read a little bit about Moq when we have to do unit testing. So I have 3 questions.

1 When we do Arrange [AAA pattern] Do we have to moq configuration database and input before testing SQL execution?

2 Based on first question. If it required for moq then What is different if i just set up normally like this

            DumpDatabase databaseSetting = new DumpDatabase();
            databaseSetting.Host = "***.***.*.*.*";
            databaseSetting.Port = "****";
            databaseSetting.Database = "*****";
            databaseSetting.UserName = "****";
            databaseSetting.Password = **********";

instead of

   var mockdb = new Mock<DumpDatabase>();
            mockdb.Setup(x => x.dumpDatabase).Returns(
                new DumpDatabase()
                {
                    Host = "***.***.*.*.*",
                    Port = "****",
                    Database = "*******",
                    UserName = "*****",
                    Password = "***********"
                }
                );

3 If Query has been passed, i have to roll back original in every single test right ?

For example


  string query = "INSERT INTO pictures (id, created, width, height, device_id, user_id, modified, checksum, file_path, file_extension, file_size, mime_type, thumbnail, title, classification, description)" +
                                                                 " VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12::mime, @p13, @p14, @p15::classification, @p16); ";

then i need to roll back original After Assert [AAA pattern] by doing this

string sqlDelete = "DELETE FROM public.pictures WHERE id = @p1;";

Peera
  • 99
  • 1
  • 1
  • 9
  • For SQL Test I would recommend just Deattach database and make copy mdf file. Then attach the original and copy of database to server. The difference between old and new will be the Default Database name in the connection string. This way you do not need to roll back results after testing. – jdweng Apr 22 '19 at 09:29
  • @jd i forgot that i used postgres. I didn't use SQL. If you have anything or other source that would be benefit for me,please attach url/link/git – Peera Apr 22 '19 at 11:10
  • I think you can do same from postgreSQL. See : https://stackoverflow.com/questions/3305961/recover-postgresql-databases-from-raw-physical-files – jdweng Apr 22 '19 at 11:24
  • @jdweng thank you for your suggest. Anyway , i have other idea that i might do clone db[for testing] and use sql to roll back with batch script. That should be the another simple way to do this – Peera Apr 22 '19 at 12:42
  • But a greater risk of corrupting your data. – jdweng Apr 22 '19 at 13:03
  • Yeah, but let's try to do both of your and my solution. Never try Never know. ;) – Peera Apr 22 '19 at 13:15
  • Anyway, Do you know solution/idea about question 1 and 2 that i have posted to ask ?. – Peera Apr 22 '19 at 13:17
  • You need a connection string which comes from the properties (Host, Port, ...). See : https://www.connectionstrings.com/postgresql/ – jdweng Apr 22 '19 at 13:24
  • @jdweng so i don't need to moq right ? because there is no needed to use any dependency .So what i need to do is just set up PostgreSQL connection strings [ Like source that you attached ] in order to open clone/backup db right ?. So that i can do integration test with query (i.e., when I call Update, does it execute the query?). – Peera Apr 22 '19 at 14:05
  • Yes. You would need to pass the connection string from the test case to the project if it is dynamic for each test case. – jdweng Apr 22 '19 at 14:14
  • Yes. You would need to pass the connection string from the test case to the project if it is dynamic for each test case. Update is not a QUERY. Only reads of database are technically queries. Normally when you use a DataAdapter you get a DataSet as a response. Then the update only makes changes to the rows of the tables that got changed. There are four types of Commands 1) Select 2) Update 3) Insert 4 ) Delete. There is a command builder class that takes the Select Query and then generates the other three commands. – jdweng Apr 22 '19 at 14:21
  • @jdweng How about input param for query? because it required to get right datatype , so the next step is Moq for DataSet before execute Query ? or just create class with preparing data ? – Peera Apr 22 '19 at 14:34
  • You can pass both a CommandText and ParameterList for the command to use. – jdweng Apr 22 '19 at 14:50
  • Oh OK Thank you . I will try that ! – Peera Apr 22 '19 at 14:53

0 Answers0