Moved scan loop into a function which returns affected document count, added test to check all rows are affected by scan