Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 1 | # 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.""" |
| 16 | import os |
| 17 | import unittest |
| 18 | from unittest import mock |
| 19 | |
| 20 | from edit_monitor import utils |
| 21 | |
| 22 | TEST_USER = 'test_user' |
| 23 | TEST_FEATURE = 'test_feature' |
| 24 | ENABLE_TEST_FEATURE_FLAG = 'ENABLE_TEST_FEATURE' |
| 25 | ROLLOUT_TEST_FEATURE_FLAG = 'ROLLOUT_TEST_FEATURE' |
| 26 | |
| 27 | |
| 28 | class 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 | |
Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 49 | def test_feature_enabled_with_rollout_percentage(self): |
| 50 | self.assertTrue( |
| 51 | utils.is_feature_enabled( |
| 52 | TEST_FEATURE, |
| 53 | TEST_USER, |
| 54 | ENABLE_TEST_FEATURE_FLAG, |
Zhuoyao Zhang | a5bec26 | 2024-11-05 19:28:23 +0000 | [diff] [blame] | 55 | 90, |
Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 56 | ) |
| 57 | ) |
| 58 | |
Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 59 | def test_feature_disabled_with_rollout_percentage(self): |
| 60 | self.assertFalse( |
| 61 | utils.is_feature_enabled( |
| 62 | TEST_FEATURE, |
| 63 | TEST_USER, |
| 64 | ENABLE_TEST_FEATURE_FLAG, |
Zhuoyao Zhang | a5bec26 | 2024-11-05 19:28:23 +0000 | [diff] [blame] | 65 | 10, |
Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 66 | ) |
| 67 | ) |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | unittest.main() |