|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.mongodb.kotlin.client.coroutine |
| 17 | + |
| 18 | +import com.mongodb.client.Fixture.getDefaultDatabaseName |
| 19 | +import com.mongodb.client.Fixture.getMongoClientSettings |
| 20 | +import kotlin.test.assertContentEquals |
| 21 | +import kotlin.test.assertEquals |
| 22 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 23 | +import kotlinx.coroutines.flow.map |
| 24 | +import kotlinx.coroutines.flow.toList |
| 25 | +import kotlinx.coroutines.flow.toSet |
| 26 | +import kotlinx.coroutines.runBlocking |
| 27 | +import kotlinx.coroutines.test.runTest |
| 28 | +import org.bson.Document |
| 29 | +import org.junit.jupiter.api.AfterAll |
| 30 | +import org.junit.jupiter.api.AfterEach |
| 31 | +import org.junit.jupiter.api.BeforeAll |
| 32 | +import org.junit.jupiter.api.DisplayName |
| 33 | +import org.junit.jupiter.api.Test |
| 34 | + |
| 35 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 36 | +class SmokeTests { |
| 37 | + |
| 38 | + @AfterEach |
| 39 | + fun afterEach() { |
| 40 | + runBlocking { database?.drop() } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("distinct and return nulls") |
| 45 | + fun testDistinctNullable() = runTest { |
| 46 | + collection!!.insertMany( |
| 47 | + listOf( |
| 48 | + Document.parse("{_id: 1, a: 0}"), |
| 49 | + Document.parse("{_id: 2, a: 1}"), |
| 50 | + Document.parse("{_id: 3, a: 0}"), |
| 51 | + Document.parse("{_id: 4, a: null}"))) |
| 52 | + |
| 53 | + // nulls are auto excluded in reactive streams! |
| 54 | + val actual = collection!!.distinct<Int>("a").toSet() |
| 55 | + assertEquals(setOf(0, 1), actual) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("mapping can return nulls") |
| 60 | + fun testMongoIterableMap() = runTest { |
| 61 | + collection!!.insertMany( |
| 62 | + listOf( |
| 63 | + Document.parse("{_id: 1, a: 0}"), |
| 64 | + Document.parse("{_id: 2, a: 1}"), |
| 65 | + Document.parse("{_id: 3, a: 0}"), |
| 66 | + Document.parse("{_id: 4, a: null}"))) |
| 67 | + |
| 68 | + val actual = collection!!.find().map { it["a"] as Int? }.toList() |
| 69 | + assertContentEquals(listOf(0, 1, 0, null), actual) |
| 70 | + } |
| 71 | + |
| 72 | + companion object { |
| 73 | + |
| 74 | + private var mongoClient: MongoClient? = null |
| 75 | + private var database: MongoDatabase? = null |
| 76 | + private var collection: MongoCollection<Document>? = null |
| 77 | + |
| 78 | + @BeforeAll |
| 79 | + @JvmStatic |
| 80 | + internal fun beforeAll() { |
| 81 | + runBlocking { |
| 82 | + mongoClient = MongoClient.create(getMongoClientSettings()) |
| 83 | + database = mongoClient?.getDatabase(getDefaultDatabaseName()) |
| 84 | + database?.drop() |
| 85 | + collection = database?.getCollection("SmokeTests") |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @AfterAll |
| 90 | + @JvmStatic |
| 91 | + internal fun afterAll() { |
| 92 | + runBlocking { |
| 93 | + collection = null |
| 94 | + database?.drop() |
| 95 | + database = null |
| 96 | + mongoClient?.close() |
| 97 | + mongoClient = null |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments