Skip to content

Instantly share code, notes, and snippets.

@JayBazuzi
Last active December 18, 2021 00:48
Show Gist options
  • Save JayBazuzi/3cbd1d60f746c6cf434f32aeb6c64d85 to your computer and use it in GitHub Desktop.
Save JayBazuzi/3cbd1d60f746c6cf434f32aeb6c64d85 to your computer and use it in GitHub Desktop.
interface ITaxInfo {
async GetTaxInfo(sin: SIN) -> TaxInfo?
class Error {}
}
abstract class TaxInfoTests(subject: ITaxInfo) {
Null_when_SIN_does_not_exist() {
result = subject.GetTaxInfo(A_SIN_WHICH_DOES_NOT_EXIST);
Assert.IsNull(result)
}
class Fake : TaxInfoTests(InMemoryTaxInfo()) {}
@Explicit("Integration test")
class Live : TaxInfoTests(CanadianRevenueAgencyTaxInfo()) {}
}
class CanadianRevenueAgencyTaxInfo : ITaxInfo? {
async ITaxInfo.GetTaxInfo(sin: SIN) -> TaxInfo? => {
val response = await HTTP.Request("http://cra.ca/taxinfo/?sin={sin}")
if (!response.success) return None
return Decode(await response.ReadContent());
}
}
class InMemoryTaxInfo : ITaxInfo {
public TaxInfosBySSN: Map<SIN, TaxInfo> = {
A_SIN -> A_TAX_INFO,
ANOTHER_SIN -> ANOTHER_TAX_INFO,
}
async ITaxInfo.GetTaxInfo(sin: SIN) -> TaxInfo? => TaxInfosBySSN.TryGet(sin)
}
class NoTaxInfo : ITaxInfo {
async ITaxInfo.GetTaxInfo(sin: SIN) -> TaxInfo? => None
}
class VerySlowTaxInfo : InMemoryTaxInfo {
async ITaxInfo.GetTaxInfo(sin: SIN) -> TaxInfo? => {
await Async.Wait(TimeSpan.Minutes(10));
return await base.GetTaxInfo(sin);
}
}
class OfflineTaxInfo : ITaxInfo {
async ITaxInfo.GetTaxInfo(sin: SIN) -> TaxInfo? => throw ITaxInfo.Error()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment