예제 #1
0
def test_set_tags(image_factory):
    model = Model()
    view = mock.MagicMock()
    controller = Controller(model=model)
    assert controller.tags == []

    image = image_factory('foo.jpg')
    model.images.add(image)

    controller._view = view

    controller.set_tags({Tag(name="a"), Tag(name="b")})
    assert controller.tags == [Tag(name="a"), Tag(name="b")]
    assert model.tags == {Tag(name="a"), Tag(name="b")}

    view.update_tags.assert_called_once()
    view.reset_mock()

    controller.set_tags({Tag(name="a"), Tag(name="b"), Tag(name="c")})
    assert controller.tags == [Tag(name="a"), Tag(name="b"), Tag(name="c")]
    assert model.tags == {Tag(name="a"), Tag(name="b"), Tag(name="c")}

    view.update_tags.assert_called_once()
    view.reset_mock()

    image.tags = {Tag(name="a"), Tag(name="b"), Tag(name="c")}

    controller.set_tags({Tag(name="a")})
    assert controller.tags == [Tag(name="a")]
    assert model.tags == {Tag(name="a")}

    view.update_tags.assert_called_once()
    view.reset_mock()

    assert image.tags == {Tag(name="a")}
예제 #2
0
def test_add_image(image_factory):
    view = mock.MagicMock()
    controller = Controller(model=Model())
    controller._view = view
    assert not hasattr(controller, '_current_image')

    # adding the first image should automatically set it as current
    controller.add_image(image_factory('foo.jpg'))
    assert controller._current_image.path.name == 'foo.jpg'
    assert [image.path.name for image in controller.images] == ['foo.jpg']

    view.update_images.assert_called_once_with()
    view.reset_mock()

    controller.add_image(image_factory('bar.jpg'))
    assert controller._current_image.path.name == 'foo.jpg'
    assert [image.path.name
            for image in controller.images] == ['bar.jpg', 'foo.jpg']

    view.update_images.assert_called_once_with()
    view.reset_mock()

    # adding the same image twice should fail
    with pytest.raises(Controller.ImageAlreadyPresent,
                       match="foo.jpg is already present"):
        controller.add_image(image_factory('foo.jpg'))
    assert [image.path.name
            for image in controller.images] == ['bar.jpg', 'foo.jpg']

    view.update_images.assert_not_called()
    view.reset_mock()
예제 #3
0
def test_images(image_factory):
    controller = Controller(model=Model())
    assert controller.images == []

    for name in ('foo.jpg', 'bar.jpg', 'baz.jpg'):
        controller.add_image(image_factory(name))

    # images must be ordered alphabetically
    assert [image.path.name for image in controller.images] == [
        'bar.jpg',
        'baz.jpg',
        'foo.jpg',
    ]
예제 #4
0
def test_tags():
    controller = Controller(model=Model())
    assert controller.tags == []

    controller.add_tag(Tag(name="red"))
    assert controller.tags == [Tag(name="red")]

    controller.add_tag(Tag(name="blue"))
    controller.add_tag(Tag(name="green"))
    controller.add_tag(Tag(name="yellow"))

    assert controller.tags == [
        Tag(name="blue"),
        Tag(name="green"),
        Tag(name="red"),
        Tag(name="yellow"),
    ]
예제 #5
0
def test_view_model_reinitialized_on_load(basedir: pathlib.Path, model: Model):
    controller = Controller(model=model)

    controller.save(basedir / 'save.picpick')
    assert controller._view.model == model

    controller.load(basedir / 'save.picpick')
    assert controller._view.model != model
예제 #6
0
def test_load_empty_project(basedir: pathlib.Path):
    model = Model()
    controller = Controller(model=model)

    controller.save(basedir / 'save.picpick')
    controller.load(basedir / 'save.picpick')
    assert controller._model is not model

    assert controller.current_image is None
    assert controller.images == []
    assert controller.tags == []
예제 #7
0
def test_add_tag():
    view = mock.MagicMock()
    controller = Controller(model=Model())
    controller._view = view
    assert controller.tags == []

    controller.add_tag(Tag(name="foo"))

    view.update_tags.assert_called_once_with()
    view.reset_mock()

    controller.add_tag(Tag(name="bar"))

    view.update_tags.assert_called_once_with()
    view.reset_mock()

    assert controller.tags == [Tag(name="bar"), Tag(name="foo")]
