Test fixtures¶
ldclient.integrations.test_data module¶
The entry point for this feature is ldclient.integrations.test_data.TestData.
- class ldclient.integrations.test_data.FlagBuilder(key: str)[source]¶
Bases:
objectA builder for feature flag configurations to be used with
ldclient.integrations.test_data.TestData.- See:
- See:
- boolean_flag() FlagBuilder[source]¶
A shortcut for setting the flag to use the standard boolean configuration.
This is the default for all new flags created with
ldclient.integrations.test_data.TestData.flag().The flag will have two variations,
TrueandFalse(in that order); it will returnFalsewhenever targeting is off, andTruewhen targeting is on if no other settings specify otherwise.- Returns:
the flag builder
- clear_rules() FlagBuilder[source]¶
Removes any existing rules from the flag. This undoes the effect of methods like
ldclient.integrations.test_data.FlagBuilder.if_match().- Returns:
the same flag builder
- clear_targets() FlagBuilder[source]¶
Removes any existing targets from the flag. This undoes the effect of methods like
ldclient.integrations.test_data.FlagBuilder.variation_for_user().- Returns:
the same flag builder
- fallthrough_variation(variation: bool | int) FlagBuilder[source]¶
Specifies the fallthrough variation. The fallthrough is the value that is returned if targeting is on and the user was not matched by a more specific target or rule.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
- Parameters:
variation (bool|int) –
TrueorFalseor the desired fallthrough variation index:0for the first,1for the second, etc.- Returns:
the flag builder
- if_match(attribute: str, *values) FlagRuleBuilder[source]¶
Starts defining a flag rule, using the “is one of” operator.
This is a shortcut for calling
ldclient.integrations.test_data.FlagBuilder.if_match_context()with “user” as the context kind.Example: create a rule that returns
Trueif the name is “Patsy” or “Edina”td.flag("flag") \ .if_match('name', 'Patsy', 'Edina') \ .then_return(True)
- Parameters:
attribute – the user attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- if_match_context(context_kind: str, attribute: str, *values) FlagRuleBuilder[source]¶
Starts defining a flag rule, using the “is one of” operator. This matching expression only applies to contexts of a specific kind.
Example: create a rule that returns
Trueif the name attribute for the company” context is “Ella” or “Monsoon”:td.flag("flag") \ .if_match_context('company', 'name', 'Ella', 'Monsoon') \ .then_return(True)
- Parameters:
context_kind – the context kind
attribute – the context attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- if_not_match(attribute: str, *values) FlagRuleBuilder[source]¶
Starts defining a flag rule, using the “is not one of” operator.
This is a shortcut for calling
ldclient.integrations.test_data.FlagBuilder.if_not_match_context()with “user” as the context kind.Example: create a rule that returns
Trueif the name is neither “Saffron” nor “Bubble”td.flag("flag") \ .if_not_match('name', 'Saffron', 'Bubble') \ .then_return(True)
- Parameters:
attribute – the user attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- if_not_match_context(context_kind: str, attribute: str, *values) FlagRuleBuilder[source]¶
Starts defining a flag rule, using the “is not one of” operator. This matching expression only applies to contexts of a specific kind.
Example: create a rule that returns
Trueif the name attribute for the “company” context is neither “Pendant” nor “Sterling Cooper”:td.flag("flag") \ .if_not_match('company', 'name', 'Pendant', 'Sterling Cooper') \ .then_return(True)
- Parameters:
context_kind – the context kind
attribute – the context attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- off_variation(variation: bool | int) FlagBuilder[source]¶
Specifies the fallthrough variation. This is the variation that is returned whenever targeting is off.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
- Parameters:
variation (bool|int) –
TrueorFalseor the desired off variation index:0for the first,1for the second, etc.- Returns:
the flag builder
- on(on: bool) FlagBuilder[source]¶
Sets targeting to be on or off for this flag.
The effect of this depends on the rest of the flag configuration, just as it does on the real LaunchDarkly dashboard. In the default configuration that you get from calling
ldclient.integrations.test_data.TestData.flag()with a new flag key, the flag will returnFalsewhenever targeting is off, andTruewhen targeting is on.- Parameters:
on –
Trueif targeting should be on- Returns:
the flag builder
- value_for_all(value: Any) FlagBuilder[source]¶
Sets the flag to always return the specified variation value for all users.
The value may be of any JSON type. This method changes the flag to have only a single variation, which is this value, and to return the same variation regardless of whether targeting is on or off. Any existing targets or rules are removed.
:param value the desired value to be returned for all users :return the flag builder
- variation_for_all(variation: bool | int) FlagBuilder[source]¶
Sets the flag to always return the specified variation for all contexts.
The variation is specified, targeting is switched on, and any existing targets or rules are removed. The fallthrough variation is set to the specified value. The off variation is left unchanged.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
- Parameters:
variation (bool|int) –
TrueorFalseor the desired variation index to return:0for the first,1for the second, etc.- Returns:
the flag builder
- variation_for_key(context_kind: str, context_key: str, variation: bool | int) FlagBuilder[source]¶
Sets the flag to return the specified variation for a specific context, identified by context kind and key, when targeting is on.
This has no effect when targeting is turned off for the flag.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
- Parameters:
context_kind – the context kind
context_key – the context key
variation (bool|int) –
TrueorFalseor the desired variation index to return:0for the first,1for the second, etc.
- Returns:
the flag builder
- variation_for_user(user_key: str, variation: bool | int) FlagBuilder[source]¶
Sets the flag to return the specified variation for a specific user key when targeting is on.
This has no effect when targeting is turned off for the flag.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
- Parameters:
user_key – a user key
variation (bool|int) –
TrueorFalseor the desired variation index to return:0for the first,1for the second, etc.
- Returns:
the flag builder
- variations(*variations) FlagBuilder[source]¶
Changes the allowable variation values for the flag.
The value may be of any valid JSON type. For instance, a boolean flag normally has
True, False; a string-valued flag might have'red', 'green'; etc.Example: A single variation
td.flag('new-flag').variations(True)
Example: Multiple variations
td.flag('new-flag').variations('red', 'green', 'blue')
- Parameters:
variations – the the desired variations
- Returns:
the flag builder
- class ldclient.integrations.test_data.FlagRuleBuilder(flag_builder: FlagBuilder)[source]¶
Bases:
objectA builder for feature flag rules to be used with
ldclient.integrations.test_data.FlagBuilder.In the LaunchDarkly model, a flag can have any number of rules, and a rule can have any number of clauses. A clause is an individual test such as “name is ‘X’”. A rule matches a user if all of the rule’s clauses match the user.
To start defining a rule, use one of the flag builder’s matching methods such as
ldclient.integrations.test_data.FlagBuilder.if_match(). This defines the first clause for the rule. Optionally, you may add more clauses with the rule builder’s methods such asldclient.integrations.test_data.FlagRuleBuilder.and_match()orldclient.integrations.test_data.FlagRuleBuilder.and_not_match(). Finally, callldclient.integrations.test_data.FlagRuleBuilder.then_return()to finish defining the rule.- __init__(flag_builder: FlagBuilder)[source]¶
- and_match(attribute: str, *values) FlagRuleBuilder[source]¶
Adds another clause, using the “is one of” operator.
This is a shortcut for calling
ldclient.integrations.test_data.FlagRuleBuilder.and_match_context()with “user” as the context kind.Example: create a rule that returns
Trueif the name is “Patsy” and the country is “gb”td.flag('flag') \ .if_match('name', 'Patsy') \ .and_match('country', 'gb') \ .then_return(True)
- Parameters:
attribute – the user attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- and_match_context(context_kind: str, attribute: str, *values) FlagRuleBuilder[source]¶
Adds another clause, using the “is one of” operator. This matching expression only applies to contexts of a specific kind.
Example: create a rule that returns
Trueif the name attribute for the “company” context is “Ella”, and the country attribute for the “company” context is “gb”:td.flag('flag') \ .if_match_context('company', 'name', 'Ella') \ .and_match_context('company', 'country', 'gb') \ .then_return(True)
- Parameters:
context_kind – the context kind
attribute – the context attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- and_not_match(attribute: str, *values) FlagRuleBuilder[source]¶
Adds another clause, using the “is not one of” operator.
This is a shortcut for calling
ldclient.integrations.test_data.FlagRuleBuilder.and_not_match_context()with “user” as the context kind.Example: create a rule that returns
Trueif the name is “Patsy” and the country is not “gb”td.flag('flag') \ .if_match('name', 'Patsy') \ .and_not_match('country', 'gb') \ .then_return(True)
- Parameters:
attribute – the user attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- and_not_match_context(context_kind: str, attribute: str, *values) FlagRuleBuilder[source]¶
Adds another clause, using the “is not one of” operator. This matching expression only applies to contexts of a specific kind.
Example: create a rule that returns
Trueif the name attribute for the “company” context is “Ella”, and the country attribute for the “company” context is not “gb”:td.flag('flag') \ .if_match_context('company', 'name', 'Ella') \ .and_not_match_context('company', 'country', 'gb') \ .then_return(True)
- Parameters:
context_kind – the context kind
attribute – the context attribute to match against
values – values to compare to
- Returns:
the flag rule builder
- then_return(variation: bool | int) FlagBuilder[source]¶
Finishes defining the rule, specifying the result as either a boolean or a variation index.
If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.
- Parameters:
variation (bool|int) –
TrueorFalseor the desired variation index:0for the first,1for the second, etc.- Returns:
the flag builder with this rule added
- class ldclient.integrations.test_data.TestData[source]¶
Bases:
objectA mechanism for providing dynamically updatable feature flag state in a simplified form to an SDK client in test scenarios.
Unlike
Files, this mechanism does not use any external resources. It provides only the data that the application has put into it using theupdatemethod.td = TestData.data_source() td.update(td.flag('flag-key-1').variation_for_all(True)) client = LDClient(config=Config('SDK_KEY', update_processor_class = td)) # flags can be updated at any time: td.update(td.flag('flag-key-1'). \ variation_for_user('some-user-key', True). \ fallthrough_variation(False))
The above example uses a simple boolean flag, but more complex configurations are possible using the methods of the
FlagBuilderthat is returned byflag.FlagBuildersupports many of the ways a flag can be configured on the LaunchDarkly dashboard, but does not currently support 1. rule operators other than “in” and “not in”, or 2. percentage rollouts.If the same
TestDatainstance is used to configure multipleLDClientinstances, any changes made to the data will propagate to all of theLDClientinstances.- static data_source() TestData[source]¶
Creates a new instance of the test data source.
- Returns:
a new configurable test data source
- flag(key: str) FlagBuilder[source]¶
Creates or copies a
FlagBuilderfor building a test flag configuration.If this flag key has already been defined in this
TestDatainstance, then the builder starts with the same configuration that was last provided for this flag.Otherwise, it starts with a new default configuration in which the flag has
TrueandFalsevariations, isTruefor all users when targeting is turned on andFalseotherwise, and currently has targeting turned on. You can change any of those properties, and provide more complex behavior, using theFlagBuildermethods.Once you have set the desired configuration, pass the builder to
update.- Parameters:
key (str) – the flag key
- Returns:
the flag configuration builder object
- update(flag_builder: FlagBuilder) TestData[source]¶
Updates the test data with the specified flag configuration.
This has the same effect as if a flag were added or modified on the LaunchDarkly dashboard. It immediately propagates the flag change to any
LDClientinstance(s) that you have already configured to use thisTestData. If noLDClienthas been started yet, it simply adds this flag to the test data which will be provided to anyLDClientthat you subsequently configure.Any subsequent changes to this
FlagBuilderinstance do not affect the test data, unless you callupdateagain.- Parameters:
flag_builder – a flag configuration builder
- Returns:
self (the TestData object)