Unit testing, without a database

Often we want to test some code that makes a whole lot of database calls, one way to do it is to set up a testing database and clean it up again when we are done. This works well, but is hard to do, and is hard to do as part of an install step when we may not even have a database server yet. What we have done in Koha is put these tests in a db_dependent directory, and the developers and Jenkins run them. But we can also do a lot of testing using things like Test::Mockmodule and DBD::Mock. Below is a snippet of code showing how we can use it for Koha.

use DBI;
use Test::More tests => 15;
use Test::MockModule;
BEGIN {
use_ok('C4::ItemType');
}
# Mock the call to create a database handler
my $module = new Test::MockModule('C4::Context');
$module->mock('_new_dbh', sub {
my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
|| die "Cannot create handle: $DBI::errstrn";
return $dbh });
# Mock data
my $itemtypes = [
['itemtype','description','rentalcharge','notforloan','imageurl','summary'],
['BK','Books',0,0,'',''],
['CD','CDRom',0,0,'','']
];
my $dbh = C4::Context->dbh(); # since we mocked _new_dbh we get a mock handle
my @itemtypes = C4::ItemType->all();
is(@itemtypes, 0, 'Testing all itemtypes is empty');
# This should run exactly one query so we can test
my $history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Correct number of statements executed') ;
# Now lets add some mock data
$dbh->{mock_add_resultset} = $itemtypes;
@itemtypes = C4::ItemType->all();
is(@itemtypes, 2, 'ItemType->all should return an array with 2 elements');
is($itemtypes[0]->itemtype, 'BK', 'First itemtype is bk');
is($itemtypes[0]->fish,undef,'Calling a bad descriptor gives undef');

Signoff Statistics for July 2012

  • Jonathan Druart- 24
  • Jared Camins-Esakov- 19
  • Kyle M Hall- 18
  • Chris Cormack- 16
  • Owen Leonard- 15
  • M. de Rooy- 9
  • Marc Véron- 8
  • Frédéric Demians- 7
  • Mirko Tietgen- 6
  • Katrin Fischer- 6
  • Paul Poulain- 4
  • Elliott Davis- 4
  • Mason James- 3
  • Gaetan Boisson- 2
  • Srdjan Jankovic- 2
  • Robin Sheat- 2
  • Nicole C. Engard- 2
  • Dobrica Pavlinusic- 2
  • Zeno Tajoli- 2
  • David Cook- 2
  • wajasu- 1
  • Christophe Croullebois- 1
  • Alex Arnaud- 1
  • Julian Maurice- 1
  • Pascale Nalon- 1

Yes, you should rewrite it. NO! Not like that!

Something I hear and have heard a lot in my life as a programmers is

We should rewrite this, I have no idea what it is doing

Now, your instinct to rewrite is almost always the correct thing to do, but almost always the implementation is wrong. Why is this? Well it’s because of the same reason we want to rewrite it, we don’t know what it is doing. So often we end up causing regressions, or accidentally leaving features out.  How do we get around this? This is where I stand up and go to put on a broken record. The answer is unit tests, in fact ask me anything I’ll probably say unit tests.

  • “How do I make Koha do this?” “Well first you write a unit test and then …”
  • “I want to change this template” “Have you run the xt/tt_valid.t on that?”
  •  “Want some coffee?” “Is it unit tested?”
  • “Where’s the remote?” “Oh, I wrote a unit test for that.”

But seriously, if we invest time in writing tests, forcing us to learn the behaviour of the current implementation, we can then make sure our refactor/rewrite still passes these tests. We can also make sure any new behaviours we add have tests written for them too, after having to implement tests to test existing code, writing tests before writing the code seems a lot nicer.

I’m sure this is new to no one, and I’m really writing it as a reminder to myself, but hopefully it may be some help to others too. Go forth and refactor/rewrite  🙂

Git statistics for 3.6.7

  • Processed 32 changesets from 13 developers
  • 10 employers found
  • A total of 858 lines added, 244 removed (delta 614)

 

Developers with the most changesets
Chris Cormack 7 21.9%
Katrin Fischer 4 12.5%
Kyle M Hall 4 12.5%
Paul Poulain 3 9.4%
Colin Campbell 3 9.4%
Owen Leonard 3 9.4%
Jared Camins-Esakov 2 6.2%
Mark Tompsett 1 3.1%
MJ Ray 1 3.1%
Piotr Kowalski 1 3.1%
Simon Story 1 3.1%
Amit Gupta 1 3.1%
Robin Sheat 1 3.1%

 

Developers with the most changed lines
Mark Tompsett 304 35.2%
Chris Cormack 291 33.7%
Owen Leonard 103 11.9%
Colin Campbell 53 6.1%
Kyle M Hall 39 4.5%
Simon Story 29 3.4%
Paul Poulain 16 1.9%
Amit Gupta 9 1.0%
Jared Camins-Esakov 6 0.7%
Katrin Fischer 4 0.5%
MJ Ray 2 0.2%
Piotr Kowalski 1 0.1%
Robin Sheat 1 0.1%

 

Developers with the most lines removed

 

Developers with the most signoffs (total 100)
Jared Camins-Esakov 38 38.0%
Chris Cormack 26 26.0%
Paul Poulain 23 23.0%
Liz Rea 5 5.0%
Jonathan Druart 3 3.0%
Magnus Enger 2 2.0%
MJ Ray 2 2.0%
Marc Veron 1 1.0%

 

Top changeset contributors by employer
BigBallOfWax 5 15.6%
ByWater-Solutions 4 12.5%
BSZ-BW 4 12.5%
(Unknown) 4 12.5%
Catalyst 3 9.4%
Biblibre 3 9.4%
PTFS-Europe 3 9.4%
ACPL 3 9.4%
C & P Bibliography 2 6.2%
Software.coop 1 3.1%

 

Top lines changed by employer
(Unknown) 343 39.7%
BigBallOfWax 274 31.7%
ACPL 109 12.6%
PTFS-Europe 53 6.1%
ByWater-Solutions 39 4.5%
Catalyst 18 2.1%
Biblibre 16 1.9%
C & P Bibliography 6 0.7%
BSZ-BW 4 0.5%
Software.coop 2 0.2%

 

Employers with the most signoffs (total 100)
C & P Bibliography 38 38.0%
Biblibre 26 26.0%
Catalyst 23 23.0%
(Unknown) 6 6.0%
BigBallOfWax 3 3.0%
Software.coop 2 2.0%
Libriotech 2 2.0%

 

Employers with the most hackers (total 14)
(Unknown) 4 28.6%
Catalyst 2 14.3%
C & P Bibliography 1 7.1%
Biblibre 1 7.1%
BigBallOfWax 1 7.1%
Software.coop 1 7.1%
ACPL 1 7.1%
PTFS-Europe 1 7.1%
ByWater-Solutions 1 7.1%
BSZ-BW 1 7.1%

 

 

Git statistics for Koha 3.8.3

  • Processed 78 changesets from 30 developers
  • 15 employers found
  • A total of 8718 lines added, 613 removed (delta 8105)

 

Developers with the most changesets
Galen Charlton 10 12.8%
Chris Cormack 9 11.5%
Jared Camins-Esakov 6 7.7%
Owen Leonard 5 6.4%
Kyle M Hall 5 6.4%
Paul Poulain 4 5.1%
Jonathan Druart 3 3.8%
Marcel de Rooy 3 3.8%
Alex Arnaud 3 3.8%
Colin Campbell 3 3.8%
Katrin Fischer 2 2.6%
Mason James 2 2.6%
Robin Sheat 2 2.6%
Mark Tompsett 2 2.6%
Marc Veron 2 2.6%
Matthias Meusburger 2 2.6%
Claire Hernandez 2 2.6%
wajasu 1 1.3%
Dobrica Pavlinusic 1 1.3%
Frédéric Demians 1 1.3%
Mirko Tietgen 1 1.3%
Frère Sébastien Marie 1 1.3%
Chris Hall 1 1.3%
Julian Maurice 1 1.3%
David Cook 1 1.3%
Duncan Tyler 1 1.3%
Stacey Walker 1 1.3%
Lyon3 Team 1 1.3%
Sophie Meynieux 1 1.3%
Maxime Pelletier 1 1.3%

 

Developers with the most changed lines
Galen Charlton 4300 47.7%
Frédéric Demians 3522 39.1%
Jared Camins-Esakov 261 2.9%
Julian Maurice 250 2.8%
Claire Hernandez 164 1.8%
Chris Cormack 79 0.9%
Colin Campbell 64 0.7%
Dobrica Pavlinusic 60 0.7%
Stacey Walker 54 0.6%
Kyle M Hall 36 0.4%
Alex Arnaud 24 0.3%
Owen Leonard 19 0.2%
Matthias Meusburger 19 0.2%
Frère Sébastien Marie 15 0.2%
Maxime Pelletier 15 0.2%
Robin Sheat 14 0.2%
Paul Poulain 9 0.1%
Marcel de Rooy 9 0.1%
Sophie Meynieux 6 0.1%
Jonathan Druart 5 0.1%
Duncan Tyler 5 0.1%
Katrin Fischer 4 0.0%
Mason James 4 0.0%
Mark Tompsett 3 0.0%
Marc Veron 2 0.0%
Lyon3 Team 2 0.0%
wajasu 1 0.0%
Mirko Tietgen 1 0.0%
Chris Hall 1 0.0%
David Cook 1 0.0%

 

Developers with the most lines removed
Jared Camins-Esakov 220 35.9%
Colin Campbell 11 1.8%

 

Developers with the most signoffs (total 196)
Chris Cormack 78 39.8%
Paul Poulain 62 31.6%
Jared Camins-Esakov 16 8.2%
Katrin Fischer 10 5.1%
Marc Veron 5 2.6%
Kyle M Hall 4 2.0%
Nicole C. Engard 3 1.5%
Martin Renvoize 2 1.0%
Marcel de Rooy 2 1.0%
Owen Leonard 2 1.0%
Stacey Walker 2 1.0%
Dobrica Pavlinusic 1 0.5%
Liz Rea 1 0.5%
Delaye Stephane 1 0.5%
Tomas Cohen Arazi 1 0.5%
Mason James 1 0.5%
wajasu 1 0.5%
Jonathan Druart 1 0.5%
Robin Sheat 1 0.5%
Julian Maurice 1 0.5%
Frédéric Demians 1 0.5%
Top changeset contributors by employer
Biblibre 16 20.5%
(Unknown) 11 14.1%
Catalyst 10 12.8%
Equinox 10 12.8%
C & P Bibliography 6 7.7%
ByWater-Solutions 5 6.4%
ACPL 5 6.4%
PTFS-Europe 3 3.8%
Rijksmuseum 3 3.8%
BigBallOfWax 2 2.6%
KohaAloha 2 2.6%
BSZ-BW 2 2.6%
Libeo 1 1.3%
rot13.org 1 1.3%
Tamil 1 1.3%

 

Top lines changed by employer
Equinox 4300 47.7%
Tamil 3522 39.1%
Biblibre 479 5.3%
C & P Bibliography 290 3.2%
(Unknown) 84 0.9%
PTFS-Europe 74 0.8%
rot13.org 60 0.7%
BigBallOfWax 57 0.6%
Catalyst 50 0.6%
ByWater-Solutions 36 0.4%
ACPL 24 0.3%
Libeo 15 0.2%
Rijksmuseum 9 0.1%
KohaAloha 4 0.0%
BSZ-BW 4 0.0%

 

Employers with the most signoffs (total 196)
Catalyst 69 35.2%
Biblibre 65 33.2%
C & P Bibliography 16 8.2%
(Unknown) 10 5.1%
BigBallOfWax 10 5.1%
BSZ-BW 10 5.1%
ByWater-Solutions 7 3.6%
PTFS-Europe 2 1.0%
ACPL 2 1.0%
Rijksmuseum 2 1.0%
Tamil 1 0.5%
rot13.org 1 0.5%
KohaAloha 1 0.5%

 

