Big restructuring by moving to python

This commit is contained in:
budtmo
2023-05-09 19:34:44 +02:00
parent abcdf3f4d1
commit 6767970307
448 changed files with 2493 additions and 5679 deletions
+9
View File
@@ -0,0 +1,9 @@
from unittest import TestCase
class BaseTest(TestCase):
def setUp(self) -> None:
pass
def tearDown(self) -> None:
pass
+21
View File
@@ -0,0 +1,21 @@
import os
from src.constants import ENV
from src.tests import BaseTest
class BaseDeviceTest(BaseTest):
DEVICE_ENVS = {
ENV.WORK_PATH: "/home/androidusr",
ENV.USER_BEHAVIOR_ANALYTICS: str(False),
ENV.EMULATOR_NO_SKIN: str(False)
}
def setUp(self) -> None:
for k, v in self.DEVICE_ENVS.items():
os.environ[k] = v
def tearDown(self) -> None:
for k in self.DEVICE_ENVS.keys():
if os.environ[k]:
del os.environ[k]
+8
View File
@@ -0,0 +1,8 @@
from src.device import Device
from src.tests.device import BaseDeviceTest
class TestDevice(BaseDeviceTest):
def test_create_device(self):
with self.assertRaises(TypeError):
Device()
+87
View File
@@ -0,0 +1,87 @@
import mock
from src.device.emulator import Emulator
from src.tests.device import BaseDeviceTest
class TestEmulator(BaseDeviceTest):
def setUp(self) -> None:
super().setUp()
self.name = "my_emu"
self.device = "Nexus 4"
self.a_version = "10.0"
self.d_partition = "550m"
self.additional_args = ""
self.i_type = "google_apis"
self.s_img = "x86"
self.emu = Emulator(self.name, self.device, self.a_version, self.d_partition,
self.additional_args, self.i_type, self.s_img)
def tearDown(self) -> None:
super().tearDown()
def test_adb_name(self):
my_emu = Emulator("my_other_emu", self.device, self.a_version, self.d_partition,
self.additional_args, self.i_type, self.s_img)
self.assertNotEqual(self.emu.adb_name, my_emu.adb_name)
def test_invalid_device(self):
with self.assertRaises(RuntimeError):
Emulator("my_other_emu", "unknown device", self.a_version, self.d_partition,
self.additional_args, self.i_type, self.s_img)
with self.assertRaises(RuntimeError):
Emulator("my_other_emu", "NEXUS 5", self.a_version, self.d_partition,
self.additional_args, self.i_type, self.s_img)
def test_invalid_android_version(self):
with self.assertRaises(RuntimeError):
Emulator("my_other_emu", self.device, "0.0", self.d_partition,
self.additional_args, self.i_type, self.s_img)
@mock.patch("os.path.exists", mock.MagicMock(return_value=False))
def test_initialisation_config_not_exist(self):
self.assertEqual(self.emu.is_initialized(), False)
@mock.patch("os.path.exists", mock.MagicMock(return_value=True))
@mock.patch("builtins.open", mock.mock_open(read_data=""))
def test_initialisation_device_not_exist(self):
self.assertEqual(self.emu.is_initialized(), False)
@mock.patch("os.path.exists", mock.MagicMock(return_value=True))
@mock.patch("builtins.open", mock.mock_open(read_data="hw.device.name=Nexus 4\n"))
def test_initialisation_device_exists(self):
self.assertEqual(self.emu.is_initialized(), True)
@mock.patch("src.device.Device.set_status")
@mock.patch("src.device.emulator.Emulator._add_profile")
@mock.patch("subprocess.check_call")
@mock.patch("src.device.emulator.Emulator._add_skin")
@mock.patch("src.device.emulator.Emulator.is_initialized", mock.MagicMock(return_value=False))
def test_create_device_not_exist(self, mocked_status, mocked_profile, mocked_subprocess, mocked_skin):
self.emu.create()
self.assertEqual(mocked_status.called, True)
self.assertEqual(mocked_profile.called, True)
self.assertEqual(mocked_subprocess.called, True)
self.assertEqual(mocked_skin.called, True)
@mock.patch("src.device.Device.set_status")
@mock.patch("src.device.emulator.Emulator._add_profile")
@mock.patch("subprocess.check_call")
@mock.patch("src.device.emulator.Emulator._add_skin")
@mock.patch("src.device.emulator.Emulator.is_initialized", mock.MagicMock(return_value=True))
def test_create_device_exists(self, mocked_status, mocked_profile, mocked_subprocess, mocked_skin):
self.emu.create()
self.assertEqual(mocked_status.called, False)
self.assertEqual(mocked_profile.called, False)
self.assertEqual(mocked_subprocess.called, False)
def test_check_adb_command(self):
with mock.patch("subprocess.check_output", mock.MagicMock(return_value="1".encode("utf-8"))):
self.emu.check_adb_command(
self.emu.ReadinessCheck.BOOTED, "mocked_command", "1", 3, 0)
def test_check_adb_command_out_of_attempts(self):
with mock.patch("subprocess.check_output", mock.MagicMock(return_value=" ".encode("utf-8"))):
with self.assertRaises(RuntimeError):
self.emu.check_adb_command(
self.emu.ReadinessCheck.BOOTED, "mocked_command", "1", 3, 0)
View File
+73
View File
@@ -0,0 +1,73 @@
import os
import mock
from src.helper import convert_str_to_bool, get_env_value_or_raise, symlink_force
from src.tests import BaseTest
class TestHelperMethods(BaseTest):
def test_boolean_converter_with_valid_str(self):
self.assertEqual(convert_str_to_bool("TRUE"), True)
self.assertEqual(convert_str_to_bool("true"), True)
self.assertEqual(convert_str_to_bool("T"), True)
self.assertEqual(convert_str_to_bool("Yes"), True)
self.assertEqual(convert_str_to_bool("1"), True)
self.assertEqual(convert_str_to_bool("False"), False)
self.assertEqual(convert_str_to_bool("f"), False)
self.assertEqual(convert_str_to_bool("0"), False)
def test_boolean_converter_with_empty(self):
self.assertEqual(convert_str_to_bool(None), False)
self.assertEqual(convert_str_to_bool(""), False)
def test_boolean_converter_with_invalid_str(self):
self.assertEqual(convert_str_to_bool(" "), False)
self.assertEqual(convert_str_to_bool("test"), False)
def test_boolean_converter_with_invalid_format(self):
with self.assertRaises(AttributeError):
convert_str_to_bool(True)
def test_get_env_value_from_valid_key(self):
env_key = "env_key01"
os.environ[env_key] = "env_value01"
get_env_value_or_raise(env_key)
del os.environ[env_key]
def test_get_env_value_with_empty_string(self):
with self.assertRaises(RuntimeError):
env_key = "env_key01"
os.environ[env_key] = " "
get_env_value_or_raise(env_key)
del os.environ[env_key]
def test_get_env_value_from_invalid_key(self):
with self.assertRaises(RuntimeError):
get_env_value_or_raise("env_key02")
def test_get_env_value_with_invalid_format(self):
with mock.patch("src.logger"):
get_env_value_or_raise(True)
def test_symlink(self):
s = os.path.join("source.txt")
t = os.path.join("target_file.txt")
open(s, "a").close()
symlink_force(s, t)
os.remove(s)
os.remove(t)
def test_symlink_already_exist(self):
s = os.path.join("source.txt")
t = os.path.join("target_file.txt")
open(s, "a").close()
open(t, "a").close()
symlink_force(s, t)
os.remove(s)
os.remove(t)
def test_symlink_file_not_exists(self):
s = os.path.join("source.txt")
t = os.path.join("target_file.txt")
symlink_force(s, t)
os.remove(t)