Refactor building graphs in tests.