2016-08-24 22:54:32 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.database;
|
|
|
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
2016-08-25 00:00:53 +02:00
|
|
|
import org.junit.After;
|
2016-08-24 22:54:32 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
import org.robolectric.RobolectricTestRunner;
|
|
|
|
import org.robolectric.RuntimeEnvironment;
|
|
|
|
import org.robolectric.annotation.Config;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
2016-08-25 00:00:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
2016-08-27 00:23:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.ActivityDescription;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.ActivityDescriptionDao;
|
2016-08-24 22:54:32 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
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-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
|
|
|
|
|
|
|
@RunWith(RobolectricTestRunner.class)
|
2016-08-26 23:36:54 +02:00
|
|
|
@Config(constants = BuildConfig.class, sdk = 19)
|
|
|
|
// need sdk 19 because "WITHOUT ROWID" is not supported in robolectric/sqlite4java
|
2016-08-24 22:54:32 +02:00
|
|
|
public class EntitiesTest {
|
|
|
|
|
|
|
|
private DaoSession daoSession;
|
|
|
|
private UserDao userDao;
|
|
|
|
private UserAttributesDao userAttributesDao;
|
2016-08-25 00:00:53 +02:00
|
|
|
private DBHandler dbHandler;
|
|
|
|
private GBApplication app = (GBApplication) RuntimeEnvironment.application;
|
2016-08-24 22:54:32 +02:00
|
|
|
|
|
|
|
@Before
|
2016-08-25 00:00:53 +02:00
|
|
|
public void setUp() throws GBException {
|
2016-08-27 21:40:46 +02:00
|
|
|
// doesn't work with Robolectric yet
|
2016-08-25 00:00:53 +02:00
|
|
|
// dbHandler = GBApplication.acquireDB();
|
|
|
|
// daoSession = dbHandler.getDaoSession();
|
|
|
|
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(app, null, null);
|
2016-08-24 22:54:32 +02:00
|
|
|
SQLiteDatabase db = openHelper.getWritableDatabase();
|
|
|
|
daoSession = new DaoMaster(db).newSession();
|
|
|
|
userDao = daoSession.getUserDao();
|
|
|
|
userAttributesDao = daoSession.getUserAttributesDao();
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:00:53 +02:00
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
// GBApplication.releaseDB();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-08-27 00:23:41 +02:00
|
|
|
GBDevice dummyGBDevice = createDummyGDevice();
|
2016-08-25 00:00:53 +02:00
|
|
|
Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
|
|
|
|
assertNotNull(device);
|
|
|
|
assertEquals("00:00:00:00:00", device.getIdentifier());
|
|
|
|
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
|
|
|
private GBDevice createDummyGDevice() {
|
|
|
|
GBDevice dummyGBDevice = new GBDevice("00:00:00:00:00", "Testie", DeviceType.TEST);
|
|
|
|
dummyGBDevice.setFirmwareVersion("1.2.3");
|
|
|
|
dummyGBDevice.setModel("4.0");
|
|
|
|
return dummyGBDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testActivityDescription() {
|
|
|
|
User user = DBHelper.getUser(daoSession);
|
|
|
|
assertNotNull(user);
|
|
|
|
|
|
|
|
ActivityDescriptionDao descDao = daoSession.getActivityDescriptionDao();
|
|
|
|
assertEquals(0, descDao.count());
|
|
|
|
|
|
|
|
List<ActivityDescription> list = DBHelper.findActivityDecriptions(user, 0, 10, daoSession);
|
|
|
|
assertTrue(list.isEmpty());
|
|
|
|
|
|
|
|
ActivityDescription desc = DBHelper.createActivityDescription(user, 0, 10, daoSession);
|
|
|
|
assertNotNull(desc);
|
|
|
|
assertEquals(user, desc.getUser());
|
|
|
|
assertEquals(0, desc.getTimestampFrom());
|
|
|
|
assertEquals(10, desc.getTimestampTo());
|
|
|
|
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);
|
|
|
|
|
|
|
|
list = DBHelper.findActivityDecriptions(user, 0, 10, daoSession);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
ActivityDescription desc1 = list.get(0);
|
|
|
|
assertEquals(desc, desc1);
|
|
|
|
assertEquals(1, desc1.getTagList().size());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-24 22:54:32 +02:00
|
|
|
}
|