Skip to content

Commit

Permalink
chore: add integration tests for serializing transformed keys (#115)
Browse files Browse the repository at this point in the history
* add integration tests for rust and dart

* add integration tests for swift

* add integration tests for kotlin
  • Loading branch information
joshmossas authored Nov 22, 2024
1 parent f28b3b3 commit aaa9f53
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
25 changes: 25 additions & 0 deletions tests/clients/dart/test/test_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ Future<void> main() async {
expect(result2, equals(input2));
expect(input == input2, equals(false));
});
test("can send/receive objects with snake_case keys", () async {
final payload = ObjectWithSnakeCaseKeys(
createdAt: targetDate,
displayName: "testing 123",
phoneNumber: "211-211-2111",
emailAddress: "johndoe@gmail",
);
final result = await client.tests.sendObjectWithSnakeCaseKeys(payload);
expect(result, equals(payload));
});
test("can send/receive objects with PascalCase keys", () async {
final payload = ObjectWithPascalCaseKeys(
createdAt: targetDate,
displayName: "testing 123",
phoneNumber: null,
);
final result = await client.tests.sendObjectWithPascalCaseKeys(payload);
expect(result, equals(payload));
final payload2 = payload.copyWith(
phoneNumber: () => "2112112111",
emailAddress: () => "johndoe@gmail.com",
);
final result2 = await client.tests.sendObjectWithPascalCaseKeys(payload2);
expect(result2, equals(payload2));
});
test("supports injecting custom http clients", () async {
final result = await clientWCustomHttpClient.tests.sendObject(input);
expect(result.array.length, equals(input.array.length));
Expand Down
32 changes: 31 additions & 1 deletion tests/clients/kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import io.ktor.client.plugins.*
import kotlinx.coroutines.*
import kotlinx.serialization.json.JsonPrimitive
import java.time.Instant
import java.util.*

fun main() {
val httpClient = HttpClient() {
Expand Down Expand Up @@ -84,6 +85,35 @@ fun main() {
expect(tag, result.string, "hello world")
}

runBlocking {
val tag = "SEND/RECEIVE OBJECT WITH SNAKE_CASE KEYS"
val input = ObjectWithSnakeCaseKeys(
createdAt = targetDate,
displayName = "john doe",
phoneNumber = null,
emailAddress = "johndoe@gmail.com",
isAdmin = null
)
val result = client.tests.sendObjectWithSnakeCaseKeys(input)
expect(tag, result, input)
}

runBlocking {
val tag = "SEND/RECEIVE OBJECT WITH PASCAL CASE KEYS"
var input = ObjectWithPascalCaseKeys(
createdAt = targetDate,
displayName = "john doe",
phoneNumber = "211-211-2111",
emailAddress = null,
isAdmin = true
)
val result = client.tests.sendObjectWithPascalCaseKeys(input)
expect(tag, result, input)
input = input.copy(isAdmin = null)
val result2 = client.tests.sendObjectWithPascalCaseKeys(input)
expect(tag, result2, input)
}

runBlocking {
val tag = "UNAUTHENTICATED REQUEST RETURNS ERROR"
try {
Expand Down Expand Up @@ -434,7 +464,7 @@ fun testSseReconnectsWithNewCredentials(httpClient: HttpClient, baseUrl: String)
httpClient = httpClient,
baseUrl = baseUrl,
headers = {
val newHeader = "kt_${java.util.UUID.randomUUID().toString()}"
val newHeader = "kt_${UUID.randomUUID().toString()}"
headers.add(newHeader)
mutableMapOf(Pair("x-test-header", newHeader))
}
Expand Down
50 changes: 48 additions & 2 deletions tests/clients/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ mod tests {
};

use crate::test_client::{
AutoReconnectParams, ChatMessageParams, StreamConnectionErrorTestParams,
StreamLargeObjectsResponse,
self, AutoReconnectParams, ChatMessageParams, ObjectWithPascalCaseKeys,
ObjectWithSnakeCaseKeys, StreamConnectionErrorTestParams, StreamLargeObjectsResponse,
};
#[allow(deprecated)]
use crate::test_client::{
Expand Down Expand Up @@ -118,6 +118,52 @@ mod tests {
assert_ne!(result.as_ref().unwrap(), &input);
}

#[tokio::test]
async fn can_send_and_receive_objects_with_snake_case_keys() {
let client = TestClient::create(get_config(headers()));
let target_date = DateTime::<Utc>::from_timestamp_millis(TARGET_MS).unwrap();
let mut input = ObjectWithSnakeCaseKeys {
created_at: target_date.fixed_offset(),
display_name: "John Doe".to_string(),
phone_number: None,
email_address: Some("johndoe@gmail.com".to_string()),
is_admin: Some(true),
};
let result = client
.tests
.send_object_with_snake_case_keys(input.clone())
.await;
assert_eq!(result.as_ref().unwrap(), &input.clone());
input.display_name = "something else".to_string();
assert_ne!(result.as_ref().unwrap(), &input);
}

#[tokio::test]
async fn can_send_and_receive_objects_with_pascal_case_keys() {
let client = TestClient::create(get_config(headers()));
let target_date = DateTime::<Utc>::from_timestamp_millis(TARGET_MS).unwrap();
let mut input = ObjectWithPascalCaseKeys {
created_at: target_date.fixed_offset(),
display_name: "John Doe".to_string(),
phone_number: None,
email_address: Some("johndoe@gmail.com".to_string()),
is_admin: Some(true),
};
let result = client
.tests
.send_object_with_pascal_case_keys(input.clone())
.await;
assert_eq!(result.as_ref().unwrap(), &input.clone());
input.display_name = "something else".to_string();
input.email_address = None;
assert_ne!(result.as_ref().unwrap(), &input);
let result2 = client
.tests
.send_object_with_pascal_case_keys(input.clone())
.await;
assert_eq!(result2.as_ref().unwrap(), &input);
}

#[tokio::test]
async fn unauthenticated_client_returns_error() {
let config = get_config(HashMap::new());
Expand Down
25 changes: 25 additions & 0 deletions tests/clients/swift/Tests/TestClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ final class TestSwiftClientTests: XCTestCase {
let result = try await client.tests.sendObject(input)
XCTAssertEqual(input, result)
}
func testSendObjectWithSnakeCaseKeys() async throws {
let input = ObjectWithSnakeCaseKeys(
createdAt: testDate,
displayName: "John Doe",
phoneNumber: nil,
emailAddress: "johndoe@gmail.com",
isAdmin: false
)
let result = try await client.tests.sendObjectWithSnakeCaseKeys(input)
XCTAssertEqual(input, result)
}
func testSendObjectWithPascalCaseKeys() async throws {
let input = ObjectWithPascalCaseKeys(
createdAt: testDate,
displayName: "John Doe",
phoneNumber: "2112112111",
emailAddress: nil,
isAdmin: nil
)
let result = try await client.tests.sendObjectWithPascalCaseKeys(input)
XCTAssertEqual(input, result)
var clonedInput = input.clone()
clonedInput.emailAddress = "johndoe@gmail.com"
XCTAssertNotEqual(clonedInput, result)
}
func testSendObjectWithNullableFields() async throws {
let input = ObjectWithEveryNullableType(
any: JSON("null"),
Expand Down

0 comments on commit aaa9f53

Please sign in to comment.