test_variable.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """Unit tests for Variable class."""
  2. import pytest
  3. from cli.core.template.variable import Variable
  4. # Test constants
  5. TEST_PORT = 8080
  6. TEST_COUNT = 42
  7. TEST_RATE = 3.14
  8. class TestVariableInitialization:
  9. """Tests for Variable initialization."""
  10. def test_create_simple_variable(self):
  11. """Test creating a simple string variable."""
  12. var = Variable({"name": "test_var", "type": "str"})
  13. assert var.name == "test_var"
  14. assert var.type == "str"
  15. assert var.value is None
  16. def test_create_variable_with_default(self):
  17. """Test creating a variable with default value."""
  18. var = Variable({"name": "port", "type": "int", "default": TEST_PORT})
  19. assert var.name == "port"
  20. assert var.value == TEST_PORT
  21. def test_create_bool_variable_without_default(self):
  22. """Test that bool variables default to False."""
  23. var = Variable({"name": "enabled", "type": "bool"})
  24. assert var.value is False
  25. def test_create_variable_with_description(self):
  26. """Test creating a variable with description."""
  27. var = Variable({"name": "test", "type": "str", "description": "Test variable"})
  28. assert var.description == "Test variable"
  29. def test_missing_name_raises_error(self):
  30. """Test that missing name key raises ValueError."""
  31. with pytest.raises(ValueError, match="must contain 'name' key"):
  32. Variable({"type": "str"})
  33. def test_invalid_data_type_raises_error(self):
  34. """Test that non-dict data raises ValueError."""
  35. with pytest.raises(ValueError, match="must be a dictionary"):
  36. Variable("not a dict")
  37. class TestVariableTypes:
  38. """Tests for variable type handling."""
  39. def test_string_type(self):
  40. """Test string variable type."""
  41. var = Variable({"name": "test", "type": "str", "default": "hello"})
  42. assert var.value == "hello"
  43. def test_int_type(self):
  44. """Test integer variable type."""
  45. var = Variable({"name": "count", "type": "int", "default": TEST_COUNT})
  46. assert var.value == TEST_COUNT
  47. def test_bool_type_true(self):
  48. """Test boolean variable with true value."""
  49. var = Variable({"name": "enabled", "type": "bool", "default": True})
  50. assert var.value is True
  51. def test_bool_type_false(self):
  52. """Test boolean variable with false value."""
  53. var = Variable({"name": "disabled", "type": "bool", "default": False})
  54. assert var.value is False
  55. def test_float_type(self):
  56. """Test float variable type."""
  57. var = Variable({"name": "rate", "type": "float", "default": TEST_RATE})
  58. assert var.value == TEST_RATE
  59. class TestVariableProperties:
  60. """Tests for variable properties."""
  61. def test_sensitive_flag(self):
  62. """Test sensitive flag for passwords."""
  63. var = Variable({"name": "password", "type": "str", "sensitive": True})
  64. assert var.sensitive is True
  65. def test_autogenerated_flag(self):
  66. """Test autogenerated flag."""
  67. var = Variable({"name": "secret", "type": "str", "autogenerated": True})
  68. assert var.autogenerated is True
  69. def test_required_flag(self):
  70. """Test required flag."""
  71. var = Variable({"name": "hostname", "type": "str", "required": True})
  72. assert var.required is True
  73. def test_options_list(self):
  74. """Test enum variable with options."""
  75. var = Variable({"name": "mode", "type": "enum", "options": ["dev", "prod"]})
  76. assert var.options == ["dev", "prod"]
  77. def test_extra_help_text(self):
  78. """Test extra help text."""
  79. var = Variable({"name": "test", "type": "str", "extra": "Additional info"})
  80. assert var.extra == "Additional info"
  81. class TestVariableNeeds:
  82. """Tests for variable dependency constraints."""
  83. def test_needs_single_string(self):
  84. """Test needs constraint as single string."""
  85. var = Variable({"name": "test", "type": "str", "needs": "other_var=value"})
  86. assert var.needs == ["other_var=value"]
  87. def test_needs_semicolon_separated(self):
  88. """Test needs constraint with semicolon-separated conditions."""
  89. var = Variable({"name": "test", "type": "str", "needs": "var1=value1;var2=value2"})
  90. assert var.needs == ["var1=value1", "var2=value2"]
  91. def test_needs_list(self):
  92. """Test needs constraint as list."""
  93. var = Variable({"name": "test", "type": "str", "needs": ["var1=value1", "var2=value2"]})
  94. assert var.needs == ["var1=value1", "var2=value2"]
  95. def test_needs_empty(self):
  96. """Test variable without needs constraint."""
  97. var = Variable({"name": "test", "type": "str"})
  98. assert var.needs == []
  99. class TestVariableConversion:
  100. """Tests for variable value conversion."""
  101. def test_convert_string_to_int(self):
  102. """Test converting string to integer."""
  103. var = Variable({"name": "count", "type": "int"})
  104. result = var.convert("42")
  105. assert result == TEST_COUNT
  106. assert isinstance(result, int)
  107. def test_convert_string_to_bool_true(self):
  108. """Test converting string to boolean true."""
  109. var = Variable({"name": "enabled", "type": "bool"})
  110. assert var.convert("true") is True
  111. assert var.convert("1") is True
  112. assert var.convert("yes") is True
  113. def test_convert_string_to_bool_false(self):
  114. """Test converting string to boolean false."""
  115. var = Variable({"name": "disabled", "type": "bool"})
  116. assert var.convert("false") is False
  117. assert var.convert("0") is False
  118. assert var.convert("no") is False
  119. def test_convert_string_to_float(self):
  120. """Test converting string to float."""
  121. var = Variable({"name": "rate", "type": "float"})
  122. result = var.convert("3.14")
  123. assert result == TEST_RATE
  124. assert isinstance(result, float)
  125. @pytest.mark.parametrize(
  126. "var_type,default_value,expected",
  127. [
  128. ("str", "hello", "hello"),
  129. ("int", TEST_COUNT, TEST_COUNT),
  130. ("bool", True, True),
  131. ("float", TEST_RATE, TEST_RATE),
  132. ],
  133. )
  134. def test_variable_types_parametrized(var_type, default_value, expected):
  135. """Test various variable types with parametrized inputs."""
  136. var = Variable({"name": "test", "type": var_type, "default": default_value})
  137. assert var.value == expected