Alpaca – Unit tests over Hana
How Alpaca burn ?
Its all start when we was asked to move some code from java into db store procedure.
Usually when writing a code in java we use the TDD (Test Driven Development Methodology) and write unit test together with the development process .
Doing this in java its easy and we are using JUnit libraries for it.
However, There is no tool or framework that will allow me to write unit test on Hana db and there is no way to work TDD while developing store procedure and function in Hana .
Of course I can run Junit test that connect to Hana or write some XS code that check hana , but I wanted to test my code within the language itself without having other layers .
We have in SAP Labs Israel contest that called “Inno Jam”, each employee in the lab can work 3 days on an idea he or his friends thinnk of .
After the 3 days, every team present a demo (it must be demo) in only 6 minutes that show their idea working.
It was my first innoJam, the atmosphere was great , pizza and beers was served until night.
I choose the name ALPCA for my idea and 3 other employees from my team join too.
(from left to right: Sapir Golan, Ido Mosseri(me), Rony Lahav, Avi Klein)
We had 3 missions for Alpaca
- Testing and working tdd within Hana
- Insert the testing into quality process within Jenkins
- Have a nice Alpaca report via mail
The main idea was to create a framework that provide us a list of assert db functions that will help us testing our hana code.
By running the assert functions we will get a report record that contain the fields : tested object(name of the store procedure/ funtion), test name (for exmaple: “1+1=4”), test status (paas or fail) and messege .
So, user who want to work TDD , can start with creating the tests inside dedicated test store procedure, execute the test , fail , and fix the tested SP.
it will be more clear after looking at the example.
we want to add ‘multi’ functionalaty to the SP CALC (the sp calc get 2 items and action and return the result of the action on the items).
the currewnt situation is that we have the action plus and minus :
create procedure "CALC" ( in item1 INTEGER, in item2 INTEGER , in action varchar(256), out result INTEGER) AS BEGIN if action = 'plus' then result := item1 + item2; end if; if action = 'minus' then result := item1 - item2; end if; END;
now, lets have a look on the developer test SP. we have an ALPACA test that check the result of running the calc SP with 1, 3 and ‘plus’ .
create procedure "TEST_CALC" ( out result TEST_RESULT) AS sp_result INTEGER; BEGIN call "CALC"(1,3,'plus',sp_result) ; call "ASSERT_TRUE" ('CALC','test 1+3= 4',sp_result,4,?); result = select * from "TEST_RESULTS" where tested_object = 'CALC' ; END;
now, by calling :
we will get the output :
Now, lets start the TDD…
first we will write the test, so we add to TEST_CALC the lines :
call "CALC"(2,3,'multi',sp_result) ; call "ASSERT_TRUE" ('CALC','test 2*3=6',sp_result,6,?);
Lets run the test :
Ohhhh we get Exception , but Hey, this is what was expected since the multi action is not implemented yet …
ok, lets develop the multi action, we will add to the calc sp the lines :
if action = 'multi' then result := item1 * item2; end if;
now, lets run it again
lets add test that should fail and add those lines to TEST_CALC:
call "CALC"(2,5,'multi',sp_result) ; call "ASSERT_TRUE" ('CALC','test 2*5=9',sp_result,9,?);
In the same way we implement the ASSERT_TRUE function we also implement the ASSERT_EXIST that check if there is a record in a table.
CALL ASSERT_EXIST ('ADD_PURCHASE'(sp name), 'Check new record in PURCHASES table '(test description), 'PURCHASES' (table name), ' customer_name=''IDO'' and product_name = ''BIKE'' ' (where clause),?);
in 3 days we did all the development including adding suites(that contain several tests sp) , connecting the test suites to Jenkins and sending a report by email .
(example of email generated by Alpaca)
The Alpaca project is not ready for customers use since we need to implement more functionality over the framework.
However,It can give huge benifit to the hana developers .
We didnt win the contest however we had a lot of fun implementing this idea ..