Setting euler rotates camera, but setting quaternion doesn't
It works with eulers because rotation_mode is set to XYZ by default.

Either change it to QUATERNION or modify matrix_basis (or some of the other matrix attributes). You can easily convert any quaternion to a matrix using its method to_matrix and resize it with to_4x4

I.e.

mat = q.to_matrix().to_4x4()
mat.translation = cam.location
cam.matrix_basis = mat