예제 #8
0
def test_tag_some_images(basedir, model: Model):
    controller = Controller(model=model)

    assert controller._view._window.title() == "PicPick *"

    # images and tags must be ordered alphabetically
    assert [image.path.name for image in controller.images] == [
        'one.jpg',
        'three.jpg',
        'two.jpg',
    ]

    red = Tag(name='red')
    green = Tag(name='green')
    blue = Tag(name='blue')
    assert controller.tags == [blue, green, red]

    # initially selected image is first one
    assert controller.current_image is not None
    assert controller.current_image.path.name == 'one.jpg'
    assert controller.current_image.tags == set()

    # tag image
    controller.tag_current_image(tag=red)
    controller.tag_current_image(tag=blue)
    assert controller.current_image.tags == {red, blue}

    # select next image
    controller.set_current_image(controller.images[1])
    assert controller.current_image.path.name == 'three.jpg'
    assert controller.current_image.tags == set()

    # tag and untag image
    controller.tag_current_image(red)
    controller.tag_current_image(green)
    assert controller.current_image.tags == {red, green}

    controller.untag_current_image(red)
    assert controller.current_image.tags == {green}

    # save picking to file
    controller.save(basedir / 'save.pickle')
    assert controller._view._window.title() == "PicPick - save.pickle"

    # add tags to last image
    controller.set_current_image(controller.images[2])
    assert controller.current_image.path.name == 'two.jpg'
    assert controller.current_image.tags == set()

    controller.tag_current_image(blue)
    assert controller.current_image.tags == {blue}
    assert controller._view._window.title() == "PicPick - save.pickle*"

    # load previous picking and ensure tagging rollbacked
    controller.load(basedir / 'save.pickle')
    assert controller._view._window.title() == "PicPick - save.pickle"

    # tags an images should be the same
    assert [image.path.name for image in controller.images] == [
        'one.jpg',
        'three.jpg',
        'two.jpg',
    ]
    assert controller.tags == [blue, green, red]

    # because it's the same file, current_image should be the same as saved
    assert controller.current_image.path.name == 'three.jpg'
    assert controller.current_image.tags == {green}

    # last image tags should have been rollbacked
    controller.set_current_image(controller.images[2])
    assert controller.current_image.path.name == 'two.jpg'
    assert controller.current_image.tags == set()
    assert controller._view._window.title() == "PicPick - save.pickle*"

    # first image tags should have remained the same
    controller.set_current_image(controller.images[0])
    assert controller.current_image.path.name == 'one.jpg'
    assert controller.current_image.tags == {red, blue}

    # add tags to last image again
    controller.set_current_image(controller.images[2])
    assert controller.current_image.path.name == 'two.jpg'
    assert controller.current_image.tags == set()

    controller.tag_current_image(red)
    assert controller.current_image.tags == {red}

    # save with save current (not save as) function
    controller.save_current()
    assert controller._view._window.title() == "PicPick - save.pickle"

    # remove tag from last image and load again to ensure save worked
    controller.untag_current_image(red)
    assert controller.current_image.tags == set()

    controller.load(basedir / 'save.pickle')
    assert controller._view._window.title() == "PicPick - save.pickle"

    assert controller.current_image.path.name == 'two.jpg'
    assert controller.current_image.tags == {red}
예제 #9
0
def test_empty_add_one_image_and_tag_it(basedir, image_factory):
    controller = Controller(model=Model())
    assert controller._view._window.title() == "PicPick *"
    assert controller.images == []
    assert controller.tags == []
    assert controller.current_image is None

    controller.save(basedir / 'save.picpick')
    assert controller._view._window.title() == "PicPick - save.picpick"

    controller.add_image(image_factory('foo.jpg'))
    assert [image.path.name for image in controller.images] == ['foo.jpg']
    assert controller.current_image is not None

    controller.add_tag(Tag(name='alpha'))
    controller.add_tag(Tag(name='beta'))
    assert controller.tags == [Tag(name='alpha'), Tag(name='beta')]

    assert controller._view._window.title() == "PicPick - save.picpick*"
    controller.save(basedir / 'save.picpick')
    assert controller._view._window.title() == "PicPick - save.picpick"

    assert controller.current_image.path.name == 'foo.jpg'
    assert controller.current_image.tags == set()

    controller.tag_current_image(Tag(name='alpha'))
    controller.tag_current_image(Tag(name='beta'))
    assert controller.current_image.tags == {
        Tag(name='alpha'), Tag(name='beta')
    }

    assert controller._view._window.title() == "PicPick - save.picpick*"
    controller.save(basedir / 'save.picpick')
    assert controller._view._window.title() == "PicPick - save.picpick"

    # eventually wants to rename the first tag and delete the second
    controller.update_tag(old=Tag(name="alpha"), new=Tag(name="gamma"))
    controller.delete_tag(Tag(name="beta"))

    assert controller.tags == [Tag(name="gamma")]

    # image tags must have also been updated
    assert controller.current_image.tags == {Tag(name="gamma")}
