Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Teester committed Apr 15, 2018
1 parent e3dbcd6 commit 4755dfd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void checkLocationPermission() {
*/
@Override
public void run() {
SourceContract.Overpass overpassQuery = new QueryOverpass(context);
SourceContract.Overpass overpassQuery = new QueryOverpass(context, preferences);
overpassQuery.queryOverpass(location.getLatitude(), location.getLongitude(), location.getAccuracy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ public class QueryOverpass implements SourceContract.Overpass {
private double queryLongitude;
private List<OsmObject> poiList = new ArrayList<>();
private Context context;
private SourceContract.Preferences preferences;

public QueryOverpass(Context context) {
public QueryOverpass(Context context, SourceContract.Preferences preferences) {
this.context = context;
this.preferences = preferences;
}

QueryOverpass() {
QueryOverpass(SourceContract.Preferences preferences) {
this.preferences = preferences;
}

/**
Expand Down Expand Up @@ -169,7 +172,6 @@ public void processResult(String result) {
*/
@Override
public void queryOverpass(double latitude, double longitude, float accuracy) {
SourceContract.Preferences preferences = new Preferences(context);
preferences.setLongPreference(PreferenceList.LAST_QUERY_TIME, System.currentTimeMillis());
this.queryLatitude = latitude;
this.queryLongitude = longitude;
Expand All @@ -190,6 +192,7 @@ public void queryOverpass(double latitude, double longitude, float accuracy) {
* @return - the location type
* @throws JSONException - if it's not an amenity, shop, tourism or leisure
*/
@Override
public String getType(JSONObject tags) throws JSONException {
String type = "";
if (tags.has("amenity")) {
Expand All @@ -213,10 +216,10 @@ public String getType(JSONObject tags) throws JSONException {
* @param currentLocation the current location
* @return true if we've been at a location near here in the last week
*/
private boolean checkDatabaseForLocation(double latitude, double longitude) {
@Override
public boolean checkDatabaseForLocation(double latitude, double longitude) {
final long time = System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000);

SourceContract.Preferences preferences = new Preferences(context);
if (BuildConfig.DEBUG && preferences.getBooleanPreference(PreferenceList.DEBUG_MODE)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.teester.whatsnearby.data.source;

import org.json.JSONException;
import org.json.JSONObject;

public interface SourceContract {

interface Preferences {
Expand Down Expand Up @@ -33,6 +36,11 @@ interface Overpass {
void processResult(String result);

void queryOverpass(double latitude, double longitude, float accuracy);


String getType(JSONObject tags) throws JSONException;

boolean checkDatabaseForLocation(double latitude, double longitude);
}

interface upload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static junit.framework.TestCase.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class QueryOverpassTest {

private QueryOverpass queryOverpass;
private SourceContract.Overpass queryOverpass;

@Mock
private SourceContract.Preferences preferences;

@Before
public void setUp() {
queryOverpass = new QueryOverpass();
queryOverpass = new QueryOverpass(preferences);
}

@Test
Expand All @@ -36,4 +45,15 @@ public void testGetOverpassResultPoiType() throws JSONException {
assertEquals(expected_result, result);
}

@Test
public void testDatabaseLookup() {
double latitude = 53;
double longitude = -7;

when(preferences.getBooleanPreference(anyString())).thenReturn(true);

boolean result = queryOverpass.checkDatabaseForLocation(latitude, longitude);
assertEquals(false, result);
}

}

0 comments on commit 4755dfd

Please sign in to comment.