from django.test import TestCase

from .models import Group, Group2Type, GroupParent, GroupType, Node, Node2Group

from .model_util import get_child_nodes

# Create your tests here.
class NodeGroupingTests(TestCase):
    # Test functions must start with `test_`
    def test_node_relationships_work(self):
        node = Node(friendly_name='Node A')
        node2 = Node(friendly_name='Node B')
        node.save()
        node2.save()

        group_nm = Group(name="New Mexico")
        group_nm.save()
        group_josh = Group(name="Josh")
        group_josh.save()
        group_sill = Group(name="Windowsill")
        group_sill.save()
        group_not_in_hierarchy = Group(name="Needs Charging")
        group_not_in_hierarchy.save()

        association = Node2Group(node=node, group=group_sill)
        association.save()
        association = Node2Group(node=node, group=group_not_in_hierarchy)
        association.save()
        association = Node2Group(node=node, group=group_josh)
        association.save()

        type_site = GroupType(name="Site")
        type_site.save()
        type_windrow = GroupType(name="Windrow")
        type_windrow.save()
        type_temp_site= GroupType(name="Temperature Site")
        type_temp_site.save()
        type_arbitrary = GroupType(name="Arbitrary Group")
        type_arbitrary.save()

        association = Group2Type(group=group_nm, type=type_site)
        association.save()
        association = Group2Type(group=group_josh, type=type_windrow)
        association.save()
        association = Group2Type(group=group_sill, type=type_temp_site)
        association.save()
        association = Group2Type(group=group_not_in_hierarchy, type=type_arbitrary)
        association.save()

        association = GroupParent(child=group_josh, parent=group_nm)
        association.save()
        association = GroupParent(child=group_sill, parent=group_josh)
        association.save()


        children = get_child_nodes(group_nm)

        # Both nodes are children of "NM"
        self.assertIs(len(children), 2)

        # But only one node is a child of "arbitrary"
        children = get_child_nodes(group_not_in_hierarchy)
        self.assertIs(len(children), 1)
