from assertpy import assert_that
from cfgtools import read_string


def test_searching():
    xml = """
<config>
  <group name="group1">
    <group name="subGroup1">
      <key name="key1">
        <string>value1</string>
      </key>
    </group>
    <group name="subGroup2">
      <key name="key2">
        <string>value2</string>
      </key>
      <key name="key3">
        <string>value3</string>
      </key>
    </group>
  </group>
  <group name="group3">
    <group name="subGroup3">
      <key name="key4">
        <string>value4</string>
      </key>
    </group>
    <group name="subGroup4">
      <key name="key5">
        <string>value5</string>
      </key>
      <key name="key6">
        <string>value6</string>
      </key>
    </group>
  </group>
  <group name="group4">
    <key name="key7">
      <array>
        <string>array1</string>
        <string>array2</string>
        <string>array3</string>
      </array>
    </key>
  </group>    
</config>
"""
    cfg = read_string(xml)

    assert_that(cfg.group("group1").name).is_equal_to("group1")
    assert_that(cfg.group("group3").name).is_equal_to("group3")

    assert_that(cfg.find_group("group1.subGroup1").name).is_equal_to("subGroup1")
    assert_that(cfg.find_group("group3.subGroup4").name).is_equal_to("subGroup4")

    assert_that(cfg.find_key("group1.subGroup1.key1").name).is_equal_to("key1")
    assert_that(cfg.find_key("group3.subGroup4.key6").name).is_equal_to("key6")

    assert_that(cfg.find_value("group1.subGroup1.key1")).is_equal_to("value1")
    assert_that(cfg.find_value("group3.subGroup4.key6")).is_equal_to("value6")

    assert_that(cfg.find_values("group1.subGroup1.key1")).is_length(1)
    assert_that(cfg.find_values("group1.subGroup1.key1")[0]).is_equal_to("value1")
    assert_that(cfg.find_values("group3.subGroup4.key6")).is_length(1)
    assert_that(cfg.find_values("group3.subGroup4.key6")[0]).is_equal_to("value6")
    assert_that(cfg.find_values("group4.key7")).is_equal_to(["array1", "array2", "array3"])