Skip to content

Instantly share code, notes, and snippets.

@simplexityx
Created May 19, 2019 17:36
Show Gist options
  • Save simplexityx/dab6e478e9eb9bb017c814843736d0e0 to your computer and use it in GitHub Desktop.
Save simplexityx/dab6e478e9eb9bb017c814843736d0e0 to your computer and use it in GitHub Desktop.
package com.example.edmonproject;
/*
Basal
Bolus
FingerStick
Sleep
HeartRate
Exercise
Airtemp
SkingTemp
gsr
work
illness
*/
import android.util.Log;
import android.util.Pair;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class UnitTests {
private Random r;
SimpleDateFormat sdformat;
private int hrNum;
private String hrTime;
public void run() {
r = new Random();
String myFormat = "MMM dd, yyyy hh:mm:ss"; //In which you need put here
sdformat = new SimpleDateFormat(myFormat);
heartRatePush();
return;
}
private void heartRatePush(){
//1. setup data
//setup list of data and push to database
hrNum = r.nextInt(100);
Log.e("random num to be inserted into database", Integer.toString(hrNum));
hrTime = sdformat.format(Calendar.getInstance().getTime());
Map<String, Object> testHr = new HashMap<>();
testHr.put("hr", hrNum);
testHr.put("time", new Timestamp(new Date()));
Util.PostRecordingToFirebase("heartRate", testHr);
//2. perform operation on data
//get data from database
Util.GetDefinedRecordingFromFireBase("heartRate", new testListener("time", "hr"), "time" , "D", 1);
}
private void heartRateTest(Pair<String,Integer> resultFromDataBase){
try{
if(resultFromDataBase.first != hrTime && resultFromDataBase.second != 518571){
throw new AssertionError("wrong heart rate data gotten from database");
}
}catch(AssertionError e){
Log.e("heart rate test failed", e.toString());
}
}
private void callBackFunc(String date, String number){
heartRateTest(new Pair<>(date,Integer.parseInt(number)));
}
public class testListener implements onDataListener {
private String arg1, arg2;
public testListener(String a1, String a2){
arg1 = a1;
arg2 = a2;
}
@Override
public void onSuccess(QuerySnapshot doc, String dType) {
for (QueryDocumentSnapshot document : doc) {
callBackFunc(document.getData().get(arg1).toString(),document.getData().get(arg2).toString());
}
}
@Override
public void onFailure() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment