Merge pull request #14 from ebivibe/DBBranch

Db branch
This commit is contained in:
FunwithaPorpoise 2018-10-26 22:25:34 -04:00 committed by GitHub
commit 9291e5cc71
3 changed files with 271 additions and 31 deletions

View file

@ -43,8 +43,11 @@ public class DBHelper extends SQLiteOpenHelper {
// private static final String TABLE_SERVICEPROVIDERS = "serviceProviders"; // private static final String TABLE_SERVICEPROVIDERS = "serviceProviders";
//name of table containing services and rates
// private static final String TABLE_SERVICES = "services"; private static final String TABLE_SERVICES = "services";
//columns of TABLE_LOGIN
private static final String COLUMN_SERVICE = "service";
private static final String COLUMN_RATE = "rate";
/** /**
@ -64,18 +67,25 @@ public class DBHelper extends SQLiteOpenHelper {
public void onCreate(SQLiteDatabase db){ public void onCreate(SQLiteDatabase db){
String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "(" String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "("
+ COLUMN_USERNAME + " STRING UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK," + COLUMN_USERNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_PASSWORD + " STRING NOT NULL," + COLUMN_PASSWORD + " TEXT NOT NULL,"
+ COLUMN_FIRSTNAME + " STRING DEFAULT 'FirstName'," + COLUMN_FIRSTNAME + " TEXT DEFAULT 'FirstName',"
+ COLUMN_LASTNAME + " STRING DEFAULT 'LastName'," + COLUMN_LASTNAME + " TEXT DEFAULT 'LastName',"
+ COLUMN_USERTYPE + " STRING NOT NULL" + ")"; + COLUMN_USERTYPE + " TEXT NOT NULL" + ")";
db.execSQL(CREATE_LOGIN_TABLE); db.execSQL(CREATE_LOGIN_TABLE);
String CREATE_SERVICES_TABLE = "CREATE TABLE "+ TABLE_SERVICES + "("
+ COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_RATE + " REAL DEFAULT 0.0" + ")";
db.execSQL(CREATE_SERVICES_TABLE);
} }
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SERVICES);
onCreate(db); onCreate(db);
} }
@ -85,6 +95,8 @@ public class DBHelper extends SQLiteOpenHelper {
} }
//methods for table of users
/** /**
* Adds a user to the database. Returns false if there is a user already * Adds a user to the database. Returns false if there is a user already
* existing in the database with the same username. Returns true if * existing in the database with the same username. Returns true if
@ -188,13 +200,144 @@ public class DBHelper extends SQLiteOpenHelper {
new String[]{username}) > 0; new String[]{username}) > 0;
} }
/**
* Returns a list of String arrays containing the username, first name,
* last name, and user type of every user in TABLE_LOGIN.
*
* @return list of arrays of [username, first name, last name, user type]
*/
public List<String[]> getAllUsers(){
return getAll("SELECT " + COLUMN_USERNAME + ", "
+ COLUMN_FIRSTNAME + ", "
+ COLUMN_LASTNAME + ", "
+ COLUMN_USERTYPE
+ " FROM "+TABLE_LOGIN);
}
//methods for table of services
/**
* Adds a service to the database. Returns false if service already
* exists in the database.
* Returns true if successful in adding service to database.
*
* @param service service to be added
* @return whether adding service was successful
*/
public boolean addService(Service service){
//Check for duplicate username by querying login table
Cursor cursor = writeDB.query(TABLE_SERVICES,
new String[] {COLUMN_SERVICE},
COLUMN_SERVICE + " = ?",
new String[]{service.getName()},
null, null, null,
"1");
//If cursor has 1+ elements in it, username already exists in table
if (cursor != null && cursor.getCount() > 0){
cursor.close();
return false;
}
cursor.close();
ContentValues values = new ContentValues();
values.put(COLUMN_SERVICE, service.getName());
values.put(COLUMN_RATE, service.getRate());
writeDB.insert(TABLE_SERVICES, null, values);
return true;
}
/**
* Looks in database for service with specified, and returns an
* object of Service if found.
* Returns null if no such service found.
*
* @param serviceName service to look up
* @return object representing service found
*/
public Service findService(String serviceName){
Service service;
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_SERVICES
+ " WHERE " + COLUMN_SERVICE + " = ?",
new String[]{serviceName});
if (cursor.moveToFirst()){
String servName = cursor.getString(0);
double rate = cursor.getDouble(1);
service = new Service(servName, rate);
} else {
service = null;
}
cursor.close();
return service;
}
/**
* Updates service rate using a Service object.
* Returns true if a service was found and entry updated.
* Returns false if no service was found.
*
*
* @param service service object containing updated values
*
* @return whether updating service information was successful
*/
public boolean updateService(Service service){
ContentValues values = new ContentValues();
values.put(COLUMN_RATE, service.getRate());
return writeDB.update(TABLE_SERVICES, values, COLUMN_SERVICE+" = ?",
new String[]{service.getName()}) > 0;
}
/**
* Updates service rate using input of service name and rate.
* Returns true if a service was found and entry updated.
* Returns false if no service was found.
*
*
* @param name name of service
* @param rate rate of service
*
* @return whether updating service information was successful
*/
public boolean updateService(String name, double rate){
ContentValues values = new ContentValues();
if (rate > 0)
values.put(COLUMN_RATE, rate);
return writeDB.update(TABLE_SERVICES, values, COLUMN_SERVICE+" = ?",
new String[]{name}) > 0;
}
/**
* Looks in database for a service, and deletes the corresponding
* entry. Returns true if a user was deleted, false otherwise.
*
* @param service service of entry to delete
* @return whether the service was deleted
*/
public boolean deleteService(String service) {
return writeDB.delete(TABLE_SERVICES, COLUMN_SERVICE+" = ?",
new String[]{service}) > 0;
}
/**
* Returns a list of String arrays containing the service categories,
* names and hourly rates.
*
* @return list of arrays of [service, rate]
*/
public List<String[]> getAllServices(){
return getAll("SELECT * FROM " + TABLE_SERVICES);
}
/** /**
* Prints all entries of table. One row is printed per line. Columns are * Prints all entries of table. One row is printed per line. Columns are
* separated by spaces. * separated by spaces.
* *
* @param tableName name of table to print * @param tableName name of table to print
*/ */
public void printTable(String tableName){ void printTable(String tableName){
Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null); Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null);
cursor.moveToFirst(); cursor.moveToFirst();
for (int i = 0; i<cursor.getCount(); i++){ for (int i = 0; i<cursor.getCount(); i++){
@ -208,32 +351,24 @@ public class DBHelper extends SQLiteOpenHelper {
cursor.close(); cursor.close();
} }
/**
* Returns a list of String arrays containing the username, first name, private List<String[]> getAll(String rawQuery){
* last name, and user type of every user in TABLE_LOGIN. List<String[]> list = new LinkedList<>();
* String[] infoArray;
* @return list of arrays of [username, first name, last name, user type] Cursor cursor = readDB.rawQuery(rawQuery,null);
*/
public List<String[]> getAllUsers(){
List<String[]> listOfUsers = new LinkedList<>();
String[] user;
Cursor cursor = readDB.rawQuery("SELECT " + COLUMN_USERNAME + ", "
+ COLUMN_FIRSTNAME + ", "
+ COLUMN_LASTNAME + ", "
+ COLUMN_USERTYPE
+ " FROM "+TABLE_LOGIN, null);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) { for (int i = 0; i < cursor.getCount(); i++) {
user = new String[cursor.getColumnNames().length]; infoArray = new String[cursor.getColumnNames().length];
for (int j = 0; j < cursor.getColumnNames().length; j++) { for (int j = 0; j < cursor.getColumnNames().length; j++) {
user[j] = cursor.getString(j); infoArray[j] = cursor.getString(j);
} }
listOfUsers.add(user); list.add(infoArray);
cursor.moveToNext(); cursor.moveToNext();
} }
} }
cursor.close(); cursor.close();
return listOfUsers; return list;
} }
} }

