__author__ = 'Ostico <ostico@gmail.com>'

import sys
import os
import unittest

from pyorient.exceptions import *
from pyorient import OrientSocket
from pyorient import OrientRecord
from pyorient.messages.database import *
from pyorient.messages.commands import *
from pyorient.messages.cluster import *
from pyorient.messages.records import *
from pyorient.constants import DB_TYPE_DOCUMENT, QUERY_SYNC, \
    STORAGE_TYPE_PLOCAL, DB_TYPE_GRAPH, STORAGE_TYPE_MEMORY

os.environ['DEBUG'] = "0"
os.environ['DEBUG_VERBOSE'] = "0"
if os.path.realpath( '../' ) not in sys.path:
    sys.path.insert( 0, os.path.realpath( '../' ) )

if os.path.realpath( '.' ) not in sys.path:
    sys.path.insert( 0, os.path.realpath( '.' ) )




class RawMessages_2_TestCase(unittest.TestCase):
    """ Command Test Case """

    def test_record_object(self):
        x = OrientRecord()
        assert x._rid is None
        assert x._version is None
        assert x._class is None

    def test_record_load(self):
        connection = OrientSocket( "localhost", 2424 )

        assert connection.session_id == -1

        # ##################

        msg = DbOpenMessage( connection )

        db_name = "GratefulDeadConcerts"
        cluster_info = msg.prepare(
            (db_name, "admin", "admin", DB_TYPE_DOCUMENT, "")
        ).send().fetch_response()
        assert len(cluster_info) != 0

        def _test_callback(record):
            assert record is not []
            assert record._rid is not None  # assert no exception

        req_msg = RecordLoadMessage( connection )

        res = req_msg.prepare( [ "#11:0", "*:2", _test_callback ] ) \
            .send().fetch_response()