Example #1
0
def search_api(device_name):
    """
    Test endpoint for development
    """
    found_devices_normal = Device.search(device_name).all()
    found_devices_similar = Device.search_order_by_similarity(device_name).all()
    found_devices_array = Device.search_by_array(device_name).all()
    found_devices_levenshtein = Device.search_levenshtein(device_name).all()
    return jsonify(results={
        'normal': [device.name for device in found_devices_normal],
        'similar': [device.name for device in found_devices_similar],
        'array': [device.name for device in found_devices_array],
        'levenshtein': [device.name for device in found_devices_levenshtein],
    })
    def test_create(self, db, sample_device, some_devices):
        assert "search" in dir(Device)

        # Normal search
        q = Device.query.filter(Device.name.like('iphone'))
        assert len(q.all()) == 0
        assert 'LIKE' in str(q)

        # Fuzzy search
        q = Device.query.filter(Device.name.op('%%')('iphone '))
        assert len(q.all()) > 0
        assert 'LIKE' not in str(q)

        # Similarity
        result = Device.search_order_by_similarity('iPhone X')
        print(result, result.all())
        result = result.all()
        assert result[0].name == 'iPhone X'