1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Unit-Test and fix *Attributes sort order

When adding e.g. new DeviceAttributes, we cannot simply add them to the
attributes list, because that list is supposed to be ordered. We could
insert it at the beginning, but that would be brittle to changes in the
GBDaoGenerator, so we simply re-fetch them from the db after adding
new attributes.
This commit is contained in:
cpfeiffer 2016-09-02 00:26:31 +02:00
parent 507338d034
commit 6f02f9e350
2 changed files with 32 additions and 6 deletions

View File

@ -251,7 +251,9 @@ public class DBHelper {
attributes.setUserId(user.getId());
session.getUserAttributesDao().insert(attributes);
userAttributes.add(attributes);
// sort order is important, so we re-fetch from the db
// userAttributes.add(attributes);
user.resetUserAttributesList();
}
private static void invalidateUserAttributes(UserAttributes userAttributes, Calendar now, DaoSession session) {
@ -423,7 +425,9 @@ public class DBHelper {
DeviceAttributesDao attributesDao = session.getDeviceAttributesDao();
attributesDao.insert(attributes);
deviceAttributes.add(attributes);
// sort order is important, so we re-fetch from the db
// deviceAttributes.add(attributes);
device.resetDeviceAttributesList();
}
private static void invalidateDeviceAttributes(DeviceAttributes deviceAttributes, Calendar now, DaoSession session) {

View File

@ -104,10 +104,10 @@ public class EntitiesTest {
@Test
public void testDBHelper() {
GBDevice dummyGBDevice = createDummyGDevice();
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:01");
Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
assertNotNull(device);
assertEquals("00:00:00:00:00", device.getIdentifier());
assertEquals("00:00:00:00:01", device.getIdentifier());
assertEquals("Testie", device.getName());
assertEquals("4.0", device.getModel());
assertEquals(DeviceType.TEST.getKey(), device.getType());
@ -116,8 +116,8 @@ public class EntitiesTest {
assertEquals("1.2.3", attributes.getFirmwareVersion1());
}
private GBDevice createDummyGDevice() {
GBDevice dummyGBDevice = new GBDevice("00:00:00:00:00", "Testie", DeviceType.TEST);
private GBDevice createDummyGDevice(String macAddress) {
GBDevice dummyGBDevice = new GBDevice(macAddress, "Testie", DeviceType.TEST);
dummyGBDevice.setFirmwareVersion("1.2.3");
dummyGBDevice.setModel("4.0");
return dummyGBDevice;
@ -204,4 +204,26 @@ public class EntitiesTest {
assertEquals(2, list.size());
}
@Test
public void testDeviceAttributes() throws Exception {
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:02");
dummyGBDevice.setFirmwareVersion("1.0");
Device deviceOld = DBHelper.getDevice(dummyGBDevice, daoSession);
assertNotNull(deviceOld);
List<DeviceAttributes> attrListOld = deviceOld.getDeviceAttributesList();
assertEquals(1, attrListOld.size());
assertEquals("1.0", attrListOld.get(0).getFirmwareVersion1());
// some time passes, firmware update occurs
Thread.sleep(2 * 1000);
dummyGBDevice.setFirmwareVersion("2.0");
Device deviceNew = DBHelper.getDevice(dummyGBDevice, daoSession);
assertNotNull(deviceNew);
List<DeviceAttributes> attrListNew = deviceNew.getDeviceAttributesList();
assertEquals(2, attrListNew.size());
assertEquals("2.0", attrListNew.get(0).getFirmwareVersion1());
assertEquals("1.0", attrListNew.get(1).getFirmwareVersion1());
}
}