예제 #10
0
def test_empty_add_some_tags(basedir):
    controller = Controller(model=Model())
    assert controller._view._window.title() == "PicPick *"
    assert controller.images == []
    assert controller.tags == []
    assert controller.current_image is None

    # just want to add some tags
    for name in ("alpha", "beta", "gamma"):
        controller.add_tag(Tag(name=name))
    assert controller.tags == [
        Tag(name="alpha"),
        Tag(name="beta"),
        Tag(name="gamma")
    ]

    controller.save(basedir / 'save.picpick')
    assert controller._view._window.title() == "PicPick - save.picpick"

    # eventually wants to remove beta
    controller.delete_tag(Tag(name="beta"))
    assert controller.tags == [Tag(name="alpha"), Tag(name="gamma")]

    assert controller._view._window.title() == "PicPick - save.picpick*"
    controller.save(basedir / 'save.picpick')
    assert controller._view._window.title() == "PicPick - save.picpick"

    # renames alpha to delta
    controller.update_tag(old=Tag(name="alpha"), new=Tag(name="delta"))
    assert controller.tags == [Tag(name="delta"), Tag(name="gamma")]
    assert controller._view._window.title() == "PicPick - save.picpick*"
예제 #11
0
def test_delete_tag(model_factory):
    controller = Controller(model_factory((), ("foo", )))
    assert controller.tags == [Tag(name="foo")]

    view = mock.MagicMock()
    controller._view = view

    controller.delete_tag(Tag(name="foo"))
    assert controller._model.tags == set()

    view.update_tags.assert_called_once_with()

    # no loaded image, hence should not update it
    view.update_current_image_tags.assert_not_called()
    view.reset_mock()

    controller = Controller(model_factory(('pic.jpg', ), ("foo", "bar")))
    assert controller.current_image is not None
    assert controller.tags == [Tag(name="bar"), Tag(name="foo")]

    controller.current_image.tags = {Tag(name="foo"), Tag(name="bar")}

    controller._view = view

    controller.delete_tag(Tag(name="foo"))
    assert controller.tags == [Tag(name="bar")]

    assert controller._model.tags == {Tag(name="bar")}
    assert controller.current_image.tags == {Tag(name="bar")}

    view.update_tags.assert_called_once_with()
    view.update_current_image_tags.assert_called_once_with()
    view.reset_mock()

    controller.delete_tag(Tag(name="bar"))
    assert controller.tags == []

    assert controller._model.tags == set()
    assert controller.current_image.tags == set()

    view.update_tags.assert_called_once_with()
    view.update_current_image_tags.assert_called_once_with()
    view.reset_mock()
예제 #12
0
def test_update_tag(model_factory):
    controller = Controller(model_factory((), ("foo", )))
    assert controller.tags == [Tag(name="foo")]

    view = mock.MagicMock()
    controller._view = view

    controller.update_tag(old=Tag(name="foo"), new=Tag(name="bar"))
    assert controller._model.tags == {Tag(name="bar")}

    view.update_tags.assert_called_once_with()

    # no loaded image, hence should not update it
    view.update_current_image_tags.assert_not_called()
    view.reset_mock()

    controller = Controller(model_factory(('pic.jpg', ), ("foo", "bar")))
    assert controller.current_image is not None

    controller.current_image.tags = {Tag(name="foo"), Tag(name="bar")}

    controller._view = view

    controller.update_tag(Tag(name="bar"), Tag(name="qux"))
    assert controller.tags == [Tag(name="foo"), Tag(name="qux")]

    assert controller._model.tags == {Tag(name="foo"), Tag(name="qux")}
    assert controller.current_image.tags == {Tag(name="foo"), Tag(name="qux")}

    view.update_tags.assert_called_once_with()
    view.update_current_image_tags.assert_not_called()
    view.reset_mock()

    with pytest.raises(Controller.TagAlreadyPresent,
                       match="\"qux\" tag is already present"):
        controller.update_tag(Tag(name="foo"), Tag(name="qux"))

    assert controller.tags == [Tag(name="foo"), Tag(name="qux")]

    assert controller._model.tags == {Tag(name="foo"), Tag(name="qux")}
    assert controller.current_image.tags == {Tag(name="foo"), Tag(name="qux")}

    view.update_tags.assert_not_called()
    view.update_current_image_tags.assert_not_called()
    view.reset_mock()
예제 #13
0
def test_current_image(model: Model):
    controller = Controller(model=Model())
    assert not hasattr(controller, '_current_image')

    controller = Controller(model=model)

    assert controller._current_image.path.name == 'one.jpg'
    controller.set_current_image(controller.images[1])
    assert controller._current_image.path.name == 'three.jpg'
    controller.set_current_image(controller.images[2])
    assert controller._current_image.path.name == 'two.jpg'
    controller.set_current_image(controller.images[0])
    assert controller._current_image.path.name == 'one.jpg'

    controller.set_current_image(None)
    assert not hasattr(controller, '_current_image')