Parcourir la source

Tests in separaten Ordner ausgelagert

gc-server3 il y a 15 heures
Parent
commit
7ab3e7ac62

+ 0 - 0
tests/__init__.py


+ 0 - 0
tests/gchr/__init__.py


+ 0 - 0
gchr/tests/test_gchr.py → tests/gchr/test_gchr.py


+ 0 - 0
tests/ldap/__init__.py


+ 4 - 0
tests/ldap/temp/new_selected.json

@@ -0,0 +1,4 @@
+[
+  "cn=group1,ou=groups,dc=example,dc=com",
+  "cn=group2,ou=groups,dc=example,dc=com"
+]

+ 1 - 0
tests/ldap/temp/selected.json

@@ -0,0 +1 @@
+["cn=group1,ou=groups,dc=example,dc=com"]

+ 1 - 0
tests/ldap/temp/test.json

@@ -0,0 +1 @@
+{"entries": [{"dn": "cn=group1,dc=example,dc=com", "attributes": {"cn": "group1"}}]}

+ 128 - 0
tests/ldap/test_convert_ldap_to_db.py

@@ -0,0 +1,128 @@
+import json
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+
+from ldap.convert_ldap_to_db import (
+    get_ou_subgroups,
+    get_subgroups,
+    print_group_membership,
+    read_ldap_json,
+    read_or_create_selected_groups,
+    set_group_member_of,
+)
+
+
+@pytest.fixture
+def tmp_path():
+    return Path(__file__).parent / "temp"
+
+
+@pytest.fixture
+def sample_groups():
+    return {
+        "cn=group1,ou=groups,dc=example,dc=com": {
+            "cn": "group1",
+            "member": ["cn=group2,ou=groups,dc=example,dc=com"],
+        },
+        "cn=group2,ou=groups,dc=example,dc=com": {
+            "cn": "group2",
+            "member": ["cn=user1,ou=users,dc=example,dc=com"],
+        },
+        "cn=user1,ou=users,dc=example,dc=com": {"cn": "user1", "member": []},
+    }
+
+
+@pytest.fixture
+def sample_users():
+    return {
+        "cn=user1,ou=users,dc=example,dc=com": {
+            "sAMAccountName": "user1",
+            "cn": "User One",
+            "mail": "user1@example.com",
+            "memberOf": ["cn=group2,ou=groups,dc=example,dc=com"],
+        },
+    }
+
+
+def test_read_ldap_json(tmp_path):
+    json_file = tmp_path / "test.json"
+    test_data = {
+        "entries": [
+            {"dn": "cn=group1,dc=example,dc=com", "attributes": {"cn": "group1"}},
+        ]
+    }
+    json_file.write_text(json.dumps(test_data), encoding="latin-1")
+
+    with patch("ldap.convert_ldap_to_db.base_dir", tmp_path):
+        result = read_ldap_json("test.json")
+
+    assert result == {"cn=group1,dc=example,dc=com": {"cn": "group1"}}
+
+
+def test_read_or_create_selected_groups_existing(tmp_path, sample_groups):
+    json_file = tmp_path / "selected.json"
+    selected = ["cn=group1,ou=groups,dc=example,dc=com"]
+    json_file.write_text(json.dumps(selected), encoding="latin-1")
+
+    with patch("ldap.convert_ldap_to_db.base_dir", tmp_path):
+        result = read_or_create_selected_groups("selected.json", sample_groups)
+
+    assert result == selected
+
+
+def test_read_or_create_selected_groups_create_new(tmp_path, sample_groups):
+    with patch("ldap.convert_ldap_to_db.base_dir", tmp_path):
+        result = read_or_create_selected_groups("new_selected.json", sample_groups)
+
+    assert "cn=group1,ou=groups,dc=example,dc=com" in result
+    assert "cn=group2,ou=groups,dc=example,dc=com" in result
+
+
+def test_get_subgroups(sample_groups):
+    sel_groups = ["cn=group1,ou=groups,dc=example,dc=com"]
+    result = get_subgroups(sample_groups, sel_groups)
+
+    assert "cn=group1,ou=groups,dc=example,dc=com" in result
+    assert "cn=group2,ou=groups,dc=example,dc=com" in result
+
+
+def test_get_subgroups_missing_group(sample_groups):
+    sel_groups = ["cn=nonexistent,ou=groups,dc=example,dc=com"]
+    result = get_subgroups(sample_groups, sel_groups)
+
+    assert result == []
+
+
+def test_set_group_member_of(sample_groups):
+    sel_groups = ["cn=group1,ou=groups,dc=example,dc=com"]
+    set_group_member_of(sample_groups, sel_groups)
+
+    assert sample_groups["cn=group1,ou=groups,dc=example,dc=com"]["memberOf"] == []
+    assert sample_groups["cn=group2,ou=groups,dc=example,dc=com"]["memberOf"] == [
+        "cn=group1,ou=groups,dc=example,dc=com"
+    ]
+
+
+def test_get_ou_subgroups():
+    group_dn = "cn=group1,ou=sales,ou=departments,dc=example,dc=com"
+    result = get_ou_subgroups(group_dn)
+
+    assert "ou=sales,ou=departments,dc=example,dc=com" in result
+    assert "ou=departments,dc=example,dc=com" in result
+
+
+def test_get_ou_subgroups_no_ou():
+    group_dn = "cn=group1,dc=example,dc=com"
+    result = get_ou_subgroups(group_dn)
+
+    assert result == []
+
+
+def test_print_group_membership(capsys, sample_groups):
+    sel_groups = ["cn=group1,ou=groups,dc=example,dc=com"]
+    print_group_membership(sample_groups, sel_groups)
+
+    captured = capsys.readouterr()
+    assert "group1" in captured.out or "cn=group1" in captured.out

+ 0 - 0
tests/sandbox/__init__.py


+ 8 - 0
tests/sandbox/test_ohno.py

@@ -0,0 +1,8 @@
+from sandbox import ohno
+
+
+def test_simple():
+    board = ohno.Board(4)
+    board.set_initial([[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]])
+    solved = board.solve()
+    assert len(solved) == 1

+ 209 - 0
tests/sandbox/test_relationship.py

@@ -0,0 +1,209 @@
+from sandbox import relationship
+
+relations_first_grade_female = [
+    (("1.2.3.4.5", "1.2.3.4", "w"), "Mutter"),
+    (("1.2.3.4.5", "1.2.3", "w"), "Großmutter"),
+    (("1.2.3.4.5", "1.2", "w"), "Ur-Großmutter"),
+    (("1.2.3.4.5", "1", "w"), "Ur-Ur-Großmutter"),
+    (("1.2.3.4.5", "1.2.3.4.5.6", "w"), "Tochter"),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7", "w"), "Enkelin"),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7.8", "w"), "Ur-Enkelin"),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7.8.9", "w"), "Ur-Ur-Enkelin"),
+]
+
+relations_second_grade_female = [
+    (("1.2.3.4.5", "1.2.3.x", "w"), "Tante"),
+    (("1.2.3.4.5", "1.2.x", "w"), "Großtante"),
+    (("1.2.3.4.5", "1.x", "w"), "Ur-Großtante"),
+    (("1.2.3.4.5", "x", "w"), "Ur-Ur-Großtante"),
+    (("1.2.3.4.5", "1.2.3.4.x", "w"), "Schwester"),
+    (("1.2.3.4.5", "1.2.3.4.x.6", "w"), "Nichte"),
+    (("1.2.3.4.5", "1.2.3.4.x.6.7", "w"), "Großnichte"),
+    (("1.2.3.4.5", "1.2.3.4.x.6.7.8", "w"), "Ur-Großnichte"),
+    (("1.2.3.4.5", "1.2.3.4.x.6.7.8.9", "w"), "Ur-Ur-Großnichte"),
+]
+
+relations_higher_grade_female = [
+    (("1.2.3.4.5", "1.2.3.x", "w"), "Tante"),
+    (("1.2.3.4.5", "1.2.x.x", "w"), "Tante 2. Grades"),
+    (("1.2.3.4.5", "1.x.x.x", "w"), "Tante 3. Grades"),
+    (("1.2.3.4.5", "x.x.x.x", "w"), "Tante 4. Grades"),
+    (("1.2.3.4.5", "1.2.x", "w"), "Großtante"),
+    (("1.2.3.4.5", "1.x.x", "w"), "Großtante 2. Grades"),
+    (("1.2.3.4.5", "x.x.x", "w"), "Großtante 3. Grades"),
+    (("1.2.3.4.5", "1.x", "w"), "Ur-Großtante"),
+    (("1.2.3.4.5", "x.x", "w"), "Ur-Großtante 2. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x", "w"), "Nichte"),
+    (("1.2.3.4.5", "1.2.3.x.x.x", "w"), "Nichte 2. Grades"),
+    (("1.2.3.4.5", "1.2.x.x.x.x", "w"), "Nichte 3. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x.x", "w"), "Nichte 4. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x", "w"), "Großnichte"),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x", "w"), "Großnichte 2. Grades"),
+    (("1.2.3.4.5", "1.2.x.x.x.x.x", "w"), "Großnichte 3. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x.x.x", "w"), "Großnichte 4. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x.x", "w"), "Ur-Großnichte"),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x.x", "w"), "Ur-Großnichte 2. Grades"),
+    (("1.2.3.4.5", "1.2.x.x.x.x.x.x", "w"), "Ur-Großnichte 3. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x.x.x.x", "w"), "Ur-Großnichte 4. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x.x.x", "w"), "Ur-Ur-Großnichte"),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x.x.x", "w"), "Ur-Ur-Großnichte 2. Grades"),
+]
+
+relations_special_grade_female = [
+    (("1.2.3.4.5", "1.2.3.4.x", "w"), "Schwester"),
+    (("1.2.3.4.5", "1.2.3.x.x", "w"), "Cousine"),
+    (("1.2.3.4.5", "1.2.x.x.x", "w"), "Cousine 2. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x", "w"), "Cousine 3. Grades"),
+]
+
+relations_first_grade_male = [
+    (("1.2.3.4.5", "1.2.3.4", "m"), "Vater"),
+    (("1.2.3.4.5", "1.2.3", "m"), "Großvater"),
+    (("1.2.3.4.5", "1.2", "m"), "Ur-Großvater"),
+    (("1.2.3.4.5", "1", "m"), "Ur-Ur-Großvater"),
+    (("1.2.3.4.5", "1.2.3.4.5.6", "m"), "Sohn"),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7", "m"), "Enkel"),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7.8", "m"), "Ur-Enkel"),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7.8.9", "m"), "Ur-Ur-Enkel"),
+]
+
+relations_second_grade_male = [
+    (("1.2.3.4.5", "1.2.3.x", "m"), "Onkel"),
+    (("1.2.3.4.5", "1.2.x", "m"), "Großonkel"),
+    (("1.2.3.4.5", "1.x", "m"), "Ur-Großonkel"),
+    (("1.2.3.4.5", "x", "m"), "Ur-Ur-Großonkel"),
+    (("1.2.3.4.5", "1.2.3.4.x", "m"), "Bruder"),
+    (("1.2.3.4.5", "1.2.3.4.x.6", "m"), "Neffe"),
+    (("1.2.3.4.5", "1.2.3.4.x.6.7", "m"), "Großneffe"),
+    (("1.2.3.4.5", "1.2.3.4.x.6.7.8", "m"), "Ur-Großneffe"),
+    (("1.2.3.4.5", "1.2.3.4.x.6.7.8.9", "m"), "Ur-Ur-Großneffe"),
+]
+
+relations_higher_grade_male = [
+    (("1.2.3.4.5", "1.2.3.x", "m"), "Onkel"),
+    (("1.2.3.4.5", "1.2.x.x", "m"), "Onkel 2. Grades"),
+    (("1.2.3.4.5", "1.x.x.x", "m"), "Onkel 3. Grades"),
+    (("1.2.3.4.5", "x.x.x.x", "m"), "Onkel 4. Grades"),
+    (("1.2.3.4.5", "1.2.x", "m"), "Großonkel"),
+    (("1.2.3.4.5", "1.x.x", "m"), "Großonkel 2. Grades"),
+    (("1.2.3.4.5", "x.x.x", "m"), "Großonkel 3. Grades"),
+    (("1.2.3.4.5", "1.x", "m"), "Ur-Großonkel"),
+    (("1.2.3.4.5", "x.x", "m"), "Ur-Großonkel 2. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x", "m"), "Neffe"),
+    (("1.2.3.4.5", "1.2.3.x.x.x", "m"), "Neffe 2. Grades"),
+    (("1.2.3.4.5", "1.2.x.x.x.x", "m"), "Neffe 3. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x.x", "m"), "Neffe 4. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x", "m"), "Großneffe"),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x", "m"), "Großneffe 2. Grades"),
+    (("1.2.3.4.5", "1.2.x.x.x.x.x", "m"), "Großneffe 3. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x.x.x", "m"), "Großneffe 4. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x.x", "m"), "Ur-Großneffe"),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x.x", "m"), "Ur-Großneffe 2. Grades"),
+    (("1.2.3.4.5", "1.2.x.x.x.x.x.x", "m"), "Ur-Großneffe 3. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x.x.x.x", "m"), "Ur-Großneffe 4. Grades"),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x.x.x", "m"), "Ur-Ur-Großneffe"),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x.x.x", "m"), "Ur-Ur-Großneffe 2. Grades"),
+]
+
+relations_special_grade_male = [
+    (("1.2.3.4.5", "1.2.3.4.x", "m"), "Bruder"),
+    (("1.2.3.4.5", "1.2.3.x.x", "m"), "Cousin"),
+    (("1.2.3.4.5", "1.2.x.x.x", "m"), "Cousin 2. Grades"),
+    (("1.2.3.4.5", "1.x.x.x.x", "m"), "Cousin 3. Grades"),
+]
+
+relations_higher_grade_distance = [
+    (("1.2.3.4.5", "1.2.3.4.5.6", "w"), "Tochter", 1),
+    (("1.2.3.4.5", "1.2.3.4.5.6.7", "w"), "Enkelin", 2),
+    (("1.2.3.4.5", "1", "w"), "Ur-Ur-Großmutter", 4),
+    (("1.2.3.4.5", "1.2.3.x", "w"), "Tante", 3),
+    (("1.2.3.4.5", "1.2.x.x", "w"), "Tante 2. Grades", 5),
+    (("1.2.3.4.5", "1.x.x.x", "w"), "Tante 3. Grades", 7),
+    (("1.2.3.4.5", "x.x.x.x", "w"), "Tante 4. Grades", 9),
+    (("1.2.3.4.5", "1.2.x", "w"), "Großtante", 4),
+    (("1.2.3.4.5", "1.x.x", "w"), "Großtante 2. Grades", 6),
+    (("1.2.3.4.5", "x.x.x", "w"), "Großtante 3. Grades", 8),
+    (("1.2.3.4.5", "1.x", "w"), "Ur-Großtante", 5),
+    (("1.2.3.4.5", "x.x", "w"), "Ur-Großtante 2. Grades", 7),
+    (("1.2.3.4.5", "1.2.3.4.x.x", "w"), "Nichte", 3),
+    (("1.2.3.4.5", "1.2.3.x.x.x", "w"), "Nichte 2. Grades", 5),
+    (("1.2.3.4.5", "1.2.x.x.x.x", "w"), "Nichte 3. Grades", 7),
+    (("1.2.3.4.5", "1.x.x.x.x.x", "w"), "Nichte 4. Grades", 9),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x", "w"), "Großnichte", 4),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x", "w"), "Großnichte 2. Grades", 6),
+    (("1.2.3.4.5", "1.2.x.x.x.x.x", "w"), "Großnichte 3. Grades", 8),
+    (("1.2.3.4.5", "1.x.x.x.x.x.x", "w"), "Großnichte 4. Grades", 10),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x.x", "w"), "Ur-Großnichte", 5),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x.x", "w"), "Ur-Großnichte 2. Grades", 7),
+    (("1.2.3.4.5", "1.2.x.x.x.x.x.x", "w"), "Ur-Großnichte 3. Grades", 9),
+    (("1.2.3.4.5", "1.x.x.x.x.x.x.x", "w"), "Ur-Großnichte 4. Grades", 11),
+    (("1.2.3.4.5", "1.2.3.4.x.x.x.x.x", "w"), "Ur-Ur-Großnichte", 6),
+    (("1.2.3.4.5", "1.2.3.x.x.x.x.x.x", "w"), "Ur-Ur-Großnichte 2. Grades", 8),
+]
+
+burghard = [
+    ("Harald", "1.4.6.3.4", "m"),
+    ("Alice", "1.4.6.3.4.1", "w"),
+    ("Janina", "1.4.6.3.4.2", "w"),
+    ("Theresa", "1.4.6.3.4.2.1", "w"),
+    ("Valentin", "1.4.6.3.4.2.2", "m"),
+    ("Freya", "1.4.6.3.4.3", "w"),
+]
+
+burghard_freya = [("Freya", "1.4.6.3.4.3", "w", "Tochter", "Harald", "1.4.6.3.4", "m", "Vater", 1)]
+
+burghard_all = [("Harald", "1.4.6.3.4", "m", "ich", "Harald", "1.4.6.3.4", "m", "ich", 0)]
+
+
+def relations(relations_list):
+    for rel, title in relations_list:
+        result = relationship.relationship_name(*rel)
+        assert result == title
+
+
+def test_first_grade_female():
+    relations(relations_first_grade_female)
+
+
+def test_second_grade_female():
+    relations(relations_second_grade_female)
+
+
+def test_higher_grade_female():
+    relations(relations_higher_grade_female)
+
+
+def test_special_grade_female():
+    relations(relations_special_grade_female)
+
+
+def test_first_grade_male():
+    relations(relations_first_grade_male)
+
+
+def test_second_grade_male():
+    relations(relations_second_grade_male)
+
+
+def test_higher_grade_male():
+    relations(relations_higher_grade_male)
+
+
+def test_special_grade_male():
+    relations(relations_special_grade_male)
+
+
+def test_distance():
+    for rel, title, distance in relations_higher_grade_distance:
+        result = relationship.relationship_distance(rel[0], rel[1])
+        assert result == distance, title
+
+
+def test_relations_list():
+    result = relationship.relationship_list(burghard, 5)
+    assert result[0] == burghard_freya[0]
+
+
+def test_relations_list_all():
+    result = relationship.relationship_list_all(burghard)
+    assert result[0] == burghard_all[0]

