It's impossible to catch dopers using drug tests. There are simply too many substances to test.
In California, Horse Racing is controlled tighter than the Pentagon controls nuclear weapons. By California state law, dozens of detectives with law enforcement career track, college degrees, at least 20 years of experience each, are present at the race track, and when horses are offloaded from the airport (LAX for Breeder's Cup for instance), etc. The jockeys, grooms, trainers, owners, all have extensive personal background checks made. By law, only the state veterinarian many inject a substance into any horse, or draw blood. The state detectives stand there next to the state vet and are handed samples from horses and jockeys, then personally package the items, and personally drive and deliver the samples to Fed Ex for shipping to the lab for testing. The stables, grounds, etc. are secured by the state detectives. The motivation is to prevent cheating and to keep organized crime out of horse racing. The cost of course is enormous. Huge fees are deducted from ticket prices and prize winnings to pay the police. Taxpayer funds are not used at all.
But even with these extreme Gestapo style measures, doping still occurs. The labs can't detect all substances.