Exemple #1
0
    def test_instance_from(self, app):
        # Given
        tag1 = Tag(label='foo')
        ApiHandler.save(tag1)

        # When
        tag2 = Tag.instance_from({'label': 'foo'})

        # Then
        assert tag1.id == tag2.id
Exemple #2
0
    def test_create_or_modify_retrieves_automatically_with_unique_datum(
            self, app):
        # Given
        tag1 = Tag(label='foo')
        ApiHandler.save(tag1)
        datum = {
            'label': 'foo',
        }

        # When
        tag2 = Tag.create_or_modify(datum)

        # Then
        assert tag2.id == tag1.id
Exemple #3
0
    def test_dictify_with_sync_map(self, app):
        # given
        offer = Offer(name='foo', type='bar')
        tag1 = Tag(label='beep')
        offer_tag1 = OfferTag(offer=offer, tag=tag1)
        tag2 = Tag(label='boop')
        offer_tag2 = OfferTag(offer=offer, tag=tag2)

        # when
        includes = [{'key': 'offerTags', 'includes': ['tag']}]
        offer_dict = as_dict(offer, includes=includes)

        # then
        assert len(offer_dict['offerTags']) == 2
        assert offer_dict['offerTags'][0]['tag']['label'] == 'beep'
        assert offer_dict['offerTags'][1]['tag']['label'] == 'boop'
Exemple #4
0
    def test_create_or_modify_with_relationships_search(self, app):
        # Given
        offer = Offer.create_or_modify(
            {
                '__SEARCH_BY__': 'name',
                'name': 'foo',
                'type': 'bar'
            },
            with_add=True,
            with_flush=True)
        tag = Tag.create_or_modify({
            '__SEARCH_BY__': 'label',
            'label': 'car'
        },
                                   with_add=True,
                                   with_flush=True)

        # When
        offer_tag = OfferTag.create_or_modify({
            '__SEARCH_BY__': ['offer', 'tag'],
            'offer': offer,
            'tag': tag
        })

        #Then
        assert offer.id != None
        assert offer.name == 'foo'
        assert offer.type == 'bar'
        assert tag.id != None
        assert tag.label == 'car'
        assert offer_tag.id == None
Exemple #5
0
    def test_foo(self, app):
        # Given
        TAGS = [{
            'label': 'Accurate',
        }, {
            'label': 'Biased',
        }]
        for tag in TAGS:
            tag.update({
                '__SEARCH_BY__': ['label', 'type'],
                'id':
                '__NEXT_ID_IF_NOT_EXISTS__',
                'scopes': [{
                    '__SEARCH_BY__': ['tagId', 'type'],
                    'tagId': {
                        'humanized': True,
                        'key': 'id',
                        'type': '__PARENT__'
                    },
                    'type': ScopeType.REVIEW
                }],
                'type':
                TagType.QUALIFICATION
            })

        tags1 = []
        for tag_dict in TAGS:
            tag = Tag.create_or_modify(tag_dict)
            tags1.append(tag)
        ApiHandler.save(*tags1)

        # When
        tags2 = []
        for tag_dict in TAGS:
            tag = Tag.create_or_modify(tag_dict)
            tags2.append(tag)
        ApiHandler.save(*tags2)

        assert '/'.join([str(tag.id) for tag in tags1
                         ]) == '/'.join([str(tag.id) for tag in tags2])
        assert '/'.join([str(tag.scopes[0].id) for tag in tags1]) == '/'.join(
            [str(tag.scopes[0].id) for tag in tags2])
Exemple #6
0
    def test_create_or_modify_returns_modified_tag_with_nested_scope(
            self, app):
        # Given
        tag_dict = {
            '__SEARCH_BY__': 'label',
            'label': 'Very High',
            'scopes': [{
                '__SEARCH_BY__': ['type'],
                'type': ScopeType.REVIEW
            }]
        }
        tag1 = Tag(**tag_dict)
        ApiHandler.save(tag1)

        # When
        tag2 = Tag.create_or_modify(tag_dict)
        ApiHandler.save(tag2)

        # Then
        assert tag2.id == tag1.id
        assert len(tag2.scopes) == 1
        assert tag2.scopes[0].tagId == tag2.id
