|
| 1 | +from testcontainers.core.labels import ( |
| 2 | + LABEL_LANG, |
| 3 | + LABEL_SESSION_ID, |
| 4 | + LABEL_TESTCONTAINERS, |
| 5 | + LABEL_VERSION, |
| 6 | + create_labels, |
| 7 | + TESTCONTAINERS_NAMESPACE, |
| 8 | +) |
| 9 | +import pytest |
| 10 | +from testcontainers.core.config import RYUK_IMAGE |
| 11 | + |
| 12 | + |
| 13 | +def assert_in_with_value(labels: dict, label: str, value: str, known_before_test_time: bool) -> None: |
| 14 | + assert label in labels |
| 15 | + if known_before_test_time: |
| 16 | + assert labels[label] == value |
| 17 | + |
| 18 | + |
| 19 | +testdata = [ |
| 20 | + (LABEL_LANG, "python", True), |
| 21 | + (LABEL_TESTCONTAINERS, "true", True), |
| 22 | + (LABEL_SESSION_ID, "some", False), |
| 23 | + (LABEL_VERSION, "some", False), |
| 24 | +] |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("label,value,known_before_test_time", testdata) |
| 28 | +def test_containers_creates_expected_labels(label, value, known_before_test_time): |
| 29 | + actual_labels = create_labels("not-ryuk", None) |
| 30 | + assert_in_with_value(actual_labels, label, value, known_before_test_time) |
| 31 | + |
| 32 | + |
| 33 | +def test_containers_throws_on_namespace_collision(): |
| 34 | + with pytest.raises(ValueError): |
| 35 | + create_labels("not-ryuk", {TESTCONTAINERS_NAMESPACE: "fake"}) |
| 36 | + |
| 37 | + |
| 38 | +def test_containers_respect_custom_labels_if_no_collision(): |
| 39 | + custom_namespace = "org.foo.bar" |
| 40 | + value = "fake" |
| 41 | + actual_labels = create_labels("not-ryuk", {custom_namespace: value}) |
| 42 | + assert_in_with_value(actual_labels, custom_namespace, value, True) |
| 43 | + |
| 44 | + |
| 45 | +def test_if_ryuk_no_session(): |
| 46 | + actual_labels = create_labels(RYUK_IMAGE, None) |
| 47 | + assert LABEL_SESSION_ID not in actual_labels |
| 48 | + |
| 49 | + |
| 50 | +def test_session_are_module_import_scoped(): |
| 51 | + """ |
| 52 | + Asserts that sessions are a module-level variable and don't differ between invocation |
| 53 | + """ |
| 54 | + first_labels = create_labels("not-ryuk", None) |
| 55 | + second_labels = create_labels("not-ryuk", None) |
| 56 | + assert LABEL_SESSION_ID in first_labels |
| 57 | + assert LABEL_SESSION_ID in second_labels |
| 58 | + assert first_labels[LABEL_SESSION_ID] == second_labels[LABEL_SESSION_ID] |
0 commit comments