def test_build_out(self): FLAGS = Flags("PEOPLE", "HOBBIES") registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key="people") def method_people(*args): return dict(simon="123", george="234") @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key="hobbies") def method_hobbies(result): hobbies = { "123": ["mountain biking", "skiing"], "234": ["snail collecting", "roaring like a dinosaur"], } return_value = dict() for person, uid in result["people"].items(): return_value[person] = hobbies.get(uid) return return_value # Simple example # - No dependencies # - Single Flag # - No starts_with dict # - Default pass_dictionary value (False) result = registry.build_out(FLAGS.PEOPLE) self.assertEqual(result, dict(people=dict(simon="123", george="234"))) # Only send the leaf node of a dependency chain. Rely on registry to calculate dependencies. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result = registry.build_out(FLAGS.HOBBIES) self.assertEqual( result, dict( people=dict(simon="123", george="234"), hobbies=dict( simon=["mountain biking", "skiing"], george=["snail collecting", "roaring like a dinosaur"], ), ), ) # Explicitly send all required methods in the dependency chain. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result_1 = registry.build_out(FLAGS.PEOPLE | FLAGS.HOBBIES) self.assertEqual(result, result_1) FLAGS = Flags("PETS", "FARM_ANIMALS", "WILD_ANIMALS") registry = FlagRegistry() @registry.register( flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=("pets", "farm", "wild"), ) def some_method(): return "cat", "pig", "rhino" # Multiple Return Value Flag Subset result_2 = registry.build_out(FLAGS.PETS | FLAGS.FARM_ANIMALS) self.assertEqual(set(result_2.keys()), set(["pets", "farm"])) FLAGS = Flags("ONE") registry = FlagRegistry() @registry.register(flag=FLAGS.ONE) def method_one(): return dict(tanya="redAlert") # Let the registry build our results dictionary. (Lacks start_with) # Use the default value for pass_dictionary (False) result_3 = registry.build_out(FLAGS.ONE) self.assertEqual(result_3, dict(tanya="redAlert")) # Pass in our own results dictionary (start_with=somedict) # Use the default value for pass_dictionary (False) somedict = dict(somekey="asdf", anotherkey="defg") result_4 = registry.build_out(FLAGS.ONE, start_with=somedict) self.assertEqual(set(result_4.keys()), set(["tanya", "somekey", "anotherkey"])) # Pass in our own results dictionary (somedict=somedict) # Set pass_dictionary to True FLAGS = Flags("WINNER") registry = FlagRegistry() @registry.register(flag=FLAGS.WINNER) def method_winner(data): return dict(winner=data["name"]) somedict = dict(name="george") result_4 = registry.build_out( FLAGS.WINNER, start_with=somedict, pass_datastructure=True ) self.assertEqual(result_4, dict(winner="george", name="george")) # Let the registry build our results dictionary. (Lacks start_with) # Set pass_datastructure to True FLAGS = Flags("Cookies") registry = FlagRegistry() @registry.register(flag=FLAGS.Cookies) def method_cookies(data): return dict(cookies_remaining=len(data.keys())) result_4 = registry.build_out(FLAGS.ALL, pass_datastructure=True) self.assertEqual(result_4, dict(cookies_remaining=0)) # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags("ACK") registry = FlagRegistry() @registry.register(flag=FLAGS.ACK, key="salutation") def some_salutation(data): return data["hello"] starting_dict = dict(hello="goodbye") result = registry.build_out( FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict ) self.assertEqual(result, dict(hello="goodbye", salutation="goodbye")) # Dependent Method # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags("PEOPLE", "HOBBIES") registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key="people") def method_people1(*args): return dict(simon="123", george="234") @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key="hobbies") def method_hobbies1(result): hobbies = { "123": ["mountain biking", "skiing"], "234": ["snail collecting", "roaring like a dinosaur"], } return_value = dict() for person, uid in result["people"].items(): return_value[person] = hobbies.get(uid) return return_value starting_dict = dict(hello="goodbye") result = registry.build_out( FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict ) self.assertEqual( result, dict( hello="goodbye", people=dict(simon="123", george="234"), hobbies=dict( simon=["mountain biking", "skiing"], george=["snail collecting", "roaring like a dinosaur"], ), ), )
def test_build_out(self): FLAGS = Flags('PEOPLE', 'HOBBIES') registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key='people') def method_people(*args): return dict(simon='123', george='234') @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key='hobbies') def method_hobbies(result): hobbies = { '123': ['mountain biking', 'skiing'], '234': ['snail collecting', 'roaring like a dinosaur'] } return_value = dict() for person, uid in result['people'].items(): return_value[person] = hobbies.get(uid) return return_value # Simple example # - No dependencies # - Single Flag # - No starts_with dict # - Default pass_dictionary value (False) result = registry.build_out(FLAGS.PEOPLE) self.assertEqual(result, dict(people=dict(simon='123', george='234'))) # Only send the leaf node of a dependency chain. Rely on registry to calculate dependencies. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result = registry.build_out(FLAGS.HOBBIES) self.assertEqual( result, dict(people=dict(simon='123', george='234'), hobbies=dict( simon=['mountain biking', 'skiing'], george=['snail collecting', 'roaring like a dinosaur']))) # Explicitly send all required methods in the dependency chain. # Relies on the registry to detect that hobbies has a dependency and will automatically # pass the datastructure (result) to it as an arg. result_1 = registry.build_out(FLAGS.PEOPLE | FLAGS.HOBBIES) self.assertEqual(result, result_1) FLAGS = Flags('PETS', 'FARM_ANIMALS', 'WILD_ANIMALS') registry = FlagRegistry() @registry.register(flag=(FLAGS.PETS, FLAGS.FARM_ANIMALS, FLAGS.WILD_ANIMALS), key=('pets', 'farm', 'wild')) def some_method(): return 'cat', 'pig', 'rhino' # Multiple Return Value Flag Subset result_2 = registry.build_out(FLAGS.PETS | FLAGS.FARM_ANIMALS) self.assertEqual(set(result_2.keys()), set(['pets', 'farm'])) FLAGS = Flags('ONE') registry = FlagRegistry() @registry.register(flag=FLAGS.ONE) def method_one(): return dict(tanya='redAlert') # Let the registry build our results dictionary. (Lacks start_with) # Use the default value for pass_dictionary (False) result_3 = registry.build_out(FLAGS.ONE) self.assertEqual(result_3, dict(tanya='redAlert')) # Pass in our own results dictionary (start_with=somedict) # Use the default value for pass_dictionary (False) somedict = dict(somekey='asdf', anotherkey='defg') result_4 = registry.build_out(FLAGS.ONE, start_with=somedict) self.assertEqual(set(result_4.keys()), set(['tanya', 'somekey', 'anotherkey'])) # Pass in our own results dictionary (somedict=somedict) # Set pass_dictionary to True FLAGS = Flags('WINNER') registry = FlagRegistry() @registry.register(flag=FLAGS.WINNER) def method_winner(data): return dict(winner=data['name']) somedict = dict(name='george') result_4 = registry.build_out(FLAGS.WINNER, start_with=somedict, pass_datastructure=True) self.assertEqual(result_4, dict(winner='george', name='george')) # Let the registry build our results dictionary. (Lacks start_with) # Set pass_datastructure to True FLAGS = Flags('Cookies') registry = FlagRegistry() @registry.register(flag=FLAGS.Cookies) def method_cookies(data): return dict(cookies_remaining=len(data.keys())) result_4 = registry.build_out(FLAGS.ALL, pass_datastructure=True) self.assertEqual(result_4, dict(cookies_remaining=0)) # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags('ACK') registry = FlagRegistry() @registry.register(flag=FLAGS.ACK, key='salutation') def some_salutation(data): return data['hello'] starting_dict = dict(hello="goodbye") result = registry.build_out(FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict) self.assertEqual(result, dict(hello='goodbye', salutation='goodbye')) # Dependent Method # Set pass_datastructure=True # Set starts_with # Also send starting_dict in *args # Make sure the registry doesn't send two copies of data to the method. FLAGS = Flags('PEOPLE', 'HOBBIES') registry = FlagRegistry() @registry.register(flag=FLAGS.PEOPLE, key='people') def method_people1(*args): return dict(simon='123', george='234') @registry.register(flag=FLAGS.HOBBIES, depends_on=FLAGS.PEOPLE, key='hobbies') def method_hobbies1(result): hobbies = { '123': ['mountain biking', 'skiing'], '234': ['snail collecting', 'roaring like a dinosaur'] } return_value = dict() for person, uid in result['people'].items(): return_value[person] = hobbies.get(uid) return return_value starting_dict = dict(hello="goodbye") result = registry.build_out(FLAGS.ALL, starting_dict, pass_datastructure=True, start_with=starting_dict) self.assertEqual( result, dict(hello='goodbye', people=dict(simon='123', george='234'), hobbies=dict( simon=['mountain biking', 'skiing'], george=['snail collecting', 'roaring like a dinosaur'])))