Exemple #7
0
    def test_create_or_modify_returns_created_tag_with_nested_scope(self, app):
        # Given
        tag = Tag.create_or_modify({
            '__SEARCH_BY__': 'label',
            'label': 'Very High',
            'scopes': [{
                'type': ScopeType.REVIEW
            }]
        })

        # When
        ApiHandler.save(tag)

        # Then
        assert tag.scopes[0].tagId == tag.id
Exemple #8
0
def tags():
    tags_ = [
        "Astronomy",
        "Geology",
        "Oceanography",
        "Physics",
        "Earth Science",
        "Chemistry",
        "Botany",
        "Zoology",
        "Biochemistry",
        "Computer Science",
        "Sports",
    ]
    for tag in tags_:
        db.session.add(Tag(name=tag))
        db.session.commit()
Exemple #9
0
    def test_dictify_with_async_map_join(self, app):
        # given
        offer_tags_count = 10
        offer = Offer(name='foo', type='bar')
        offer_tags = []
        for index in range(0, offer_tags_count):
            tag = Tag(label=str(index))
            offer_tags.append(OfferTag(offer=offer, tag=tag))

        # when
        includes = [{'key': '|offerTags', 'includes': ['tag']}]
        offer_dict = as_dict(offer, includes=includes)

        # then
        assert len(offer_dict['offerTags']) == offer_tags_count
        for index in range(0, offer_tags_count):
            assert offer_dict['offerTags'][index]['tag']['label'] == str(index)
            assert offer_dict['offerTags'][index]['tag']['sleptFoo'] == 0
Exemple #10
0
    def test_dictify_with_async_map_key(self, app):
        # given
        offer_tags_count = 10
        offer = Offer(name='foo', type='bar')
        offer_tags = []
        for index in range(0, offer_tags_count):
            tag = Tag(label=str(index))
            offer_tags.append(OfferTag(offer=offer, tag=tag))

        # when
        includes = ['|offerTags']
        offer_dict = as_dict(offer, includes=includes)

        # then
        assert len(offer_dict['offerTags']) == offer_tags_count
        for index in range(0, offer_tags_count):
            assert offer_dict['offerTags'][index]['tagId'] == offer_tags[
                index].id
Exemple #11
0
    def test_create_or_modify_with_flatten_nested_unique_existing_datum(
            self, app):
        # Given
        tag = Tag(label='bar')
        ApiHandler.save(tag)
        datum = {
            'name': 'foo',
            'offerTags.0.tag.label': 'bar',
            'type': 'bar',
        }

        # When
        offer = Offer.create_or_modify(datum)

        # Then
        for (key, value) in datum.items():
            if key.endswith('__SEARCH_BY__'):
                continue
            assert offer.get(key) == value
        assert offer.offerTags[0].tag.id == tag.id
Exemple #12
0
    def test_dictify_with_custom_async_map(self, app):
        # given
        offer_tags_count = 10
        offer = Offer(name='foo', type='bar')
        offer_tags = []
        for index in range(0, offer_tags_count):
            tag = Tag(label=str(index))
            offer_tags.append(OfferTag(offer=offer, tag=tag))

        # when
        includes = [{'key': '|offerTags', 'includes': ['tag']}]
        with ThreadPoolExecutor(max_workers=5) as executor:
            offer_dict = as_dict(offer,
                                 async_map=executor.map,
                                 includes=includes)

            # then
            assert len(offer_dict['offerTags']) == offer_tags_count
            for index in range(0, offer_tags_count):
                assert offer_dict['offerTags'][index]['tag']['label'] == str(
                    index)
                assert offer_dict['offerTags'][index]['tag']['sleptFoo'] == 0