Dependency Injection in SQL Monitor

Michael Clark
Jan 9, 2019 · 7 min read
Image for post
Image for post
Picture source https://flic.kr/p/gwQtNW (Licence under CC BY 2.0)

It’s faster to add new components

It’s faster to fix problems

It’s easier to understand SQL Monitor’s startup

We tested the “untestable”

Example — Improving backup tests

[Test]
public void GetMaxRpoByDatabase_WhenOneBackupInWindowAndOneOutsideAndLatestBackupHasWorstRpo_ThenUseLatestBackupFinish()
{
var now = new DateTime(2018, 02, 10);
var windowStartDate = now.AddDays(-BackupsRepository.MaxRpoWindowLength);
var lastBackupFinished = windowStartDate.AddDays(1);
var maxRpoBaseQuery = $@"
SELECT 1 AS DatabaseId, utils.DateTimeToTicks('{lastBackupFinished:s}') AS FinishDate
UNION
SELECT 1 AS DatabaseId, utils.DateTimeToTicks('{windowStartDate.AddDays(-1):s}') AS FinishDate
";
var maxRpoByDatabase = GetMaxRpoByDatabase(now, maxRpoBaseQuery);
var result = maxRpoByDatabase.SingleOrDefault(r=>r.DatabaseId == 1);
Assert.That(result, Is.Not.Null);
Assert.That(result.From, Is.EqualTo(lastBackupFinished));
Assert.That(result.To, Is.EqualTo(now));
}
[Test]
public void GetLatestByDatabase_WhenOnlyOneBackupAndInWindow_ThenMaxRpoShouldBeNowMinusBackupFinish()
{
var backupFinished = s_DateTime.AddDays(-BackupsRepository.MaxRpoWindowLength + 0.25);
AddDatabaseBackupSample(backupFinished);
AddDatabaseSeenSample(s_DateTime);

var databaseBackupInfo = GetLatestByDatabase();
Assert.That(databaseBackupInfo.MaxRpoFrom, Is.EqualTo(backupFinished));
Assert.That(databaseBackupInfo.MaxRpoTo, Is.EqualTo(s_DateTime));
}

Recipe for DI-ing SQL Monitor

Things we learned

Read more

Ingeniously Simple

How Redgate build ingeniously simple products, from…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store