I have some code that reads five numbers from a database:
class Repository extends DatabaseRepository
{
function getCoefficients(string $model)
{
return $this->getDatabaseLink()->query("
select a, b, c, d, e from coefficients
where model = ? and active = 1
", $model)->getSingleResult();
}
}
Calling the above method returns something like this:
array (
'a' => '0.0001',
'b' => '0.001',
'c' => '0.01',
'd' => '0.1',
'e' => '1.0'
)
The returned numbers can change over time, based on which particular record is active
at any one time.
How do I write a unit test for this? Should I write one? What do I test for? To avoid dealing with the live database, I can mock up the database, but then what will I be really testing?
What I have now
For now I went ahead and wrote a test, which engages a live database and tests for the exact numbers that are returned, but that test will break as soon as relevant data changes in the database or if database implementation changes (since my test has to call and set up the live database).
Specific Questions
- What should I be testing for in a Repository level?
- How do I deal with the fact that data returned by live database can change?
- Should a unit test be written for this code at all?