Employers with the most hackers (total 31)
(Unknown) 9 29.0%
Biblibre 7 22.6%
Catalyst 3 9.7%
C & P Bibliography 1 3.2%
BigBallOfWax 1 3.2%
BSZ-BW 1 3.2%
ByWater-Solutions 1 3.2%
PTFS-Europe 1 3.2%
ACPL 1 3.2%
Rijksmuseum 1 3.2%
Tamil 1 3.2%
rot13.org 1 3.2%
KohaAloha 1 3.2%
Equinox 1 3.2%
Libeo 1 3.2%

 

Choosing something to sign off

Yesterday I wrote a post encouraging people to sign off more, and then I was standing in the shower thinking¹ this morning about how I choose what to sign off. Here’s how it works for me:

I did a presentation at Kohacon12 called “It’s all about the people” and for sign offs this is still true. A majority of the patches I sign off on are done on my own time, so they need to compete with playing with my kids, reading, watching tv, talking to my wife, …. you get the picture. So a few factors come into play.

  • If the person who wrote the patch has asked me to sign off, I will try to do it as soon as possible.
  • If the person has signed off on a patch of mine, I feel obliged to sign off on one of theirs.
  • If the person signs off lots for others, I will feel more inclined to sign off.
  • If the patch is from a new developer, this jumps to the front of my queue.
  • If the patch fixes a security bug, fixes a blocker, or even is something I find really interesting it moves up the queue also.

But finally, in the it’s all about the people vein, I sign off for friends, which luckily is pretty much everyone in the Koha community.

How do you choose what to sign off on?

¹ Bonus points for picking the Artist and song title

Do some signoffs, you know you want to!

Something the Koha project does not suffer from, is lack of features and bug fixes being submitted. Where we do have a bit of a bottleneck is getting those features/fixes their first signoff.

To that end, Biblibre has done some great work with sandboxes to allow people to sign off without needing to have a full Koha development install.

Something I have been working on, and is ready for alpha launch, is a dashboard for the project. What we have come up with is dashboard.koha-community.org which hopefully will help encourage people, and make it easy to see where a good place to help is.

The thing with Koha, is it’s for everyone, and everyone has a role to play in making it better, it’s most definitely not like the old way of building and ILS. The more you put in the more you get out, get involved, own your own software.

 

Bugzilla statistics for June 2012

  • 240 Needs Signoff
  • 194 Signed Off
  • 135 Passed QA
  • 124 Pushed to Master
  • 41 Resolved
  • 67 Failed QA
  • 27 Patch doesn’t apply

Who did all the signoffs?

  1. Katrin Fischer  31
  2. Chris Cormack   28
  3. Kyle M Hall     13
  4. Marc Véron      13
  5. Martin Renvoize 12
  6. Paul Poulain    10
  7. Jonathan Druart 9
  8. Julian Maurice  9
  9. Jared Camins-Esakov     7
  10. Michael Davis   6
  11. M. de Rooy      6
  12. Dobrica Pavlinusic      5
  13. MJ Ray   4
  14. Owen Leonard    3
  15. Liz Rea 3
  16. Adrien SAURAT   3
  17. Matthias Meusburger     3
  18. Alex Arnaud     3
  19. Marijana Glavica        3
  20. Mirko Tietgen   3
  21. Nicole C. Engard        3
  22. Robin Sheat     3
  23. Stacey Walker   2
  24. Melissa Geist   1
  25. Melia Meggs     1
  26. Francois Charbonnier    1
  27. Olugbenga Adara 1
  28. Joy Nelson      1
  29. Irma Birchall   1
  30. Bob Birchall    1
  31. Sophie MEYNIEUX  1
  32. Koha Team Lyon 3        1
  33. Colin Campbell  1
  34. Tomás Cohen Arazi       1
  35. delaye  1