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.testapp.test;
18
19import static org.hamcrest.CoreMatchers.is;
20import static org.hamcrest.MatcherAssert.assertThat;
21
22import android.content.Context;
23import android.support.test.InstrumentationRegistry;
24import android.support.test.filters.LargeTest;
25import android.support.test.runner.AndroidJUnit4;
26
27import androidx.room.Room;
28import androidx.room.integration.testapp.TestDatabase;
29import androidx.room.integration.testapp.dao.UserDao;
30import androidx.room.integration.testapp.vo.AvgWeightByAge;
31import androidx.room.integration.testapp.vo.User;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37import java.util.Arrays;
38
39@LargeTest
40@RunWith(AndroidJUnit4.class)
41public class PojoTest {
42    private UserDao mUserDao;
43
44    @Before
45    public void createDb() {
46        Context context = InstrumentationRegistry.getTargetContext();
47        TestDatabase db = Room.inMemoryDatabaseBuilder(context, TestDatabase.class).build();
48        mUserDao = db.getUserDao();
49    }
50
51    @Test
52    public void weightsByAge() {
53        User[] users = TestUtil.createUsersArray(3, 5, 7, 10);
54        users[0].setAge(10);
55        users[0].setWeight(20);
56
57        users[1].setAge(10);
58        users[1].setWeight(30);
59
60        users[2].setAge(15);
61        users[2].setWeight(12);
62
63        users[3].setAge(35);
64        users[3].setWeight(55);
65
66        mUserDao.insertAll(users);
67        assertThat(mUserDao.weightByAge(), is(
68                Arrays.asList(
69                        new AvgWeightByAge(35, 55),
70                        new AvgWeightByAge(10, 25),
71                        new AvgWeightByAge(15, 12)
72                )
73        ));
74    }
75}
76