def test_save_emits_signal(qtbot, tmpdir):
    """ When saving, the signal is raised """
    # Given
    filename = tmpdir.join("test_save_resets_need_saving.txt")
    manager = ProjectFileManager()
    qtbot.add_widget(manager)
    project = ResearchProject(str(filename))
    manager.project = project

    # When
    with qtbot.waitSignals([manager.project_saved]):
        manager.save_project()
def test_unlink_gedcom_does_nothing_if_no_gedcom_link_set(qtbot):
    """ If there is no gedcom link, nothing should happen """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    # monkeypatch.delattr(manager.gedcom_discard, "exec_")

    # when
    with qtbot.assertNotEmitted(manager.project_changed):
        manager.unlink_gedcom_file()

    # Then
    assert manager.project.gedcom == ""
def test_link_gedcom_does_nothing_on_cancel(monkeypatch, qtbot):
    """ When the fileopen dialog is cancelled, the gedcom path is not changed """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    monkeypatch.setattr(QFileDialog, "getOpenFileName",
                        lambda _, __, ___, ____: ("", False))

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

    # Then
    assert manager.project.gedcom == ""
def test_save_writes_project(qtbot, tmpdir):
    """ When saving, the file is written"""
    # Given
    filename = tmpdir.join("test_save_resets_need_saving.txt")
    manager = ProjectFileManager()
    qtbot.add_widget(manager)
    project = ResearchProject(str(filename))
    manager.project = project

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

    # Then
    assert filename.read() != ""
def test_unlink_discard_cancel_makes_no_change(monkeypatch, qtbot):
    """ When a link exists and the confirmation dialog cancels, nothing should happen """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    manager.project.gedcom = "Foo"
    monkeypatch.setattr(manager.gedcom_discard, "exec_",
                        lambda: QMessageBox.Cancel)

    # when
    with qtbot.assertNotEmitted(manager.project_changed):
        manager.unlink_gedcom_file()

    # Then
    assert manager.project.gedcom == "Foo"
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_unlink_discard_ok_removes_link(monkeypatch, qtbot):
    """ When a link exists and the confirmation dialog accepted, the link is removed """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    manager.project.gedcom = "Foo"
    monkeypatch.setattr(manager.gedcom_discard, "exec_",
                        lambda: QMessageBox.Ok)

    # when
    with qtbot.waitSignals([manager.project_changed]):
        manager.unlink_gedcom_file()

    # Then
    assert manager.project.gedcom == ""
    assert manager.needs_saving is True
def test_link_gedcom_sets_selected_link(monkeypatch, qtbot):
    """ When a new link is selected it should be set on the project """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    file_name = "Foo"
    monkeypatch.setattr(QFileDialog, "getOpenFileName",
                        lambda _, __, ___, ____: (file_name, True))

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

    # Then
    assert manager.project.gedcom == file_name
    assert manager.needs_saving is True
def test_link_gedcom_discard_cancel_stops(monkeypatch, qtbot):
    """ When a link already exists, ask the user to overwrite it and stop on cancel """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    manager.project.gedcom = "Foo"
    monkeypatch.delattr(QFileDialog, "getOpenFileName")
    monkeypatch.setattr(manager.gedcom_discard, "exec_",
                        lambda: QMessageBox.Cancel)

    # when
    with qtbot.assertNotEmitted(manager.project_changed):
        manager.link_gedcom_file()

    # Then
    assert manager.project.gedcom == "Foo"
def test_save_as_changes_filename(qtbot, tmpdir, monkeypatch):
    """ When saving, the signal is raised """
    # Given
    filename = tmpdir.join("test_save_resets_need_saving.txt")
    manager = ProjectFileManager()
    qtbot.add_widget(manager)
    project = ResearchProject("")
    manager.project = project

    monkeypatch.setattr(QFileDialog, "getSaveFileName",
                        lambda _, __, ___, ____: (str(filename), True))

    # When
    manager.save_project_as()

    # Then
    assert manager.project.filename == str(filename)
def test_save_as_aborts_on_empty_filename(qtbot, monkeypatch):
    """ When saving, the signal is raised """
    # Given
    filename = "Foo"
    manager = ProjectFileManager()
    qtbot.add_widget(manager)
    project = ResearchProject(filename)
    manager.project = project

    monkeypatch.setattr(QFileDialog, "getSaveFileName",
                        lambda _, __, ___, ____: ("", True))

    # When
    with qtbot.assertNotEmitted(manager.project_saved):
        manager.save_project_as()

    # Then
    assert manager.project.filename == filename
def test_link_gedcom_discard_ok_proceeds(monkeypatch, qtbot):
    """ When a link already exists, ask the user to overwrite it and continue on ok """
    # Given
    manager = ProjectFileManager()
    manager.project = ResearchProject("")
    manager.project.gedcom = "Bar"
    file_name = "Foo"
    monkeypatch.setattr(manager.gedcom_discard, "exec_",
                        lambda: QMessageBox.Ok)
    monkeypatch.setattr(QFileDialog, "getOpenFileName",
                        lambda _, __, ___, ____: (file_name, True))

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

    # Then
    assert manager.project.gedcom == file_name
    assert manager.needs_saving is True