def test_save_resets_need_saving(qtbot, tmpdir):
    """ When saving, the needs_saving flag is reset """
    # Given
    filename = tmpdir.join("test_save_resets_need_saving.txt")
    manager = ProjectFileManager()
    qtbot.add_widget(manager)
    project = ResearchProject(str(filename))
    manager.project = project
    manager.needs_saving = True

    # When
    manager.save_project()

    # Then
    assert manager.needs_saving is False
def test_open_confirms_discard_and_aborts(qtbot, monkeypatch):
    """ When saving, the signal is raised """
    # Given
    manager = ProjectFileManager()
    manager.needs_saving = True
    qtbot.add_widget(manager)
    monkeypatch.delattr(QFileDialog, "getOpenFileName")
    monkeypatch.setattr(manager.project_discard, "exec_",
                        lambda: QMessageBox.Cancel)

    # When
    with qtbot.assertNotEmitted(manager.project_changed):
        manager.open_project()

    # Then
    assert manager.project is None
def test_create_new_saves_after_confirming(qtbot, monkeypatch, tmpdir):
    """ When creating new, confirm discarding unsaved changes will then create new """
    # Given
    filename = tmpdir.join("test_save_resets_need_saving.txt")
    manager = ProjectFileManager()
    manager.needs_saving = True
    monkeypatch.setattr(manager.project_discard, "exec_",
                        lambda: QMessageBox.Ok)
    monkeypatch.setattr(QFileDialog, "getSaveFileName",
                        lambda _, __, ___, ____: (str(filename), False))

    # When
    with qtbot.waitSignals([manager.project_saved]):
        manager.create_new_project()

    # Then
    assert manager.project is not None
    assert manager.project.filename == filename
def test_open_saves_after_confirming(qtbot, monkeypatch, tmpdir):
    """ When creating new, confirm discarding unsaved changes will then create new """
    # Given
    filename = tmpdir.join("test_open_saves_after_confirming.txt")
    filename.write("gedcom: none\n" + "plans: []\n" + "version: '1.0'")
    manager = ProjectFileManager()
    manager.needs_saving = True
    monkeypatch.setattr(manager.project_discard, "exec_",
                        lambda: QMessageBox.Ok)
    monkeypatch.setattr(QFileDialog, "getOpenFileName",
                        lambda _, __, ___, ____: (str(filename), False))

    # When
    with qtbot.waitSignals([manager.project_changed]):
        manager.open_project()

    # Then
    assert manager.project is not None
    assert manager.project.filename == filename