# -*- mode: Python; coding: utf-8 -*-

# Copyright (c) 2002-2011 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Neo4j is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement

import unit_tests
import tempfile, os

class CypherTest(unit_tests.GraphDatabaseTest):
        
    def test_simple_query(self):
        db = self.graphdb
        
        # START SNIPPET: basicCypherQuery
        result = db.query("START n=node(0) RETURN n")
        # END SNIPPET: basicCypherQuery
        
        # START SNIPPET: getCypherResultColumn
        result = db.query("START n=node(0) RETURN n")
        
        # Fetch an iterator for the "n" column
        col = result['n']
        
        for value in col:
            node = value
        
        # We know it's a single result,
        # so we could have done this as well
        node = result['n'].single
        # END SNIPPET: getCypherResultColumn
        
        self.assertEquals(0, node.id)
        
        # START SNIPPET: iterateCypherResult
        result = db.query("START n=node(0) RETURN n")
        
        # Iterate through all result rows
        for row in result:
            node = row['n']
            
        # Or, we can use single here as well
        node = result.single['n']
        # END SNIPPET: iterateCypherResult
        self.assertEquals(0, node.id)
        
    def test_list_columns(self):
        db = self.graphdb
        
        # START SNIPPET: listCypherResultColumns
        result = db.query("START n=node(0) RETURN n,count(n)")
        
        # Get a list of the column names
        columns = result.keys()
        # END SNIPPET: listCypherResultColumns
        
        self.assertEquals(columns[0], 'n')
        self.assertEquals(columns[1], 'count(n)')
        
    def test_parameterized_query(self):
        db = self.graphdb
        
        # START SNIPPET: parameterizedCypherQuery
        result = db.query("START n=node({id}) RETURN n",id=0)
        
        node = result.single['n']
        # END SNIPPET: parameterizedCypherQuery
        
        self.assertEquals(0, node.id)
        
    def test_prepared_queries(self):
        db = self.graphdb
        
        # START SNIPPET: preparedCypherQuery
        get_node_by_id = db.prepare_query("START n=node({id}) RETURN n")
        
        result = db.query(get_node_by_id, id=0)
        
        node = result.single['n']
        # END SNIPPET: preparedCypherQuery
        
        self.assertEquals(0, node.id)
        
        
if __name__ == '__main__':
    unit_tests.unittest.main()

@@ -1209,33 +1209,27 @@
        r
-esult = db.query(
+oot_node = 
 %22START n
@@ -1238,33 +1238,32 @@
 ode(0) RETURN n%22
-)
 %0A        %0A      
@@ -1314,25 +1314,41 @@
      col
- = result
+umn = db.query(root_node)
 %5B'n'%5D%0A  
@@ -1370,20 +1370,22 @@
 for 
-value
+cell
  in col
+umn
 :%0A  
@@ -1405,13 +1405,12 @@
 e = 
-value
+cell
 %0A   
@@ -1429,82 +1429,74 @@
   # 
-We know it's a single result,%0A        # so we could have done this as well
+Coumns support %22single%22:%0A        column = db.query(root_node)%5B'n'%5D
 %0A   
@@ -1511,19 +1511,14 @@
 e = 
-result%5B'n'%5D
+column
 .sin
@@ -1676,33 +1676,27 @@
        r
-esult = db.query(
+oot_node = 
 %22START n
@@ -1705,33 +1705,32 @@
 ode(0) RETURN n%22
-)
 %0A        %0A      
@@ -1784,22 +1784,35 @@
  row in 
-result
+db.query(root_node)
 :%0A      
@@ -1860,34 +1860,74 @@
   # 
-Or, we can use single here
+We know it's a single result,%0A        # so we could have done this
  as 
@@ -1938,38 +1938,51 @@
 %0A        node = 
-result
+db.query(root_node)
 .single%5B'n'%5D%0A   

Fix for breaking cypher tests.