Add Admin first, always, and fixed getAllUsers
This commit is contained in:
parent
846df76cdb
commit
6cbf771b82
2 changed files with 49 additions and 15 deletions
|
@ -57,6 +57,7 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
super(context, DB_NAME, null, DB_VERSION);
|
super(context, DB_NAME, null, DB_VERSION);
|
||||||
readDB = this.getReadableDatabase();
|
readDB = this.getReadableDatabase();
|
||||||
writeDB = this.getWritableDatabase();
|
writeDB = this.getWritableDatabase();
|
||||||
|
addUser(new Admin());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -70,7 +71,6 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
+ COLUMN_USERTYPE + " STRING NOT NULL" + ")";
|
+ COLUMN_USERTYPE + " STRING NOT NULL" + ")";
|
||||||
|
|
||||||
db.execSQL(CREATE_LOGIN_TABLE);
|
db.execSQL(CREATE_LOGIN_TABLE);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -96,14 +96,16 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
public boolean addUser(UserType userType){
|
public boolean addUser(UserType userType){
|
||||||
//Check for duplicate username by querying login table
|
//Check for duplicate username by querying login table
|
||||||
Cursor cursor = writeDB.query(TABLE_LOGIN,
|
Cursor cursor = writeDB.query(TABLE_LOGIN,
|
||||||
new String[] {COLUMN_USERNAME},
|
new String[] {COLUMN_USERNAME},
|
||||||
COLUMN_USERNAME + " = ?",
|
COLUMN_USERNAME + " = ?",
|
||||||
new String[]{userType.getUsername()},
|
new String[]{userType.getUsername()},
|
||||||
null, null, null,
|
null, null, null,
|
||||||
"1");
|
"1");
|
||||||
//If cursor has 1+ elements in it, username already exists in table
|
//If cursor has 1+ elements in it, username already exists in table
|
||||||
if (cursor != null && cursor.getCount() > 0)
|
if (cursor != null && cursor.getCount() > 0){
|
||||||
|
cursor.close();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
|
@ -127,8 +129,8 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
public UserType findUserByUsername(String username){
|
public UserType findUserByUsername(String username){
|
||||||
UserType usertype;
|
UserType usertype;
|
||||||
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN
|
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN
|
||||||
+ " WHERE " + COLUMN_USERNAME + " = ?",
|
+ " WHERE " + COLUMN_USERNAME + " = ?",
|
||||||
new String[]{username});
|
new String[]{username});
|
||||||
|
|
||||||
if (cursor.moveToFirst()){
|
if (cursor.moveToFirst()){
|
||||||
String password = cursor.getString(1);
|
String password = cursor.getString(1);
|
||||||
|
@ -215,12 +217,15 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
public List<String[]> getAllUsers(){
|
public List<String[]> getAllUsers(){
|
||||||
List<String[]> listOfUsers = new LinkedList<>();
|
List<String[]> listOfUsers = new LinkedList<>();
|
||||||
String[] user;
|
String[] user;
|
||||||
Cursor cursor = readDB.rawQuery("SELECT ? , ?, ?, ? FROM "+TABLE_LOGIN,
|
Cursor cursor = readDB.rawQuery("SELECT " + COLUMN_USERNAME + ", "
|
||||||
new String[]{COLUMN_USERNAME, COLUMN_FIRSTNAME,
|
+ COLUMN_FIRSTNAME + ", "
|
||||||
COLUMN_LASTNAME, COLUMN_USERTYPE});
|
+ 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[4];
|
user = 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);
|
user[j] = cursor.getString(j);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,13 +11,26 @@ import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(packageName = "com.uottawa.olympus.olympusservices")
|
@Config(packageName = "com.uottawa.olympus.olympusservices")
|
||||||
public class DBUnitTest {
|
public class DBIntegrationTest {
|
||||||
private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application);
|
private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application);
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdminExists(){
|
||||||
|
UserType dbUser = dbHelper.findUserByUsername("admin");
|
||||||
|
assertEquals("Admin", dbUser.getClass().getSimpleName());
|
||||||
|
assertEquals("admin", dbUser.getUsername());
|
||||||
|
assertEquals("admin", dbUser.getPassword());
|
||||||
|
assertEquals("Admin", dbUser.getFirstname());
|
||||||
|
assertEquals("Admin", dbUser.getLastname());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddAndDeleteUser(){
|
public void testAddAndDeleteUser(){
|
||||||
UserType originalUser, dbUser;
|
UserType originalUser, dbUser;
|
||||||
|
@ -95,4 +108,20 @@ public class DBUnitTest {
|
||||||
dbHelper.deleteUser("jbO4aBF4dC");
|
dbHelper.deleteUser("jbO4aBF4dC");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllUsers(){
|
||||||
|
dbHelper.addUser(new User("jbO4aBF4dC", "soccer", "Miguel", "Garzon"));
|
||||||
|
|
||||||
|
List<String[]> allUsers = dbHelper.getAllUsers();
|
||||||
|
|
||||||
|
for (String[] user : allUsers){
|
||||||
|
UserType usertype = dbHelper.findUserByUsername(user[0]);
|
||||||
|
assertEquals(usertype.getFirstname(), user[1]);
|
||||||
|
assertEquals(usertype.getLastname(), user[2]);
|
||||||
|
assertEquals(usertype.getClass().getSimpleName(), user[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
dbHelper.deleteUser("jbO4aBF4dC");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue