Ejemplo n.º 1
0
    def test_duplicates(self):
        user = model_factories.UserFactory()
        sticker = model_factories.StickerFactory()
        label = model_factories.LabelFactory()
        association = model_factories.AssociationFactory(user=user,
                                                         sticker=sticker,
                                                         label=label)

        with pytest.raises(models.ObjectAlreadyExistsError):
            user_duplicate = model_factories.UserFactory(user_id=user.id)

        with pytest.raises(models.ObjectAlreadyExistsError):
            sticker_duplicate = model_factories.StickerFactory(
                sticker_id=sticker.id)

        with pytest.raises(models.ObjectAlreadyExistsError):
            label_duplicate = model_factories.LabelFactory(text=label.text)

        with pytest.raises(models.ObjectAlreadyExistsError):
            association_duplicate = model_factories.AssociationFactory(
                user=user, sticker=sticker, label=label)

        assert models.User.count() == 1
        assert models.Sticker.count() == 1
        assert models.Label.count() == 1
        assert models.Association.count() == 1
Ejemplo n.º 2
0
    def test_insertion_overlapping(self):
        user = model_factories.UserFactory()
        sticker = model_factories.StickerFactory()
        label_a = model_factories.LabelFactory()
        association_a = models.Association(user=user,
                                           sticker=sticker,
                                           label=label_a)

        label_b = model_factories.LabelFactory()
        association_b = models.Association(user=user,
                                           sticker=sticker,
                                           label=label_b)

        assert models.User.id_exists(user.id)
        assert models.User.count() == 1

        assert models.Sticker.id_exists(sticker.id)
        assert models.Sticker.count() == 1

        assert models.Label.id_exists(label_a.id)
        assert models.Label.id_exists(label_b.id)
        assert models.Label.count() == 2

        assert models.Association.id_exists(association_a.id)
        assert models.Association.id_exists(association_b.id)
        assert models.Association.count() == 2

        assert models.Association.count("user_id", user.id) == 2
        assert models.Association.count("sticker_id", sticker.id) == 2
        assert models.Association.count("label_id") == 2
Ejemplo n.º 3
0
    def test_get_usage_count_for_nonexistent_association(self):
        nonexistent_sticker_id = "0"
        nonexistent_label = "label"
        nonexistent_user_id = 0

        nonexistent_without_user_id = models.Association.get_usage_count(
            nonexistent_sticker_id, nonexistent_label)
        assert nonexistent_without_user_id == 0

        nonexistent_with_user_id = models.Association.get_usage_count(
            nonexistent_sticker_id, nonexistent_label, nonexistent_user_id)
        assert nonexistent_with_user_id == 0

        valid_label = model_factories.LabelFactory().text
        valid_sticker_id = model_factories.StickerFactory().id
        valid_user_id = model_factories.UserFactory().id

        nonexistent_sticker_count = models.Association.get_usage_count(
            nonexistent_sticker_id, valid_label, valid_user_id)
        assert nonexistent_sticker_count == 0

        nonexistent_label_count = models.Association.get_usage_count(
            valid_sticker_id, nonexistent_label, valid_user_id)
        assert nonexistent_label_count == 0

        nonexistent_user_count = models.Association.get_usage_count(
            valid_sticker_id, valid_label, nonexistent_user_id)
        assert nonexistent_user_count == 0
Ejemplo n.º 4
0
    def test_increment_usage(self):
        label = model_factories.LabelFactory()
        association = model_factories.AssociationFactory(label=label)
        assert association.uses == 0

        models.Association.increment_usage(association.user_id,
                                           association.sticker_id,
                                           [label.text])
        assert association.uses == 1
Ejemplo n.º 5
0
    def test_get_usage_count_across_users(self):
        sticker = model_factories.StickerFactory()
        label = model_factories.LabelFactory()
        associations = model_factories.AssociationFactory.build_batch(
            3, sticker=sticker, label=label)

        associations_unique = \
            model_factories.AssociationFactory.build_batch(5)

        for association in associations + associations_unique:
            association.uses = 1
        models.database.session.flush()

        count = models.Association.get_usage_count(sticker.id, label.text)
        assert count == len(associations)
Ejemplo n.º 6
0
    def test_get_usage_count(self):
        user = model_factories.UserFactory()
        sticker = model_factories.StickerFactory()
        label = model_factories.LabelFactory()
        association = model_factories.AssociationFactory(user=user,
                                                         sticker=sticker,
                                                         label=label)

        usage = models.Association.get_usage_count(sticker.id, label.text,
                                                   user.id)
        assert usage == 0

        new_usage = 5
        association.uses = new_usage
        usage = models.Association.get_usage_count(sticker.id, label.text,
                                                   user.id)
        assert usage == new_usage
Ejemplo n.º 7
0
    def test_insertion_basic(self):
        user = model_factories.UserFactory()
        sticker = model_factories.StickerFactory()
        label = model_factories.LabelFactory()
        association = model_factories.AssociationFactory(user=user,
                                                         sticker=sticker,
                                                         label=label)

        assert models.User.id_exists(user.id)
        assert models.User.count() == 1

        assert models.Sticker.id_exists(sticker.id)
        assert models.Sticker.count() == 1

        assert models.Label.id_exists(label.id)
        assert models.Label.count() == 1

        assert models.Association.id_exists(association.id)
        assert models.Association.count() == 1
Ejemplo n.º 8
0
    def test_get_existing_label_by_text(self):
        label_text = "label2"
        label = model_factories.LabelFactory(text=label_text)

        retrieved_label = models.Label.get_or_create(label_text, get_only=True)
        assert retrieved_label == label