Repository navigation
Expand file tree
/
Copy pathAccountDaoTest.java
More file actions
96 lines (78 loc) · 4.09 KB
/
Copy pathAccountDaoTest.java
File metadata and controls
96 lines (78 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.persistence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import androidx.annotation.NonNull;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.shared.model.Capabilities;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 36)
public class AccountDaoTest {
@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
@NonNull
private NotesDatabase db;
@Before
public void setupDB() {
db = Room
.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), NotesDatabase.class)
.allowMainThreadQueries()
.build();
}
@After
public void closeDb() {
db.close();
}
@Test
public void insertAccount() {
final long createdId = db.getAccountDao().insert(new Account("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", null, new Capabilities()));
final var createdAccount = db.getAccountDao().getAccountById(createdId);
assertEquals("https://äöüß.example.com", createdAccount.getUrl());
assertEquals("彼得", createdAccount.getUserName());
assertEquals("彼得@äöüß.example.com", createdAccount.getAccountName());
}
@Test
public void updateApiVersionFromNull() {
final var account = db.getAccountDao().getAccountById(db.getAccountDao().insert(new Account("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", null, new Capabilities())));
assertNull(account.getApiVersion());
assertEquals(0, db.getAccountDao().updateApiVersion(account.getId(), null));
assertEquals(1, db.getAccountDao().updateApiVersion(account.getId(), "[0.2]"));
assertEquals(0, db.getAccountDao().updateApiVersion(account.getId(), "[0.2]"));
}
@Test
public void updateApiVersionFromExisting() {
final var capabilities = new Capabilities();
capabilities.setApiVersion("[0.2]");
final var account = db.getAccountDao().getAccountById(db.getAccountDao().insert(new Account("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", null, capabilities)));
assertEquals("[0.2]", account.getApiVersion());
assertEquals(0, db.getAccountDao().updateApiVersion(account.getId(), "[0.2]"));
assertEquals(1, db.getAccountDao().updateApiVersion(account.getId(), "[0.2, 1.0]"));
assertEquals(1, db.getAccountDao().updateApiVersion(account.getId(), null));
}
@Test
public void updateDisplayName() {
final var account = db.getAccountDao().getAccountById(db.getAccountDao().insert(new Account("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", null, new Capabilities())));
assertEquals("Should read userName in favor of displayName if displayName is NULL", "彼得", account.getDisplayName());
db.getAccountDao().updateDisplayName(account.getId(), "");
assertEquals("Should properly update the displayName, even if it is blank", "", db.getAccountDao().getAccountById(account.getId()).getDisplayName());
db.getAccountDao().updateDisplayName(account.getId(), "Foo Bar");
assertEquals("Foo Bar", db.getAccountDao().getAccountById(account.getId()).getDisplayName());
db.getAccountDao().updateDisplayName(account.getId(), null);
assertEquals("Should read userName in favor of displayName if displayName is NULL", "彼得", db.getAccountDao().getAccountById(account.getId()).getDisplayName());
}
}