Add Admin first, always, and fixed getAllUsers

This commit is contained in:
Mary Tran 2018-10-21 21:13:11 -04:00
parent 846df76cdb
commit 6cbf771b82
2 changed files with 49 additions and 15 deletions

View file

@ -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
@ -102,8 +102,10 @@ public class DBHelper extends SQLiteOpenHelper {
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();
@ -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);
} }

View file

@ -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");
}
} }