|
| 1 | +// |
| 2 | +// Copyright (c) 2026 Couchbase, 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 | + |
| 17 | +package com.couchbase.lite |
| 18 | + |
| 19 | +import kotlinx.serialization.Serializable |
| 20 | +import kotlinx.serialization.Transient |
| 21 | +import org.junit.Assert |
| 22 | +import org.junit.Test |
| 23 | + |
| 24 | + |
| 25 | +/** Tests serialization-based Collection accessors implemented in CollectionExtensions.kt. */ |
| 26 | +class CollectionSerializationTest : BaseDbTest() { |
| 27 | + //--------------------------------------------- |
| 28 | + // Model-based (serialization) API |
| 29 | + //--------------------------------------------- |
| 30 | + |
| 31 | + @Test |
| 32 | + fun saveNewDocInCollectionFromModel() { |
| 33 | + val id = getUniqueName("test_doc") |
| 34 | + |
| 35 | + // Create a new model and save it: |
| 36 | + val model = TestModel("Nigel", 12) |
| 37 | + testCollection.save(model, id) |
| 38 | + Assert.assertEquals(testCollection, model.documentMeta?.collection) |
| 39 | + Assert.assertEquals(id, model.documentMeta?.id) |
| 40 | + |
| 41 | + // Read it back: |
| 42 | + Assert.assertEquals(1, testCollection.count) |
| 43 | + val gotModel = testCollection.getDocumentAs<TestModel>(id)!! |
| 44 | + Assert.assertEquals(model, gotModel) |
| 45 | + |
| 46 | + // Modify and save again: |
| 47 | + gotModel.favorites = listOf("XTC", "Elvis Costello") |
| 48 | + Assert.assertTrue(testCollection.save(gotModel)) |
| 49 | + Assert.assertNotEquals(model.documentMeta?.revisionID, gotModel.documentMeta?.revisionID) |
| 50 | + |
| 51 | + // Get it as a regular Document and verify the contents: |
| 52 | + val doc = testCollection.getDocument(id)!! |
| 53 | + Assert.assertEquals(gotModel.documentMeta?.revisionID, doc.revisionID) |
| 54 | + Assert.assertEquals("Nigel", doc.getString("name")) |
| 55 | + Assert.assertEquals(12, doc.getInt("age")) |
| 56 | + val faves = doc.getArray("favorites")!! |
| 57 | + Assert.assertEquals(2, faves.count()) |
| 58 | + Assert.assertEquals("XTC", faves.getString(0)) |
| 59 | + Assert.assertEquals("Elvis Costello", faves.getString(1)) |
| 60 | + |
| 61 | + // Delete the model. `model` is out of date, so it will fail, but `gotModel` is OK: |
| 62 | + Assert.assertFalse(testCollection.delete(model, ConcurrencyControl.FAIL_ON_CONFLICT)) |
| 63 | + Assert.assertTrue(testCollection.delete(gotModel, ConcurrencyControl.FAIL_ON_CONFLICT)) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +// Simple Model class for tests |
| 69 | +@Serializable |
| 70 | +data class TestModel(var name: String, |
| 71 | + var age: Int, |
| 72 | + var favorites: List<String>? = null): DocumentModel { |
| 73 | + @Transient override var documentMeta: DocumentMeta? = null |
| 74 | +} |
0 commit comments