Solved

Unsure how to write specific unit test

  • 19 June 2020
  • 8 replies
  • 201 views

Userlevel 4
Badge +3

Hello all,

I have a question about unit tests. We're currently working on getting to grips with this subject. But I'm kinda stuck on one specific unit test. I have a default that I want to test. It is a simple one that removes spaces from a string, as follows:

if @cursor_from_col_id = '[field]'
and @[field] is not null
begin
set @[field] = REPLACE(@[field], ' ', '');
end;

But now I'm lost when adding the input parameters for the unit test. I'm choosing 'postal_code’ as a parameter with input '1234 AB’. But when I run the test it just returns '1234 AB’ (so it failed). I also tried adding 'cursor_from_col_id’ with input 'postal_code’ as an input parameter, but that also didn't work.

Looking forward to your answer!

icon

Best answer by Marcel van Langen 26 June 2020, 10:32

View original

8 replies

Userlevel 2
Badge +1

Hi Marcel,

As you describe it, it should work fine. So I'm wondering what the missing context is.

If I understood you well, you are trying to do the following:

Code:
if @cursor_from_col_id = 'postal_code’
and @postal_code is not null
begin
    set @postal_code = REPLACE(@postal_code, ' ', '');
end;

Unit test parameters:

INPUT: @cursor_from_col_id = postal_code
INPUT: @postal_code = 1234 AB
EXPECTED OUTPUT: @postal_code = 1234AB

Result:

Expected: success (@postal_code = 1234AB)
Actual result: failed (@postal_code = 1234 AB)

This situation should indeed be an expected success. Are you sure that there is nothing else altering the code, and that the default procedure code is executed on the database?

I'll need to look into this case to figure out what causes the problem. It indeed seems like an easy unit test scenario.

 

With kind regards,

Serhat Sahin

Userlevel 4
Badge +3

Hello Serhat,

I let it simmer for the weekend and decided to have a look at it again this morning. But no matter how I look at it, I can't seem to find anything wrong with it. So I'm hoping you can have a more in depth look at it. I think all the info is in the story, but if you need anything else, please let me know!

Many thanks in advance!

BTW: I've obviously checked that the default is working in the application

Userlevel 4
Badge +3

Just to be sure, I have added screenshots FYI.

Hope this helps!

Userlevel 5
Badge +15

Did you take a look at what the unit tests actually executes? The debugger in the SF has somewhere a process where it translates the unit test to sql code. This might give you more information / insights on what's happening.

Userlevel 4
Badge +3

Thanks for your reply. It is difficult to see what I'm looking for. Every time I run the unit test I get a couple of processes (but they all sort of look the same). The following seems to be the essential of it:

 declare @default_mode tinyint;
    declare @import_mode tinyint;
    declare @cursor_from_col_id varchar(255) = ''achternaam'';
    declare @cursor_to_col_id varchar(255);
    declare @zba_portaal_berichten_overig_client_id int;
    declare @zba_portaal_berichten_overig_bericht_id int;
    declare @bsn varchar(9);
    declare @geboortedatum date;
    declare @geslacht tinyint;
    declare @achternaam varchar(200) = ''van Langen'';
    declare @voorvoegsel varchar(10);
    declare @voorletters varchar(6);
    declare @commentaar varchar(8000);
    execute def_zba_portaal_berichten_overig_client
        @default_mode = @default_mode,
        @import_mode = @import_mode,
        @cursor_from_col_id = @cursor_from_col_id,
        @cursor_to_col_id = @cursor_to_col_id output,
        @zba_portaal_berichten_overig_client_id = @zba_portaal_berichten_overig_client_id output,
        @zba_portaal_berichten_overig_bericht_id = @zba_portaal_berichten_overig_bericht_id output,
        @bsn = @bsn output,
        @geboortedatum = @geboortedatum output,
        @geslacht = @geslacht output,
        @achternaam = @achternaam output,
        @voorvoegsel = @voorvoegsel output,
        @voorletters = @voorletters output,
        @commentaar = @commentaar output;

    select 
         @cursor_to_col_id as cursor_to_col_id,
        @zba_portaal_berichten_overig_client_id as zba_portaal_berichten_overig_client_id,
        @zba_portaal_berichten_overig_bericht_id as zba_portaal_berichten_overig_bericht_id,
        @bsn as bsn,
        @geboortedatum as geboortedatum,
        @geslacht as geslacht,
        @achternaam as achternaam,
        @voorvoegsel as voorvoegsel,
        @voorletters as voorletters,
        @commentaar as commentaar;

Notice the double single quotes around the values (these wouldn't work in SQL Server) and the fact that @cursor_from_col_id is not included in the SELECT. But again, not sure if this is a problem.

Userlevel 2
Badge +1

Thanks for all replies.

You've picked the right piece of code from the debug. Even the debug seems okay…

Just to clarify, the double quotes are needed to execute the whole as a string (so double quotes will become a single quote), so you can ignore those and replace them with single quotes in SQL Server. The select statement is selecting the output parameters. The cursor_from_col_id parameter is an input parameter only, that's why it's excluded in the select statement.

Could you check your private messages for me? I've sent you a message to help you in this matter and try to figure out what's wrong with this whole scenario.

Userlevel 4
Badge +3

Serhat and I will look into it together. After that, I'll post our findings below this story for others.

Userlevel 4
Badge +3

Today Serhat looked at it. The fact that it didn't work was because I used a field for unit testing (postal_code) that I didn't use in the application. In the application I used the field 'initials’. So the lesson here: only unit test a dynamic default on columns that you also use in the application. Thanks Serhat for the help!

Reply