def test_retrieve_networks(site):
    models.Attribute.objects.create(
        site=site, resource_name='Network', name='test'
    )

    net_8 = models.Network.objects.create(
        site=site, cidr=u'10.0.0.0/8', attributes={'test': 'foo'}
    )
    net_24 = models.Network.objects.create(
        site=site, cidr=u'10.0.0.0/24', attributes={'test': 'bar'}
    )
    ip = models.Network.objects.create(
        site=site, cidr=u'10.0.0.1/32'
    )

    # root=True
    assert list(site.networks.filter(parent_id=None)) == [net_8]


    # include_networks=True, include_ips=Fals
    assert list(site.networks.filter(is_ip=False)) == [net_8, net_24]

    # include_networks=False, include_ips=False
    assert list(site.networks.none()) == []

    # include_networks=True, include_ips=True
    assert list(site.networks.all()) == [net_8, net_24, ip]

    # include_networks=False, include_ips=True
    assert list(site.networks.filter(is_ip=True)) == [ip]

    # Filter by attribute
    assert list(site.networks.by_attribute(None, 'foo')) == []
    assert list(site.networks.by_attribute('test', 'foo')) == [net_8]

    # Get by address
    assert site.networks.get_by_address(u'10.0.0.0/8') == net_8

    #
    # .get_closest_parent()
    #
    # Get closest parent for non-existent 10.0.0.2/32 network
    assert site.networks.get_closest_parent(u'10.0.0.2/32') == net_24

    # Matching ip with shorter prefix_length should not match
    with pytest.raises(models.Network.DoesNotExist):
        site.networks.get_closest_parent(u'10.0.0.2/32', prefix_length=27)

    # Non-existent closest parent should error
    with pytest.raises(models.Network.DoesNotExist):
        site.networks.get_closest_parent(u'1.0.0.2/32')

    # Invalid prefix_length
    with pytest.raises(exc.ValidationError):
        site.networks.get_closest_parent(u'10.0.0.2/32', prefix_length='shoe')

    # Invalid CIDR
    with pytest.raises(exc.ValidationError):
        site.networks.get_closest_parent(u'1')
Test lookup of Network objects.
dataset/ETHPy150Open dropbox/nsot/tests/model_tests/test_networks.py/test_retrieve_networks