QueryDeviceMessages
Adds tests for `QueryDeviceMessages` and also includes some optimizations to reduce allocations in the DB layer.