Here are changes to make set_field() not signal dirty books if nothing actually changed. If they work for you then I will make a pull request to give them to Kovid. The changes in formatter_functions aren't part of the main changes, but instead make testing for being in the gui more robust.
My test function does set_metadata(get_metadata()) on evey book in the
Code:
target library. Run it with calibre-debug -e test.py path-to-library
Note that it really does update metadata so you should keep a backup if you want repeatable tests. For example, in one of my libraries setting the comments field did some cleanup so a change was signaled. On the second run no cleanup was needed and no change was signaled.
I also checked the changes using the calibre test suite.