+ 147 - 0
tests/sandbox/test_sudoku_solver.py

@@ -0,0 +1,147 @@
+from sandbox import sudoku_solver
+
+board_1 = [
+    ".1.82....",
+    "...3.6...",
+    "54..7....",
+    "..2...95.",
+    "7.52..1.4",
+    ".89...2..",
+    "....8..79",
+    "...5.3...",
+    "...792.3.",
+]
+
+solution_1 = [
+    "317825496",
+    "298346715",
+    "546971328",
+    "432618957",
+    "765239184",
+    "189457263",
+    "623184579",
+    "971563842",
+    "854792631",
+]
+
+board_2 = [
+    "16...7.9.",
+    ".281.967.",
+    ".....82..",
+    "...7....4",
+    "....6....",
+    "5....3...",
+    "..75.....",
+    ".869.173.",
+    ".4.3...86",
+]
+
+solution_2 = [
+    "163427598",
+    "428159673",
+    "759638241",
+    "632795814",
+    "891264357",
+    "574813962",
+    "317586429",
+    "286941735",
+    "945372186",
+]
+
+board_3 = [
+    "..1..8763",
+    "8..9.....",
+    "....1..5.",
+    ".8....4..",
+    "1.54.3...",
+    ".6....2..",
+    "....6..2.",
+    "4..8.....",
+    "..6..1378",
+]
+
+solution_3 = [
+    "941258763",
+    "857936142",
+    "632714859",
+    "789625431",
+    "125483697",
+    "364179285",
+    "518367924",
+    "473892516",
+    "296541378",
+]
+
+empty = ["." * 9 for i in range(9)]
+
+
+def test_simple():
+    board = sudoku_solver.Board()
+    board.set_initial(board_1)
+    row = board.get_row(0)
+    assert row == board_1[0]
+    col = board.get_col(0)
+    assert col == "..5.7...."
+    block = board.get_block(0, 0)
+    assert block == [".1.", "...", "54."]
+    block = board.get_block(4, 6)
+    assert block == ["95.", "1.4", "2.."]
+
+
+def test_simple_set():
+    board = sudoku_solver.Board()
+    board.set_initial(board_1)
+    board.set_cell(5, 7, "6")
+    row = board.get_row(5)
+    assert row == ".89...26."
+
+
+def test_possibilities():
+    board = sudoku_solver.Board()
+    board.set_initial(board_1)
+    p = board.get_possibilities(1, 4)
+    assert p == {"1", "4", "5"}
+
+
+def test_all_possibilities():
+    board = sudoku_solver.Board()
+    board.set_initial(board_1)
+    p = board.get_all_possibilities()
+    assert p[1][4] == {"1", "4", "5"}
+
+
+def test_unique_cell():
+    board = sudoku_solver.Board()
+    board.set_initial(board_1)
+    solving_steps = [
+        (5, 7, "6"),
+        (4, 7, "8"),
+        (4, 5, "9"),
+        (2, 5, "1"),
+        (2, 3, "9"),
+        (2, 7, "2"),
+        (6, 5, "4"),
+        (0, 5, "5"),
+        (1, 4, "4"),
+    ]
+    for x, y, v in solving_steps:
+        cell = board.get_unique_cell()
+        assert cell == (x, y)
+        board.set_cell(x, y, v)
+
+
+def test_solve():
+    board = sudoku_solver.Board()
+    board.set_initial(board_2)
+    b = board.solve()
+    assert b == solution_2
+
+    board = sudoku_solver.Board()
+    board.set_initial(board_1)
+    b = board.solve()
+    assert b == solution_1
+
+    board = sudoku_solver.Board()
+    board.set_initial(board_3)
+    b = board.solve()
+    assert b == solution_3