Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #348 from mapzen/baldur/cleaning-up-data
Browse files Browse the repository at this point in the history
adds cleanup procedure to database
  • Loading branch information
ecgreb committed Nov 6, 2014
2 parents b89ab7d + 37588df commit bdd4af2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/mapzen/open/core/DataUploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static com.mapzen.open.util.DatabaseHelper.COLUMN_UPLOADED;
import static com.mapzen.open.util.DatabaseHelper.TABLE_GROUPS;
import static com.mapzen.open.util.DatabaseHelper.TABLE_LOCATIONS;
import static com.mapzen.open.util.DatabaseHelper.TABLE_ROUTES;
import static com.mapzen.open.util.DatabaseHelper.TABLE_ROUTE_GROUP;
import static javax.xml.transform.OutputKeys.ENCODING;
import static javax.xml.transform.OutputKeys.INDENT;
Expand Down Expand Up @@ -379,6 +380,19 @@ private void setGroupAsUploaded(String groupId) {
cv.put(DatabaseHelper.COLUMN_UPLOADED, 1);
app.getDb().update(TABLE_GROUPS, cv, COLUMN_TABLE_ID + " = ?",
new String[] { groupId });
Cursor cursor = app.getDb().query(TABLE_ROUTE_GROUP, new String[] { COLUMN_ROUTE_ID },
COLUMN_GROUP_ID + " = ?",
new String[] { groupId }, null, null, null);
while (cursor.moveToNext()) {
int routeIdIndex = cursor.getColumnIndex(COLUMN_ROUTE_ID);
String routeId = cursor.getString(routeIdIndex);
app.getDb().delete(TABLE_ROUTES, COLUMN_TABLE_ID + " = ?",
new String[] { routeId });
app.getDb().delete(TABLE_LOCATIONS, COLUMN_ROUTE_ID + " = ?",
new String[] { routeId });
}
app.getDb().delete(TABLE_GROUPS, COLUMN_TABLE_ID + " = ?",
new String[] { groupId });
}

private byte[] compressGPX(String gpxString) throws IOException {
Expand Down
86 changes: 71 additions & 15 deletions src/test/java/com/mapzen/open/core/DataUploadServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mapzen.open.support.MapzenTestRunner;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void onStartCommand_shouldNotAttemptToGenerateGPXWhenUploaded() throws Ex
@Test
public void onStartCommand_shouldAttemptToGenerateGPXforReadyRoute() throws Exception {
String expectedGroupId = "route-1";
makeGroupReady(expectedGroupId);
makeGroup(expectedGroupId, 1);
DataUploadService spy = spy(service);
spy.onStartCommand(null, 0, 0);
verify(spy).generateGpxXmlFor(expectedGroupId, "does not matter");
Expand All @@ -110,21 +111,22 @@ public void onStartCommand_shouldAttemptToGenerateGPXforReadyRoute() throws Exce
@Test
public void onStartCommand_shouldNotMarkUploaded() throws Exception {
String expectedGroupId = "route-1";
makeGroupReady(expectedGroupId);
makeGroup(expectedGroupId, 1);
service.onStartCommand(null, 0, 0);
Cursor cursor = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_UPLOADED },
COLUMN_TABLE_ID + " = ? AND " + COLUMN_UPLOADED + " = 1",
new String[] { expectedGroupId }, null, null, null);
assertThat(cursor).hasCount(0);
}

@Ignore
@Test
public void onStartCommand_shouldMarkUploaded() throws Exception {
Token token = new Token("stuff", "fun");
OAuthService mockService = mock(OAuthService.class);
((MapzenApplication) Robolectric.application).setOsmOauthService(mockService);
String expectedGroupId = "route-1";
makeGroupReady(expectedGroupId);
makeGroup(expectedGroupId, 1);
app.setAccessToken(token);
service.onStartCommand(null, 0, 0);
Cursor cursor = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_UPLOADED },
Expand All @@ -133,10 +135,59 @@ public void onStartCommand_shouldMarkUploaded() throws Exception {
assertThat(cursor).hasCount(1);
}

@Test
public void onStartCommand_shouldRemoveData() throws Exception {
Token token = new Token("stuff", "fun");
OAuthService mockService = mock(OAuthService.class);
((MapzenApplication) Robolectric.application).setOsmOauthService(mockService);
String readyGroupId = "ready";
String notReadyGroupId = "not-ready";
fillLocationsTable(readyGroupId, "ready", 10, true);
fillLocationsTable(notReadyGroupId, "not-ready", 10, false);
app.setAccessToken(token);
service.onStartCommand(null, 0, 0);
assertGroups(readyGroupId, notReadyGroupId);
assertRoutes("ready", "not-ready");
assertLocations("ready", "not-ready");
}

private void assertLocations(String readyRouteId, String notReadyRouteId) {
Cursor cursor = app.getDb().query(TABLE_LOCATIONS, new String[] { COLUMN_TABLE_ID },
COLUMN_ROUTE_ID + " = ?",
new String[] { readyRouteId }, null, null, null);
assertThat(cursor).hasCount(0);
Cursor cursor1 = app.getDb().query(TABLE_LOCATIONS, new String[] { COLUMN_TABLE_ID },
COLUMN_ROUTE_ID + " = ?",
new String[] { notReadyRouteId }, null, null, null);
assertThat(cursor1).hasCount(10);
}

