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)[source]¶ Bases:
object
A builder for feature flag configurations to be used with
ldclient.integrations.test_data.TestData
.- See
- See
-
boolean_flag
()[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,
True
andFalse
(in that order); it will returnFalse
whenever targeting is off, andTrue
when targeting is on if no other settings specify otherwise.- Return type
- Returns
the flag builder
-
clear_rules
()[source]¶ Removes any existing rules from the flag. This undoes the effect of methods like
ldclient.integrations.test_data.FlagBuilder.if_match()
- Return type
- Returns
the same flag builder
-
clear_targets
()[source]¶ Removes any existing targets from the flag. This undoes the effect of methods like
ldclient.integrations.test_data.FlagBuilder.variation_for_user()
- Return type
- Returns
the same flag builder
-
fallthrough_variation
(variation)[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) –
True
orFalse
or the desired fallthrough variation index:0
for the first,1
for the second, etc.- Return type
- Returns
the flag builder
-
if_match
(attribute, *values)[source]¶ Starts defining a flag rule, using the “is one of” operator.
Example: create a rule that returns
True
if the name is “Patsy” or “Edina”td.flag("flag") \ .if_match('name', 'Patsy', 'Edina') \ .then_return(True)
- Parameters
attribute (
str
) – the user attribute to match againstvalues – values to compare to
- Return type
- Returns
the flag rule builder
-
if_not_match
(attribute, *values)[source]¶ Starts defining a flag rule, using the “is not one of” operator.
Example: create a rule that returns
True
if the name is neither “Saffron” nor “Bubble”td.flag("flag") \ .if_not_match('name', 'Saffron', 'Bubble') \ .then_return(True)
- Parameters
attribute (
str
) – the user attribute to match againstvalues – values to compare to
- Return type
- Returns
the flag rule builder
-
off_variation
(variation)[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) –
True
orFalse
or the desired off variation index:0
for the first,1
for the second, etc.- Return type
- Returns
the flag builder
-
on
(on)[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 returnFalse
whenever targeting is off, andTrue
when targeting is on.- Parameters
on (
bool
) –True
if targeting should be on- Return type
- Returns
the flag builder
-
value_for_all_users
(value)[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
- Return type
-
variation_for_all_users
(variation)[source]¶ Sets the flag to always return the specified variation for all users.
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) –
True
orFalse
or the desired variation index to return:0
for the first,1
for the second, etc.- Return type
- Returns
the flag builder
-
variation_for_user
(user_key, variation)[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 (
str
) – a user keyvariation (bool|int) –
True
orFalse
or the desired variation index to return:0
for the first,1
for the second, etc.
- Return type
- Returns
the flag builder
-
variations
(*variations)[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
- Return type
- Returns
the flag builder
-
class
ldclient.integrations.test_data.
FlagRuleBuilder
(flag_builder)[source]¶ Bases:
object
A 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.-
and_match
(attribute, *values)[source]¶ Adds another clause, using the “is one of” operator.
Example: create a rule that returns
True
if 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 (
str
) – the user attribute to match againstvalues – values to compare to
- Return type
- Returns
the flag rule builder
-
and_not_match
(attribute, *values)[source]¶ Adds another clause, using the “is not one of” operator.
Example: create a rule that returns
True
if 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 (
str
) – the user attribute to match againstvalues – values to compare to
- Return type
- Returns
the flag rule builder
-
then_return
(variation)[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) –
True
orFalse
or the desired variation index:0
for the first,1
for the second, etc.- Return type
- Returns
the flag builder with this rule added
-
-
class
ldclient.integrations.test_data.
TestData
[source]¶ Bases:
object
A 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 theupdate
method.td = TestData.data_source() td.update(td.flag('flag-key-1').variation_for_all_users(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
FlagBuilder
that is returned byflag
.FlagBuilder
supports 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 TestData instance is used to configure multiple LDClient instances, any changes made to the data will propagate to all of the LDClient instances.
-
static
data_source
()[source]¶ Creates a new instance of the test data source.
- Return type
- Returns
a new configurable test data source
-
flag
(key)[source]¶ Creates or copies a
FlagBuilder
for building a test flag configuration.If this flag key has already been defined in this
TestData
instance, 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
True
andFalse
variations, isTrue
for all users when targeting is turned on andFalse
otherwise, and currently has targeting turned on. You can change any of those properties, and provide more complex behavior, using theFlagBuilder
methods.Once you have set the desired configuration, pass the builder to
update
.- Parameters
key (str) – the flag key
- Return type
- Returns
the flag configuration builder object
-
update
(flag_builder)[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
LDClient
instance(s) that you have already configured to use thisTestData
. If noLDClient
has been started yet, it simply adds this flag to the test data which will be provided to anyLDClient
that you subsequently configure.Any subsequent changes to this
FlagBuilder
instance do not affect the test data, unless you callupdate
again.- Parameters
flag_builder (
FlagBuilder
) – a flag configuration builder- Return type
- Returns
self (the TestData object)
-
static