2016-08-24 22:54:32 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.database;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.GregorianCalendar;
|
2016-08-27 00:23:41 +02:00
|
|
|
import java.util.List;
|
2016-08-24 22:54:32 +02:00
|
|
|
|
2016-08-27 00:23:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.ActivityDescription;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.ActivityDescriptionDao;
|
2016-08-25 00:00:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
|
2016-08-27 00:23:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Tag;
|
2016-08-24 22:54:32 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributesDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.UserDao;
|
2016-08-25 00:00:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-08-24 22:54:32 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2016-08-25 00:00:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2016-09-04 22:10:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
2016-08-24 22:54:32 +02:00
|
|
|
|
2016-08-27 00:23:41 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2016-08-24 22:54:32 +02:00
|
|
|
|
2016-09-04 22:10:35 +02:00
|
|
|
public class EntitiesTest extends TestBase {
|
2016-08-24 22:54:32 +02:00
|
|
|
|
|
|
|
private UserDao userDao;
|
|
|
|
private UserAttributesDao userAttributesDao;
|
2016-09-04 22:10:35 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
super.setUp();
|
2016-08-24 22:54:32 +02:00
|
|
|
userDao = daoSession.getUserDao();
|
|
|
|
userAttributesDao = daoSession.getUserAttributesDao();
|
|
|
|
}
|
|
|
|
|
2016-09-04 22:10:35 +02:00
|
|
|
@Override
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
super.tearDown();
|
2016-08-25 00:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-24 22:54:32 +02:00
|
|
|
@Test
|
|
|
|
public void testUser() {
|
|
|
|
User user = new User();
|
|
|
|
user.setName("Peter");
|
|
|
|
user.setGender(ActivityUser.GENDER_FEMALE);
|
|
|
|
Calendar cal = GregorianCalendar.getInstance();
|
|
|
|
cal.add(Calendar.YEAR, -20);
|
|
|
|
user.setBirthday(cal.getTime());
|
|
|
|
UserAttributes attributes = new UserAttributes();
|
|
|
|
attributes.setWeightKG(55);
|
|
|
|
attributes.setHeightCM(170);
|
|
|
|
attributes.setSleepGoalHPD(8);
|
|
|
|
attributes.setStepsGoalSPD(10000);
|
|
|
|
|
2016-08-27 23:12:48 +02:00
|
|
|
daoSession.getUserDao().insert(user);
|
2016-08-24 22:54:32 +02:00
|
|
|
assertNotNull(user.getId());
|
|
|
|
|
|
|
|
attributes.setUserId(user.getId());
|
2016-08-27 23:12:48 +02:00
|
|
|
daoSession.getUserAttributesDao().insert(attributes);
|
2016-08-24 22:54:32 +02:00
|
|
|
user.getUserAttributesList().add(attributes);
|
|
|
|
|
|
|
|
assertNotNull(userDao.load(user.getId()));
|
|
|
|
assertEquals(1, userDao.count());
|
|
|
|
assertEquals(1, daoSession.loadAll(User.class).size());
|
|
|
|
|
|
|
|
assertNotNull(userAttributesDao.load(attributes.getId()));
|
|
|
|
assertEquals(1, userAttributesDao.count());
|
|
|
|
assertEquals(1, daoSession.loadAll(UserAttributes.class).size());
|
|
|
|
|
2016-08-27 23:12:48 +02:00
|
|
|
daoSession.getUserDao().update(user);
|
2016-08-24 22:54:32 +02:00
|
|
|
daoSession.delete(user);
|
|
|
|
daoSession.delete(attributes);
|
|
|
|
daoSession.delete(attributes);
|
|
|
|
assertNull(userDao.load(user.getId()));
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:00:53 +02:00
|
|
|
@Test
|
|
|
|
public void testDBHelper() {
|
2016-09-02 00:26:31 +02:00
|
|
|
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:01");
|
2017-04-17 19:36:50 +02:00
|
|
|
dummyGBDevice.setState(GBDevice.State.INITIALIZED);
|
2016-08-25 00:00:53 +02:00
|
|
|
Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
|
|
|
|
assertNotNull(device);
|
2016-09-02 00:26:31 +02:00
|
|
|
assertEquals("00:00:00:00:01", device.getIdentifier());
|
2016-08-25 00:00:53 +02:00
|
|
|
assertEquals("Testie", device.getName());
|
2016-08-27 00:23:41 +02:00
|
|
|
assertEquals("4.0", device.getModel());
|
2016-08-25 00:00:53 +02:00
|
|
|
assertEquals(DeviceType.TEST.getKey(), device.getType());
|
|
|
|
DeviceAttributes attributes = device.getDeviceAttributesList().get(0);
|
|
|
|
assertNotNull(attributes);
|
|
|
|
assertEquals("1.2.3", attributes.getFirmwareVersion1());
|
|
|
|
}
|
2016-08-24 22:54:32 +02:00
|
|
|
|
2016-08-27 00:23:41 +02:00
|
|
|
@Test
|
|
|
|
public void testActivityDescription() {
|
|
|
|
User user = DBHelper.getUser(daoSession);
|
|
|
|
assertNotNull(user);
|
|
|
|
|
|
|
|
ActivityDescriptionDao descDao = daoSession.getActivityDescriptionDao();
|
|
|
|
assertEquals(0, descDao.count());
|
|
|
|
|
2016-08-28 00:22:34 +02:00
|
|
|
List<ActivityDescription> list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
2016-08-27 00:23:41 +02:00
|
|
|
assertTrue(list.isEmpty());
|
|
|
|
|
2016-08-28 00:22:34 +02:00
|
|
|
ActivityDescription desc = DBHelper.createActivityDescription(user, 10, 100, daoSession);
|
2016-08-27 00:23:41 +02:00
|
|
|
assertNotNull(desc);
|
|
|
|
assertEquals(user, desc.getUser());
|
2016-08-28 00:22:34 +02:00
|
|
|
assertEquals(10, desc.getTimestampFrom());
|
|
|
|
assertEquals(100, desc.getTimestampTo());
|
2016-08-27 00:23:41 +02:00
|
|
|
List<Tag> tagList = desc.getTagList();
|
|
|
|
assertEquals(0, tagList.size());
|
|
|
|
|
|
|
|
Tag t1 = DBHelper.getTag(user, "Table Tennis", daoSession);
|
|
|
|
assertNotNull(t1);
|
|
|
|
assertEquals("Table Tennis", t1.getName());
|
|
|
|
t1.setDescription("Table tennis training for Olympia");
|
|
|
|
tagList.add(t1);
|
|
|
|
|
2016-08-28 00:22:34 +02:00
|
|
|
list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
2016-08-27 00:23:41 +02:00
|
|
|
assertEquals(1, list.size());
|
|
|
|
ActivityDescription desc1 = list.get(0);
|
|
|
|
assertEquals(desc, desc1);
|
|
|
|
assertEquals(1, desc1.getTagList().size());
|
|
|
|
|
2016-08-28 00:22:34 +02:00
|
|
|
// check for partial range overlaps
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 20, 80, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 5, 120, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 20, 120, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 5, 80, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
// Now with a second, adjacent ActivityDescription
|
|
|
|
ActivityDescription desc2 = DBHelper.createActivityDescription(user, 101, 200, daoSession);
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 20, 80, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 5, 120, daoSession);
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 20, 120, daoSession);
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 5, 80, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
// Now with a third, partially overlapping ActivityDescription
|
|
|
|
ActivityDescription desc3 = DBHelper.createActivityDescription(user, 5, 15, daoSession);
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 10, 100, daoSession);
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 20, 80, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 5, 120, daoSession);
|
|
|
|
assertEquals(3, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 20, 120, daoSession);
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 5, 80, daoSession);
|
|
|
|
assertEquals(2, list.size());
|
2016-08-27 00:23:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 00:26:31 +02:00
|
|
|
@Test
|
|
|
|
public void testDeviceAttributes() throws Exception {
|
|
|
|
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:02");
|
|
|
|
dummyGBDevice.setFirmwareVersion("1.0");
|
2017-04-17 19:19:23 +02:00
|
|
|
dummyGBDevice.setState(GBDevice.State.INITIALIZED);
|
|
|
|
|
2016-09-02 00:26:31 +02:00
|
|
|
Device deviceOld = DBHelper.getDevice(dummyGBDevice, daoSession);
|
|
|
|
assertNotNull(deviceOld);
|
|
|
|
|
|
|
|
List<DeviceAttributes> attrListOld = deviceOld.getDeviceAttributesList();
|
|
|
|
assertEquals(1, attrListOld.size());
|
|
|
|
assertEquals("1.0", attrListOld.get(0).getFirmwareVersion1());
|
2016-09-03 21:16:32 +02:00
|
|
|
assertEquals("1.0", DBHelper.getDeviceAttributes(deviceOld).getFirmwareVersion1());
|
2016-09-02 00:26:31 +02:00
|
|
|
|
|
|
|
// 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());
|
2016-09-03 21:16:32 +02:00
|
|
|
|
|
|
|
assertEquals("2.0", DBHelper.getDeviceAttributes(deviceNew).getFirmwareVersion1());
|
2016-09-02 00:26:31 +02:00
|
|
|
}
|
2016-08-24 22:54:32 +02:00
|
|
|
}
|