private void assertRoutes(String readyRouteId, String notReadyRouteId) {
Cursor cursor = app.getDb().query(TABLE_ROUTES, new String[] { COLUMN_TABLE_ID },
COLUMN_TABLE_ID + " = ?",
new String[] { readyRouteId }, null, null, null);
assertThat(cursor).hasCount(0);
Cursor cursor1 = app.getDb().query(TABLE_ROUTES, new String[] { COLUMN_TABLE_ID },
COLUMN_TABLE_ID + " = ?",
new String[] { notReadyRouteId }, null, null, null);
assertThat(cursor1).hasCount(1);
}

private void assertGroups(String readyGroupId, String notReadyGroupId) {
Cursor cursor = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_TABLE_ID },
COLUMN_TABLE_ID + " = ?",
new String[] { readyGroupId }, null, null, null);
assertThat(cursor).hasCount(0);
Cursor cursor1 = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_TABLE_ID },
COLUMN_TABLE_ID + " = ?",
new String[] { notReadyGroupId }, null, null, null);
assertThat(cursor1).hasCount(1);
}

@Test
public void onStartCommand_shouldCreateButNotUploadXML() throws Exception {
String expectedGroupId = "route-1";
makeGroupReady(expectedGroupId);
makeGroup(expectedGroupId, 1);
DataUploadService spy = spy(service);
spy.onStartCommand(null, 0, 0);
verify(spy).generateGpxXmlFor(expectedGroupId, "does not matter");
Expand All @@ -149,7 +200,7 @@ public void shouldGenerateGPX_shouldSubmit() throws Exception {

String expectedGroupId = "test_route";
String expectedRouteDescription = "does not matter";
fillLocationsTable(expectedGroupId, 10);
fillLocationsTable(expectedGroupId, 10, true);
DataUploadService spy = spy(service);
spy.onStartCommand(null, 0, 0);
verify(spy).generateGpxXmlFor(expectedGroupId, expectedRouteDescription);
Expand All @@ -163,9 +214,9 @@ public void shouldGenerateGPX_shouldSubmit() throws Exception {
public void shouldHaveLocationsFromAllRoutesInGroup() throws Exception {
String groupId = "test-group-id";
String routeId = "test-route-id";
fillLocationsTable(groupId, routeId, 10);
fillLocationsTable(groupId, routeId, 10, true);
String anotherRoute = "second-route-id";
fillLocationsTable(groupId, anotherRoute, 10);
fillLocationsTable(groupId, anotherRoute, 10, true);

DOMSource domSource = service.getDocument(groupId);
Document document = domSource.getNode().getOwnerDocument();
Expand All @@ -184,7 +235,7 @@ public void shouldHaveSpeedElement() throws Exception {
String expectedSpeed = "40.0";
String groupId = "test-group-id";
String routeId = "test-route-id";
fillLocationsTable(groupId, routeId, 10);
fillLocationsTable(groupId, routeId, 10, true);
Location loc = getTestLocation(100, 200);
loc.setBearing(4.0f);
loc.setSpeed(Float.valueOf(expectedSpeed));
Expand Down Expand Up @@ -235,24 +286,29 @@ public void shouldNotUploadWhenLessThan50MetersTraveled() throws Exception {
verify(spy, never()).submitTrace(anyString(), anyString(), any(byte[].class));
}

private void makeGroupReady(String groupId) throws Exception {
private void makeGroup(String groupId, int ready) throws Exception {
ContentValues insertValues = new ContentValues();
insertValues.put(COLUMN_TABLE_ID, groupId);
insertValues.put(COLUMN_MSG, "does not matter");
insertValues.put(COLUMN_READY_FOR_UPLOAD, 1);
insertValues.put(COLUMN_READY_FOR_UPLOAD, ready);
long result = app.getDb().insert(TABLE_GROUPS, null, insertValues);
if (result < 0) {
throw new Exception("database insert failed");
}
}

private void fillLocationsTable(String groupId, double numPoints) throws Exception {
fillLocationsTable(groupId, "test-route-id", numPoints);
private void fillLocationsTable(String groupId, double numPoints, boolean ready)
throws Exception {
fillLocationsTable(groupId, "test-route-id", numPoints, ready);
}

private void fillLocationsTable(String groupId, String routeId, double numPoints)
private void fillLocationsTable(String groupId, String routeId, double numPoints, boolean ready)
throws Exception {
makeGroupReady(groupId);
if (ready) {
makeGroup(groupId, 1);
} else {
makeGroup(groupId, 0);
}

ContentValues routeValues = new ContentValues();
routeValues.put(COLUMN_TABLE_ID, routeId);
Expand All @@ -278,7 +334,7 @@ private void fillLocationsTable(String groupId, String routeId, double numPoints

private void fillLocationsTableAllSamePoint(String groupId, String routeId, double numPoints)
throws Exception {
makeGroupReady(groupId);
makeGroup(groupId, 1);

ContentValues routeValues = new ContentValues();
routeValues.put(COLUMN_TABLE_ID, routeId);
Expand Down

0 comments on commit bdd4af2

Please sign in to comment.