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);
|
||||
readDB = this.getReadableDatabase();
|
||||
writeDB = this.getWritableDatabase();
|
||||
addUser(new Admin());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -70,7 +71,6 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
+ COLUMN_USERTYPE + " STRING NOT NULL" + ")";
|
||||
|
||||
db.execSQL(CREATE_LOGIN_TABLE);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -96,14 +96,16 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
public boolean addUser(UserType userType){
|
||||
//Check for duplicate username by querying login table
|
||||
Cursor cursor = writeDB.query(TABLE_LOGIN,
|
||||
new String[] {COLUMN_USERNAME},
|
||||
COLUMN_USERNAME + " = ?",
|
||||
new String[]{userType.getUsername()},
|
||||
null, null, null,
|
||||
"1");
|
||||
new String[] {COLUMN_USERNAME},
|
||||
COLUMN_USERNAME + " = ?",
|
||||
new String[]{userType.getUsername()},
|
||||
null, null, null,
|
||||
"1");
|
||||
//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;
|
||||
}
|
||||
cursor.close();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
|
@ -127,8 +129,8 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
public UserType findUserByUsername(String username){
|
||||
UserType usertype;
|
||||
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN
|
||||
+ " WHERE " + COLUMN_USERNAME + " = ?",
|
||||
new String[]{username});
|
||||
+ " WHERE " + COLUMN_USERNAME + " = ?",
|
||||
new String[]{username});
|
||||
|
||||
if (cursor.moveToFirst()){
|
||||
String password = cursor.getString(1);
|
||||
|
@ -215,12 +217,15 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
public List<String[]> getAllUsers(){
|
||||
List<String[]> listOfUsers = new LinkedList<>();
|
||||
String[] user;
|
||||
Cursor cursor = readDB.rawQuery("SELECT ? , ?, ?, ? FROM "+TABLE_LOGIN,
|
||||
new String[]{COLUMN_USERNAME, COLUMN_FIRSTNAME,
|
||||
COLUMN_LASTNAME, COLUMN_USERTYPE});
|
||||
Cursor cursor = readDB.rawQuery("SELECT " + COLUMN_USERNAME + ", "
|
||||
+ COLUMN_FIRSTNAME + ", "
|
||||
+ COLUMN_LASTNAME + ", "
|
||||
+ COLUMN_USERTYPE
|
||||
+ " FROM "+TABLE_LOGIN, null);
|
||||
|
||||
if (cursor.moveToFirst()) {
|
||||
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++) {
|
||||
user[j] = cursor.getString(j);
|
||||
}
|
||||
|
@ -231,4 +236,4 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||
return listOfUsers;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,13 +11,26 @@ import org.robolectric.RobolectricTestRunner;
|
|||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(packageName = "com.uottawa.olympus.olympusservices")
|
||||
public class DBUnitTest {
|
||||
public class DBIntegrationTest {
|
||||
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
|
||||
public void testAddAndDeleteUser(){
|
||||
UserType originalUser, dbUser;
|
||||
|
@ -95,4 +108,20 @@ public class DBUnitTest {
|
|||
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