about summary refs log tree commit diff
path: root/src/tests.rs
blob: 623cb6b59981038c62ae47a673aa4b2ee0726597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use super::*;
use serde_json::to_string;

#[test]
fn test_text_entry_serialization() {
    let entry = LogEntry {
        labels: Value::Null,
        payload: Payload::TextPayload {
            text_payload: "test entry".into(),
        }
    };

    let expected = "{\"labels\":null,\"textPayload\":\"test entry\"}";
    let result = to_string(&entry).expect("serialization failed");

    assert_eq!(expected, result, "Plain text payload should serialize correctly")
}

#[test]
fn test_json_entry_serialization() {
    let entry = LogEntry {
        labels: Value::Null,
        payload: Payload::TextPayload {
            text_payload: "test entry".into(),
        }
    };

    let expected = "{\"labels\":null,\"textPayload\":\"test entry\"}";
    let result = to_string(&entry).expect("serialization failed");

    assert_eq!(expected, result, "Plain text payload should serialize correctly")
}

#[test]
fn test_plain_text_payload() {
    let message = "plain text payload".into();
    let payload = message_to_payload(Some(message));
    let expected = Payload::TextPayload {
        text_payload: "plain text payload".into(),
    };

    assert_eq!(expected, payload, "Plain text payload should be detected correctly");
}

#[test]
fn test_empty_payload() {
    let payload = message_to_payload(None);
    let expected = Payload::TextPayload {
        text_payload: "empty log entry".into(),
    };

    assert_eq!(expected, payload, "Empty payload should be handled correctly");
}

#[test]
fn test_json_payload() {
    let message = "{\"someKey\":\"someValue\", \"otherKey\": 42}".into();
    let payload = message_to_payload(Some(message));
    let expected = Payload::JsonPayload {
        json_payload: json!({
            "someKey": "someValue",
            "otherKey": 42
        })
    };

    assert_eq!(expected, payload, "JSON payload should be detected correctly");
}

#[test]
fn test_json_no_object() {
    // This message can be parsed as valid JSON, but it is not an
    // object - it should be returned as a plain-text payload.
    let message = "42".into();
    let payload = message_to_payload(Some(message));
    let expected = Payload::TextPayload {
        text_payload: "42".into(),
    };

    assert_eq!(expected, payload, "Non-object JSON payload should be plain text");
}