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."""
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  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,
55            90,
56        )
57    )
58
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,
65            10,
66        )
67    )
68
69
70if __name__ == '__main__':
71  unittest.main()
72