# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1437145933 -7200
#      Fri Jul 17 17:12:13 2015 +0200
# Node ID d02ad6785eb92229c03a4047fa0bd091f4c37dcb
# Parent  db7398f9e83b5554a6b88d55e0f6706eaae7a90d
[test] Add a recursive relation (`comments') and extend test suite accordingly

diff --git a/test/data/schema.py b/test/data/schema.py
--- a/test/data/schema.py
+++ b/test/data/schema.py
@@ -76,6 +76,17 @@
     composite = 'subject'
 
 
+class Comment(EntityType):
+    """A comment comments things"""
+
+
+class comments(RelationDefinition):
+    subject = 'Comment'
+    object = ('Comment', 'Anecdote')
+    composite = 'object'
+    cardinality = '1*'
+
+
 class Group(EntityType):
     """A collection of individual agents"""
 
diff --git a/test/test_compound.py b/test/test_compound.py
--- a/test/test_compound.py
+++ b/test/test_compound.py
@@ -43,6 +43,17 @@
         rels = list(graph.parent_relations('OnlineAccount'))
         self.assertEqual(rels, [(('account', 'object'), ['Agent'])])
 
+    def test_parent_relations_comment(self):
+        graph = CompositeGraph(self.schema)
+        rels = list(graph.parent_relations('Comment'))
+        self.assertEqual(rels, [(('comments', 'subject'),
+                                 ['Comment', 'Anecdote'])])
+
+    def test_child_relations_comment(self):
+        graph = CompositeGraph(self.schema)
+        rels = list(graph.child_relations('Comment'))
+        self.assertEqual(rels, [(('comments', 'object'), ['Comment'])])
+
     def test_child_relations_singleton(self):
         graph = CompositeGraph(self.schema)
         rels = list(graph.child_relations('OnlineAccount'))
@@ -73,17 +84,22 @@
                 ('event', 'object'): ['Biography'],
                 ('narrated_by', 'subject'): ['Agent'],
             },
+            'Comment': {
+                ('comments', 'subject'): ['Anecdote', 'Comment'],
+            },
         }
         self.assertEqual(structure, expected)
 
     def test_parent_structure_anecdote(self):
         graph = CompositeGraph(self.schema)
-        self.set_description('Anecdote')
         structure = graph.parent_structure('Anecdote')
         expected = {
             'Event': {
                 ('relates', 'object'): ['Anecdote'],
             },
+            'Comment': {
+                ('comments', 'subject'): ['Anecdote', 'Comment'],
+            }
         }
         self.assertEqual(structure, expected)
 
@@ -109,6 +125,8 @@
             anecdote1 = cnx.create_entity('Anecdote', reverse_event=bio)
             anecdote2 = cnx.create_entity('Anecdote', reverse_event=bio, narrated_by=agent,
                                           relates=event)
+            comment1 = cnx.create_entity('Comment', comments=anecdote1)
+            comment2 = cnx.create_entity('Comment', comments=comment1)
             cnx.commit()
             self.set_description('OnlineAccount')
             entities_graph = list(graph.parent_related(account))
@@ -118,7 +136,7 @@
             yield self.assertCountEqual, entities_graph, expected
             self.set_description('Anecdote')
             entities_graph = list(graph.parent_related(anecdote1))
-            expected = [
+            anecdote1_expected = expected = [
                 (anecdote1, ('event', 'object'), bio),
                 (bio, ('biography', 'object'), agent),
             ]
@@ -132,7 +150,14 @@
                 (bio, ('biography', 'object'), agent),
                 (anecdote2, ('narrated_by', 'subject'), agent),
             ]
-            self.assertCountEqual(entities_graph, expected)
+            yield self.assertCountEqual, entities_graph, expected
+            self.set_description('Comment')
+            entities_graph = list(graph.parent_related(comment2))
+            expected = [
+                (comment2, ('comments', 'subject'), comment1),
+                (comment1, ('comments', 'subject'), anecdote1),
+            ] + anecdote1_expected
+            yield self.assertCountEqual, entities_graph, expected
 
     def test_child_related_singleton(self):
         graph = CompositeGraph(self.schema)
@@ -151,25 +176,32 @@
             anecdote1 = cnx.create_entity('Anecdote', reverse_event=bio)
             anecdote2 = cnx.create_entity('Anecdote', reverse_event=bio, narrated_by=agent,
                                           relates=event)
+            comment1 = cnx.create_entity('Comment', comments=anecdote1)
+            comment2 = cnx.create_entity('Comment', comments=comment1)
             cnx.commit()
             self.set_description('Anecdote')
             entities_graph = list(graph.child_related(anecdote2))
-            expected = [
+            expected = anecdote2_expected = [
                 (anecdote2, ('relates', 'subject'), event),
             ]
             yield self.assertCountEqual, entities_graph, expected
+            self.set_description('Anecdote (1)')
+            entities_graph = list(graph.child_related(anecdote1))
+            expected = anecdote1_expected = [
+                (anecdote1, ('comments', 'object'), comment1),
+                (comment1, ('comments', 'object'), comment2),
+            ]
+            yield self.assertCountEqual, entities_graph, expected
             self.set_description('Agent')
             entities_graph = list(graph.child_related(agent))
             expected = [
                 (agent, ('account', 'subject'), account),
-
                 (agent, ('biography', 'subject'), bio),
                 (bio, ('event', 'subject'), event),
                 (bio, ('event', 'subject'), anecdote1),
                 (bio, ('event', 'subject'), anecdote2),
                 (agent, ('narrated_by', 'object'), anecdote2),
-                (anecdote2, ('relates', 'subject'), event),
-            ]
+            ] + anecdote2_expected + anecdote1_expected
             yield self.assertCountEqual, entities_graph, expected
 
 
@@ -184,7 +216,8 @@
                       ('narrated_by', 'object')],
             'Event': [],
             'Biography': [('event', 'subject')],
-            'Anecdote': [('relates', 'subject')],
+            'Anecdote': [('comments', 'object'), ('relates', 'subject')],
+            'Comment': [('comments', 'object')],
         }
         self.assertEqual(defn, expected)