View file

@ -1,4 +1,27 @@
package com.uottawa.olympus.olympusservices; package com.uottawa.olympus.olympusservices;
public class Service { public class Service {
private String name;
private double rate;
Service(String name, double rate) {
this.name = name;
this.rate = rate;
}
public String getName() {
return name;
}
public double getRate() {
return rate;
}
public void setName(String name) {
this.name = name;
}
public void setRate(double rate) {
this.rate = rate;
}
} }

View file

@ -20,7 +20,7 @@ import static org.junit.Assert.*;
public class DBIntegrationTest { public class DBIntegrationTest {
private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application); private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application);
//testing user login table
@Test @Test
public void testAdminExists(){ public void testAdminExists(){
UserType dbUser = dbHelper.findUserByUsername("admin"); UserType dbUser = dbHelper.findUserByUsername("admin");
@ -115,10 +115,10 @@ public class DBIntegrationTest {
List<String[]> allUsers = dbHelper.getAllUsers(); List<String[]> allUsers = dbHelper.getAllUsers();
for (String[] user : allUsers){ for (String[] user : allUsers){
// for (String s : user){ /* for (String s : user){
// System.out.print(s + " "); System.out.print(s + " ");
// } }
// System.out.println(); System.out.println();*/
UserType usertype = dbHelper.findUserByUsername(user[0]); UserType usertype = dbHelper.findUserByUsername(user[0]);
assertEquals(usertype.getFirstname(), user[1]); assertEquals(usertype.getFirstname(), user[1]);
assertEquals(usertype.getLastname(), user[2]); assertEquals(usertype.getLastname(), user[2]);
@ -128,5 +128,87 @@ public class DBIntegrationTest {
dbHelper.deleteUser("jbO4aBF4dC"); dbHelper.deleteUser("jbO4aBF4dC");
} }
//Testing services table
@Test
public void testAddAndDeleteServices(){
Service originalService, dbService;
boolean deleted, addedOne, addedTwo;
originalService = new Service("Exterminating flatworms", 20.00);
addedOne = dbHelper.addService(originalService);
dbService = dbHelper.findService("Exterminating flatworms");
assertEquals("Exterminating flatworms", dbService.getName());
assertEquals(20.00, dbService.getRate(), 0.001);
originalService = new Service("Cleaning fishtanks", 15.00);
addedTwo = dbHelper.addService(originalService);
dbService = dbHelper.findService("Cleaning fishtanks");
assertEquals("Cleaning fishtanks", dbService.getName());
assertEquals(15.00, dbService.getRate(), 0.001);
if (addedOne) {
deleted = dbHelper.deleteService("Exterminating flatworms");
assertTrue(deleted);
}
if (addedTwo) {
deleted = dbHelper.deleteService("Cleaning fishtanks");
assertTrue(deleted);
}
}
@Test
public void testAddDuplicateService(){
boolean added;
added = dbHelper.addService(new Service("Exterminating flatworms", 20.00));
assertTrue(added);
added = dbHelper.addService(new Service("Exterminating flatworms", 25.00));
assertTrue(!added);
dbHelper.deleteService("Exterminating flatworms");
}
@Test
public void testUpdateService(){
boolean updated;
Service service;
dbHelper.addService(new Service("Exterminating flatworms", 20.00));
updated = dbHelper.updateService("Exterminating flatworms", 25.00);
assertTrue(updated);
service = dbHelper.findService("Exterminating flatworms");
assertEquals("Exterminating flatworms", service.getName());
assertEquals(25.00, service.getRate(), 0.001);
dbHelper.deleteService("Exterminating flatworms");
}
@Test
public void testGetAllServices(){
dbHelper.addService(new Service("Exterminating flatworms", 20.00));
List<String[]> allServices = dbHelper.getAllServices();
for (String[] service : allServices){
/* for (String s : user){
System.out.print(s + " ");
}
System.out.println();*/
Service dbService = dbHelper.findService(service[0]);
assertEquals(dbService.getRate(), Double.parseDouble(service[1]), 0.001);
}
dbHelper.deleteService("Exterminating flatworms");
}
} }