'Sets up a before-state for groups and permissions in tests, which
can be checked afterward to ensure that a failed atomic
operation has not had any side effects.'

def set_group_permissions(self, group_permissions):

    content_type = ContentType.objects.get_for_model(Group)
    for (group_name, permission_codenames) in group_permissions.items():
        group = Group.objects.create(name=group_name)
        for codename in permission_codenames:
            group.permissions.add(Permission.objects.get(content_type=content_type, codename=codename))
