blob: 7d7e4b207c961cdd70f3fa35b46f88a46cf2797d [file] [log] [blame]
Zhuoyao Zhang3ca7cef2024-10-31 22:07:31 +00001# Copyright 2024, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for edit monitor utils."""
16import os
17import unittest
18from unittest import mock
19
20from edit_monitor import utils
21
22TEST_USER = 'test_user'
23TEST_FEATURE = 'test_feature'
24ENABLE_TEST_FEATURE_FLAG = 'ENABLE_TEST_FEATURE'
25ROLLOUT_TEST_FEATURE_FLAG = 'ROLLOUT_TEST_FEATURE'
26
27
28class EnableFeatureTest(unittest.TestCase):
29
30 def test_feature_enabled_without_flag(self):
31 self.assertTrue(utils.is_feature_enabled(TEST_FEATURE, TEST_USER))
32
33 @mock.patch.dict(os.environ, {ENABLE_TEST_FEATURE_FLAG: 'false'}, clear=True)
34 def test_feature_disabled_with_flag(self):
35 self.assertFalse(
36 utils.is_feature_enabled(
37 TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG
38 )
39 )
40
41 @mock.patch.dict(os.environ, {ENABLE_TEST_FEATURE_FLAG: 'true'}, clear=True)
42 def test_feature_enabled_with_flag(self):
43 self.assertTrue(
44 utils.is_feature_enabled(
45 TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG
46 )
47 )
48
49 @mock.patch.dict(
50 os.environ, {ROLLOUT_TEST_FEATURE_FLAG: 'invalid'}, clear=True
51 )
52 def test_feature_disabled_with_invalid_rollout_percentage(self):
53 self.assertFalse(
54 utils.is_feature_enabled(
55 TEST_FEATURE,
56 TEST_USER,
57 ENABLE_TEST_FEATURE_FLAG,
58 ROLLOUT_TEST_FEATURE_FLAG,
59 )
60 )
61
62 @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '101'}, clear=True)
63 def test_feature_disabled_with_rollout_percentage_too_high(self):
64 self.assertFalse(
65 utils.is_feature_enabled(
66 TEST_FEATURE,
67 TEST_USER,
68 ENABLE_TEST_FEATURE_FLAG,
69 ROLLOUT_TEST_FEATURE_FLAG,
70 )
71 )
72
73 @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '-1'}, clear=True)
74 def test_feature_disabled_with_rollout_percentage_too_low(self):
75 self.assertFalse(
76 utils.is_feature_enabled(
77 TEST_FEATURE,
78 TEST_USER,
79 ENABLE_TEST_FEATURE_FLAG,
80 ROLLOUT_TEST_FEATURE_FLAG,
81 )
82 )
83
84 @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '90'}, clear=True)
85 def test_feature_enabled_with_rollout_percentage(self):
86 self.assertTrue(
87 utils.is_feature_enabled(
88 TEST_FEATURE,
89 TEST_USER,
90 ENABLE_TEST_FEATURE_FLAG,
91 ROLLOUT_TEST_FEATURE_FLAG,
92 )
93 )
94
95 @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '10'}, clear=True)
96 def test_feature_disabled_with_rollout_percentage(self):
97 self.assertFalse(
98 utils.is_feature_enabled(
99 TEST_FEATURE,
100 TEST_USER,
101 ENABLE_TEST_FEATURE_FLAG,
102 ROLLOUT_TEST_FEATURE_FLAG,
103 )
104 )
105
106
107if __name__ == '__main__':
108 unittest.main()