'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
'''
import pytest

from gremlin_python.driver.protocol import GremlinServerError
from gremlin_python.driver.client import Client
from gremlin_python.driver.protocol import GremlinServerError
from gremlin_python.driver.request import RequestMessage
from gremlin_python.process.strategies import OptionsStrategy
from gremlin_python.process.graph_traversal import __
from gremlin_python.structure.graph import Graph

__author__ = 'David M. Brown (davebshow@gmail.com)'


def test_connection(connection):
    g = Graph().traversal()
    t = g.V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    results_set = connection.write(message).result()
    future = results_set.all()
    results = future.result()
    assert len(results) == 6
    assert isinstance(results, list)
    assert results_set.done.done()
    assert 'host' in results_set.status_attributes


def test_client_simple_eval(client):
    assert client.submit('1 + 1').all().result()[0] == 2


def test_client_simple_eval_bindings(client):
    assert client.submit('x + x', {'x': 2}).all().result()[0] == 4


def test_client_eval_traversal(client):
    assert len(client.submit('g.V()').all().result()) == 6


def test_client_error(client):
    try:
        # should fire an exception
        client.submit('1/0').all().result()
        assert False
    except GremlinServerError as ex:
        assert 'exceptions' in ex.status_attributes
        assert 'stackTrace' in ex.status_attributes


def test_client_connection_pool_after_error(client):
    # Overwrite fixture with pool_size=1 client
    client = Client('ws://localhost:45940/gremlin', 'gmodern', pool_size=1)

    try:
        # should fire an exception
        client.submit('1/0').all().result()
        assert False
    except GremlinServerError as gse:
        # expecting the pool size to be 1 again after query returned
        assert gse.status_code == 597
        assert client.available_pool_size == 1


def test_client_bytecode(client):
    g = Graph().traversal()
    t = g.V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    result_set = client.submit(message)
    assert len(result_set.all().result()) == 6


def test_client_bytecode_options(client):
    # smoke test to validate serialization of OptionsStrategy. no way to really validate this from an integration
    # test perspective because there's no way to access the internals of the strategy via bytecode
    g = Graph().traversal()
    t = g.withStrategies(OptionsStrategy(options={"x": "test", "y": True})).V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    result_set = client.submit(message)
    assert len(result_set.all().result()) == 6
    ##
    t = g.with_("x", "test").with_("y", True).V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    result_set = client.submit(message)
    assert len(result_set.all().result()) == 6


def test_iterate_result_set(client):
    g = Graph().traversal()
    t = g.V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    result_set = client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 6


def test_client_async(client):
    g = Graph().traversal()
    t = g.V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    future = client.submitAsync(message)
    result_set = future.result()
    assert len(result_set.all().result()) == 6


def test_connection_share(client):
    # Overwrite fixture with pool_size=1 client
    client = Client('ws://localhost:45940/gremlin', 'gmodern', pool_size=1)
    g = Graph().traversal()
    t = g.V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    message2 = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    future = client.submitAsync(message)
    future2 = client.submitAsync(message2)

    result_set2 = future2.result()
    assert len(result_set2.all().result()) == 6

    # This future has to finish for the second to yield result - pool_size=1
    assert future.done()
    result_set = future.result()
    assert len(result_set.all().result()) == 6


def test_multi_conn_pool(client):
    g = Graph().traversal()
    t = g.V()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    message2 = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}})
    client = Client('ws://localhost:45940/gremlin', 'g', pool_size=1)
    future = client.submitAsync(message)
    future2 = client.submitAsync(message2)

    result_set2 = future2.result()
    assert len(result_set2.all().result()) == 6

    # with connection pool `future` may or may not be done here
    result_set = future.result()
    assert len(result_set.all().result()) == 6


def test_big_result_set(client):
    g = Graph().traversal()
    t = g.inject(1).repeat(__.addV('person').property('name', __.loops())).times(20000).count()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 1

    t = g.V().limit(10)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 10

    t = g.V().limit(100)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 100

    t = g.V().limit(1000)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 1000

    t = g.V().limit(10000)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 10000


def test_big_result_set_secure(secure_client):
    g = Graph().traversal()
    t = g.inject(1).repeat(__.addV('person').property('name', __.loops())).times(20000).count()
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = secure_client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 1

    t = g.V().limit(10)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = secure_client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 10

    t = g.V().limit(100)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = secure_client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 100

    t = g.V().limit(1000)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = secure_client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 1000

    t = g.V().limit(10000)
    message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'g'}})
    result_set = secure_client.submit(message)
    results = []
    for result in result_set:
        results += result
    assert len(results) == 10000

[((35, 14, 35, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(35, 29, 35, 40): '"""traversal"""', (35, 42, 35, 52): '"""bytecode"""', (35, 54, 35, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((69, 13, 69, 75), 'gremlin_python.driver.client.Client', 'Client', (), '', False, 'from gremlin_python.driver.client import Client\n'), ((84, 14, 84, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(84, 29, 84, 40): '"""traversal"""', (84, 42, 84, 52): '"""bytecode"""', (84, 54, 84, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((94, 14, 94, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(94, 29, 94, 40): '"""traversal"""', (94, 42, 94, 52): '"""bytecode"""', (94, 54, 94, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((99, 14, 99, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(99, 29, 99, 40): '"""traversal"""', (99, 42, 99, 52): '"""bytecode"""', (99, 54, 99, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((107, 14, 107, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(107, 29, 107, 40): '"""traversal"""', (107, 42, 107, 52): '"""bytecode"""', (107, 54, 107, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((118, 14, 118, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(118, 29, 118, 40): '"""traversal"""', (118, 42, 118, 52): '"""bytecode"""', (118, 54, 118, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((126, 13, 126, 75), 'gremlin_python.driver.client.Client', 'Client', (), '', False, 'from gremlin_python.driver.client import Client\n'), ((129, 14, 129, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(129, 29, 129, 40): '"""traversal"""', (129, 42, 129, 52): '"""bytecode"""', (129, 54, 129, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((130, 15, 130, 108), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(130, 30, 130, 41): '"""traversal"""', (130, 43, 130, 53): '"""bytecode"""', (130, 55, 130, 107): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((146, 14, 146, 107), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(146, 29, 146, 40): '"""traversal"""', (146, 42, 146, 52): '"""bytecode"""', (146, 54, 146, 106): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((147, 15, 147, 108), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(147, 30, 147, 41): '"""traversal"""', (147, 43, 147, 53): '"""bytecode"""', (147, 55, 147, 107): "{'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'gmodern'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((148, 13, 148, 69), 'gremlin_python.driver.client.Client', 'Client', (), '', False, 'from gremlin_python.driver.client import Client\n'), ((163, 14, 163, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(163, 29, 163, 40): '"""traversal"""', (163, 42, 163, 52): '"""bytecode"""', (163, 54, 163, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((171, 14, 171, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(171, 29, 171, 40): '"""traversal"""', (171, 42, 171, 52): '"""bytecode"""', (171, 54, 171, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((179, 14, 179, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(179, 29, 179, 40): '"""traversal"""', (179, 42, 179, 52): '"""bytecode"""', (179, 54, 179, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((187, 14, 187, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(187, 29, 187, 40): '"""traversal"""', (187, 42, 187, 52): '"""bytecode"""', (187, 54, 187, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((195, 14, 195, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(195, 29, 195, 40): '"""traversal"""', (195, 42, 195, 52): '"""bytecode"""', (195, 54, 195, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((206, 14, 206, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(206, 29, 206, 40): '"""traversal"""', (206, 42, 206, 52): '"""bytecode"""', (206, 54, 206, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((214, 14, 214, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(214, 29, 214, 40): '"""traversal"""', (214, 42, 214, 52): '"""bytecode"""', (214, 54, 214, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((222, 14, 222, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(222, 29, 222, 40): '"""traversal"""', (222, 42, 222, 52): '"""bytecode"""', (222, 54, 222, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((230, 14, 230, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(230, 29, 230, 40): '"""traversal"""', (230, 42, 230, 52): '"""bytecode"""', (230, 54, 230, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((238, 14, 238, 101), 'gremlin_python.driver.request.RequestMessage', 'RequestMessage', ({(238, 29, 238, 40): '"""traversal"""', (238, 42, 238, 52): '"""bytecode"""', (238, 54, 238, 100): "{'gremlin': t.bytecode, 'aliases': {'g': 'g'}}"}, {}), "('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases':\n    {'g': 'g'}})", False, 'from gremlin_python.driver.request import RequestMessage\n'), ((33, 8, 33, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((82, 8, 82, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((92, 8, 92, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((105, 8, 105, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((116, 8, 116, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((127, 8, 127, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((144, 8, 144, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((161, 8, 161, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((204, 8, 204, 15), 'gremlin_python.structure.graph.Graph', 'Graph', ({}, {}), '()', False, 'from gremlin_python.structure.graph import Graph\n'), ((93, 25, 93, 74), 'gremlin_python.process.strategies.OptionsStrategy', 'OptionsStrategy', (), '', False, 'from gremlin_python.process.strategies import OptionsStrategy\n'), ((162, 62, 162, 72), 'gremlin_python.process.graph_traversal.__.loops', '__.loops', ({}, {}), '()', False, 'from gremlin_python.process.graph_traversal import __\n'), ((205, 62, 205, 72), 'gremlin_python.process.graph_traversal.__.loops', '__.loops', ({}, {}), '()', False, 'from gremlin_python.process.graph_traversal import __\n'), ((162, 27, 162, 44), 'gremlin_python.process.graph_traversal.__.addV', '__.addV', ({(162, 35, 162, 43): '"""person"""'}, {}), "('person')", False, 'from gremlin_python.process.graph_traversal import __\n'), ((205, 27, 205, 44), 'gremlin_python.process.graph_traversal.__.addV', '__.addV', ({(205, 35, 205, 43): '"""person"""'}, {}), "('person')", False, 'from gremlin_python.process.graph_traversal import __\n')]