#NEWLINE# def test_set_query( site ) : #NEWLINE# #INDENT# 'Test backend functionality of set queries.' #NEWLINE# site2 = models . Site . objects . create ( name = 'Site 2' ) #NEWLINE# models . Attribute . objects . create ( name = 'owner' , site = site , resource_name = 'Device' ) #NEWLINE# models . Attribute . objects . create ( name = 'owner' , site = site2 , resource_name = 'Device' ) #NEWLINE# models . Attribute . objects . create ( name = 'role' , site = site , resource_name = 'Device' ) #NEWLINE# device1 = models . Device . objects . create ( hostname = 'foo-bar1' , attributes = { #NEWLINE# #INDENT# 'owner' : 'jathan' , #NEWLINE# 'role' : 'br' , #NEWLINE# #UNINDENT# } , site = site ) #NEWLINE# device2 = models . Device . objects . create ( hostname = 'foo-bar2' , attributes = { #NEWLINE# #INDENT# 'owner' : 'gary' , #NEWLINE# 'role' : 'dr' , #NEWLINE# #UNINDENT# } , site = site ) #NEWLINE# with pytest . raises ( MultipleObjectsReturned ) : #NEWLINE# #INDENT# models . Device . objects . set_query ( 'owner=jathan' ) #NEWLINE# #UNINDENT# devices = models . Device . objects . set_query ( 'owner=jathan' , site_id = site . id ) #NEWLINE# assert ( list ( devices ) == [ device1 ] ) #NEWLINE# bad_queries = [ None , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , 3.14 , object ( ) ] #NEWLINE# for bad_q in bad_queries : #NEWLINE# #INDENT# with pytest . raises ( exc . ValidationError ) : #NEWLINE# #INDENT# models . Network . objects . set_query ( bad_q ) #NEWLINE# #UNINDENT# #UNINDENT# with pytest . raises ( exc . ValidationError ) : #NEWLINE# #INDENT# models . Device . objects . set_query ( 'role=[ab, bb, cb]' ) #NEWLINE# #UNINDENT# empty = models . Network . objects . set_query ( '' ) #NEWLINE# assert ( list ( empty ) == [ ] ) #NEWLINE# union = models . Device . objects . set_query ( 'role=br +role=dr' ) . order_by ( 'id' ) #NEWLINE# regex = models . Device . objects . set_query ( 'role_regex=[bd]r' ) . order_by ( 'id' ) #NEWLINE# assert ( list ( union ) == list ( regex ) )