test_feature.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. from dataclasses import dataclass
  2. import jsonschema
  3. import orjson
  4. import pytest
  5. import yaml
  6. from flagpole import ContextBuilder, EvaluationContext, Feature, InvalidFeatureFlagConfiguration
  7. from flagpole.conditions import ConditionOperatorKind
  8. @dataclass
  9. class SimpleTestContextData:
  10. pass
  11. class TestParseFeatureConfig:
  12. def get_is_true_context_builder(self, is_true_value: bool):
  13. return ContextBuilder().add_context_transformer(lambda _data: dict(is_true=is_true_value))
  14. def test_feature_with_empty_segments(self):
  15. feature = Feature.from_feature_config_json(
  16. "foobar",
  17. """
  18. {
  19. "created_at": "2023-10-12T00:00:00.000Z",
  20. "owner": "test-owner",
  21. "segments": []
  22. }
  23. """,
  24. )
  25. assert feature.name == "foobar"
  26. assert feature.created_at == "2023-10-12T00:00:00.000Z"
  27. assert feature.owner == "test-owner"
  28. assert feature.segments == []
  29. assert not feature.match(EvaluationContext(dict()))
  30. def test_feature_with_rollout_zero(self):
  31. feature = Feature.from_feature_config_json(
  32. "foobar",
  33. """
  34. {
  35. "created_at": "2023-10-12T00:00:00.000Z",
  36. "owner": "test-owner",
  37. "segments": [
  38. {
  39. "name": "exclude",
  40. "rollout": 0,
  41. "conditions": [
  42. {
  43. "property": "user_email",
  44. "operator": "equals",
  45. "value": "nope@example.com"
  46. }
  47. ]
  48. },
  49. {
  50. "name": "friends",
  51. "rollout": 100,
  52. "conditions": [
  53. {
  54. "property": "organization_slug",
  55. "operator": "in",
  56. "value": ["acme", "sentry"]
  57. }
  58. ]
  59. }
  60. ]
  61. }
  62. """,
  63. )
  64. exclude_user = {"user_email": "nope@example.com", "organization_slug": "acme"}
  65. assert not feature.match(EvaluationContext(exclude_user))
  66. match_user = {"user_email": "yes@example.com", "organization_slug": "acme"}
  67. assert feature.match(EvaluationContext(match_user))
  68. def test_all_conditions_in_segment(self):
  69. feature = Feature.from_feature_config_json(
  70. "foobar",
  71. """
  72. {
  73. "created_at": "2023-10-12T00:00:00.000Z",
  74. "owner": "test-owner",
  75. "segments": [
  76. {
  77. "name": "multiple conditions",
  78. "rollout": 100,
  79. "conditions": [
  80. {
  81. "property": "user_email",
  82. "operator": "equals",
  83. "value": "yes@example.com"
  84. },
  85. {
  86. "property": "organization_slug",
  87. "operator": "in",
  88. "value": ["acme", "sentry"]
  89. }
  90. ]
  91. }
  92. ]
  93. }
  94. """,
  95. )
  96. exclude_user = {"user_email": "yes@example.com"}
  97. assert not feature.match(EvaluationContext(exclude_user))
  98. match_user = {"user_email": "yes@example.com", "organization_slug": "acme"}
  99. assert feature.match(EvaluationContext(match_user))
  100. def test_valid_with_all_nesting(self):
  101. feature = Feature.from_feature_config_json(
  102. "foobar",
  103. """
  104. {
  105. "created_at": "2023-10-12T00:00:00.000Z",
  106. "owner": "test-owner",
  107. "segments": [{
  108. "name": "segment1",
  109. "rollout": 100,
  110. "conditions": [{
  111. "property": "test_property",
  112. "operator": "in",
  113. "value": ["foobar"]
  114. }]
  115. }]
  116. }
  117. """,
  118. )
  119. assert feature.name == "foobar"
  120. assert len(feature.segments) == 1
  121. assert feature.segments[0].name == "segment1"
  122. assert feature.segments[0].rollout == 100
  123. assert len(feature.segments[0].conditions) == 1
  124. condition = feature.segments[0].conditions[0]
  125. assert condition.property == "test_property"
  126. assert condition.operator
  127. assert condition.operator == ConditionOperatorKind.IN
  128. assert condition.value == ["foobar"]
  129. assert feature.match(EvaluationContext(dict(test_property="foobar")))
  130. assert not feature.match(EvaluationContext(dict(test_property="barfoo")))
  131. def test_invalid_json(self):
  132. with pytest.raises(InvalidFeatureFlagConfiguration):
  133. Feature.from_feature_config_json("foobar", "{")
  134. def test_validate_invalid_schema(self):
  135. config = """
  136. {
  137. "owner": "sentry",
  138. "segments": [
  139. {
  140. "name": "",
  141. "rollout": 1,
  142. "conditions": []
  143. }
  144. ]
  145. }
  146. """
  147. feature = Feature.from_feature_config_json("trash", config)
  148. with pytest.raises(jsonschema.ValidationError) as err:
  149. feature.validate()
  150. assert "is too short" in str(err)
  151. config = """
  152. {
  153. "owner": "sentry",
  154. "segments": [
  155. {
  156. "name": "allowed orgs",
  157. "rollout": 1,
  158. "conditions": [
  159. {
  160. "property": "organization_slug",
  161. "operator": "contains",
  162. "value": ["derp"]
  163. }
  164. ]
  165. }
  166. ]
  167. }
  168. """
  169. feature = Feature.from_feature_config_json("trash", config)
  170. with pytest.raises(jsonschema.ValidationError) as err:
  171. feature.validate()
  172. assert "'contains'} is not valid" in str(err)
  173. def test_validate_valid(self):
  174. config = """
  175. {
  176. "owner": "sentry",
  177. "segments": [
  178. {
  179. "name": "ga",
  180. "rollout": 100,
  181. "conditions": []
  182. }
  183. ]
  184. }
  185. """
  186. feature = Feature.from_feature_config_json("redpaint", config)
  187. assert feature.validate()
  188. def test_empty_string_name(self):
  189. with pytest.raises(InvalidFeatureFlagConfiguration) as exception:
  190. Feature.from_feature_config_json("", '{"segments":[]}')
  191. assert "Feature name is required" in str(exception)
  192. def test_missing_segments(self):
  193. with pytest.raises(InvalidFeatureFlagConfiguration) as exception:
  194. Feature.from_feature_config_json("foo", "{}")
  195. assert "Feature has no segments defined" in str(exception)
  196. def test_invalid_operator_condition(self):
  197. config = """
  198. {
  199. "owner": "sentry",
  200. "segments": [
  201. {
  202. "name": "derp",
  203. "conditions": [
  204. {"property": "user_email", "operator": "trash", "value": 1}
  205. ]
  206. }
  207. ]
  208. }
  209. """
  210. with pytest.raises(InvalidFeatureFlagConfiguration) as exception:
  211. Feature.from_feature_config_json("foo", config)
  212. assert "Provided config_dict is not a valid feature" in str(exception)
  213. def test_enabled_feature(self):
  214. feature = Feature.from_feature_config_json(
  215. "foo",
  216. """
  217. {
  218. "owner": "test-user",
  219. "created_at": "2023-10-12T00:00:00.000Z",
  220. "segments": [{
  221. "name": "always_pass_segment",
  222. "rollout": 100,
  223. "conditions": [{
  224. "name": "Always true",
  225. "property": "is_true",
  226. "operator": "equals",
  227. "value": true
  228. }]
  229. }]
  230. }
  231. """,
  232. )
  233. context_builder = self.get_is_true_context_builder(is_true_value=True)
  234. assert feature.match(context_builder.build(SimpleTestContextData()))
  235. def test_disabled_feature(self):
  236. feature = Feature.from_feature_config_json(
  237. "foo",
  238. """
  239. {
  240. "owner": "test-user",
  241. "enabled": false,
  242. "created_at": "2023-12-12T00:00:00.000Z",
  243. "segments": [{
  244. "name": "always_pass_segment",
  245. "rollout": 100,
  246. "conditions": [{
  247. "name": "Always true",
  248. "property": "is_true",
  249. "operator": "equals",
  250. "value": true
  251. }]
  252. }]
  253. }
  254. """,
  255. )
  256. context_builder = self.get_is_true_context_builder(is_true_value=True)
  257. assert not feature.match(context_builder.build(SimpleTestContextData()))
  258. def test_dump_yaml(self):
  259. feature = Feature.from_feature_config_json(
  260. "foo",
  261. """
  262. {
  263. "owner": "test-user",
  264. "created_at": "2023-12-12T00:00:00.000Z",
  265. "segments": [{
  266. "name": "always_pass_segment",
  267. "rollout": 100,
  268. "conditions": [{
  269. "name": "Always true",
  270. "property": "is_true",
  271. "operator": "equals",
  272. "value": true
  273. }]
  274. }]
  275. }
  276. """,
  277. )
  278. parsed_json = orjson.loads(feature.to_json_str())
  279. parsed_yaml = dict(yaml.safe_load(feature.to_yaml_str()))
  280. assert "foo" in parsed_yaml
  281. assert parsed_yaml == parsed_json
  282. features_from_yaml = Feature.from_bulk_yaml(feature.to_yaml_str())
  283. assert features_from_yaml == [feature]