def test_set_query(site):
    site2 = models.Site.objects.create(name='Site 2')

    # Attributes
    models.Attribute.objects.create(
        name='owner', site=site, resource_name='Device'
    )
    models.Attribute.objects.create(
        name='owner', site=site2, resource_name='Device'
    )
    models.Attribute.objects.create(
        name='role', site=site, resource_name='Device'
    )

    # Devices
    device1 = models.Device.objects.create(
        hostname='foo-bar1', attributes={'owner': 'jathan', 'role': 'br'},
        site=site
    )
    device2 = models.Device.objects.create(
        hostname='foo-bar2', attributes={'owner': 'gary', 'role': 'dr'},
        site=site
    )

    # Since we have two attributes named 'owner' in 2 different sites, this
    # should raise an error. (Fix #66)
    with pytest.raises(MultipleObjectsReturned):
        models.Device.objects.set_query('owner=jathan')

    # Now include the site_id
    devices = models.Device.objects.set_query('owner=jathan', site_id=site.id)
    assert list(devices) == [device1]

    # Bad set queries raises an error.
    bad_queries = [None, {}, 3.14, object()]
    for bad_q in bad_queries:
        with pytest.raises(exc.ValidationError):
            models.Network.objects.set_query(bad_q)

    # Make sure that a bogus set query raises a ValidationError
    with pytest.raises(exc.ValidationError):
        models.Device.objects.set_query('role=[ab, bb, cb]')

    # Empty set query is empty result
    empty = models.Network.objects.set_query('')
    assert list(empty) == []

    # Test regex set query matches a union set query.
    union = models.Device.objects.set_query('role=br +role=dr').order_by('id')
    regex = models.Device.objects.set_query('role_regex=[bd]r').order_by('id')
    assert list(union) == list(regex)
Get the next batch.
dataset/ETHPy150Open dropbox/nsot/tests/model_tests/test_attributes.py/test_set_query--crossover with--dataset/ETHPy150Open AppScale/appscale/AppServer/google/appengine/datastore/datastore_query.py/Batcher.next_batch