1/*
2 * Copyright (C) 2017 The Android Open Source Project
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
17package androidx.room.integration.kotlintestapp.test
18
19import android.support.test.filters.SmallTest
20import androidx.room.EmptyResultSetException
21import androidx.room.integration.kotlintestapp.vo.BookWithPublisher
22import org.junit.Test
23
24@SmallTest
25class RxJava2QueryTest : TestDatabaseTest() {
26
27    @Test
28    fun observeBooksById() {
29        booksDao.addAuthors(TestUtil.AUTHOR_1)
30        booksDao.addPublishers(TestUtil.PUBLISHER)
31        booksDao.addBooks(TestUtil.BOOK_1)
32
33        booksDao.getBookFlowable(TestUtil.BOOK_1.bookId)
34                .test()
35                .assertValue { book -> book == TestUtil.BOOK_1 }
36    }
37
38    @Test
39    fun observeBooksByIdSingle() {
40        booksDao.addAuthors(TestUtil.AUTHOR_1)
41        booksDao.addPublishers(TestUtil.PUBLISHER)
42        booksDao.addBooks(TestUtil.BOOK_1)
43
44        booksDao.getBookSingle(TestUtil.BOOK_1.bookId)
45                .test()
46                .assertComplete()
47                .assertValue { book -> book == TestUtil.BOOK_1 }
48    }
49
50    @Test
51    fun observeBooksByIdSingle_noBook() {
52        booksDao.getBookSingle("x")
53                .test()
54                .assertError(EmptyResultSetException::class.java)
55    }
56
57    @Test
58    fun observeBooksByIdMaybe() {
59        booksDao.addAuthors(TestUtil.AUTHOR_1)
60        booksDao.addPublishers(TestUtil.PUBLISHER)
61        booksDao.addBooks(TestUtil.BOOK_1)
62
63        booksDao.getBookMaybe(TestUtil.BOOK_1.bookId)
64                .test()
65                .assertComplete()
66                .assertValue { book -> book == TestUtil.BOOK_1 }
67    }
68
69    @Test
70    fun observeBooksByIdMaybe_noBook() {
71        booksDao.getBookMaybe("x")
72                .test()
73                .assertComplete()
74                .assertNoErrors()
75                .assertNoValues()
76    }
77
78    @Test
79    fun observeBooksWithPublisher() {
80        booksDao.addAuthors(TestUtil.AUTHOR_1)
81        booksDao.addPublishers(TestUtil.PUBLISHER)
82        booksDao.addBooks(TestUtil.BOOK_1)
83
84        var expected = BookWithPublisher(TestUtil.BOOK_1.bookId, TestUtil.BOOK_1.title,
85                TestUtil.PUBLISHER)
86        var expectedList = ArrayList<BookWithPublisher>()
87        expectedList.add(expected)
88
89        booksDao.getBooksWithPublisherFlowable()
90                .test()
91                .assertValue(expectedList)
92    }
93
94    @Test
95    fun publisherWithBooks() {
96        booksDao.addAuthors(TestUtil.AUTHOR_1)
97        booksDao.addPublishers(TestUtil.PUBLISHER)
98        booksDao.addBooks(TestUtil.BOOK_1, TestUtil.BOOK_2)
99
100        booksDao.getPublisherWithBooksFlowable(TestUtil.PUBLISHER.publisherId)
101                .test()
102                .assertValue {
103                    it.publisher == TestUtil.PUBLISHER
104                            && it.books?.size == 2
105                            && it.books?.get(0) == TestUtil.BOOK_1
106                            && it.books?.get(1) == TestUtil.BOOK_2
107                }
108    }
109}
110