-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw6.0419 #40
base: lesson6-sqlite
Are you sure you want to change the base?
Hw6.0419 #40
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,10 @@ protected void setUp() throws Exception { | |
db = helper.getReadableDatabase(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
public void _testDatabaseCanOpenWritableWhileOpenReadable() throws Exception { | ||
|
||
new Thread(new Runnable() { | ||
|
@@ -54,7 +58,7 @@ public void run() { | |
// TODO check that database can be opened for writing while there is open for reading and vice verse | ||
} | ||
|
||
public void testHelperIsNotNull() throws Exception { | ||
public void _testHelperIsNotNull() throws Exception { | ||
assertNotNull(helper); | ||
} | ||
|
||
|
@@ -64,7 +68,7 @@ public void testDatabaseExists() throws Exception { | |
assertEquals(WeatherDbHelper.DATABASE_VERSION, db.getVersion()); | ||
} | ||
|
||
public void testSqlErrorProducesException() throws Exception { | ||
public void _testSqlErrorProducesException() throws Exception { | ||
try { | ||
Cursor c = db.rawQuery("hackafe is the best", null); | ||
fail("sql garbage does't produce exception!"); | ||
|
@@ -73,7 +77,7 @@ public void testSqlErrorProducesException() throws Exception { | |
} | ||
} | ||
|
||
public void testTablesExists() throws Exception { | ||
public void _testTablesExists() throws Exception { | ||
Cursor c = db.rawQuery("select name from sqlite_master where type = 'table' ", null); | ||
|
||
boolean found = false; | ||
|
@@ -87,7 +91,7 @@ public void testTablesExists() throws Exception { | |
assertTrue("table forecast was not found!", found); | ||
} | ||
|
||
public void testForecastHasAllColumn() throws Exception { | ||
public void _testForecastHasAllColumn() throws Exception { | ||
HashSet<String> expectedColumn = new HashSet<>(); | ||
expectedColumn.add(WeatherContract.ForecastTable._ID); | ||
expectedColumn.add(WeatherContract.ForecastTable.COLUMN_DATE); | ||
|
@@ -105,7 +109,7 @@ public void testForecastHasAllColumn() throws Exception { | |
assertEquals(0, expectedColumn.size()); | ||
} | ||
|
||
public void testSaveNewForecast() throws Exception { | ||
public void _testSaveNewForecast() throws Exception { | ||
long timestamp = new Date().getTime(); | ||
String forecastStr = "sunny all day long with chance for pizza"; | ||
Forecast forecast = new Forecast(timestamp, | ||
|
@@ -139,8 +143,63 @@ public void testSaveNewForecast() throws Exception { | |
assertEquals(timestamp, cursor.getLong(WeatherContract.ForecastTable.INDEX_DATE)); | ||
} | ||
|
||
|
||
// TODO validate only one record per day (try to insert 2 for a single day) | ||
public void testSqlInsertTwoRecordsForOneDay() throws Exception { | ||
System.out.println("testSqlInsertTwoRecordsForOneDay"); | ||
long timestamp = new Date().getTime(); | ||
String forecastStr = "two records for one day test"; | ||
Forecast forecast = new Forecast(timestamp, | ||
forecastStr); | ||
//Insert First Record | ||
System.out.println("Insert First Record"); | ||
helper.saveNewForecast(forecast); | ||
|
||
//Insert Second Record | ||
System.out.println("Insert Second Record"); | ||
helper.saveNewForecast(forecast); | ||
|
||
Cursor cursor = db.query( | ||
// table name | ||
WeatherContract.ForecastTable.TABLE_NAME, | ||
// select field | ||
WeatherContract.ForecastTable.PROJECTION, | ||
// where clause | ||
WeatherContract.ForecastTable.COLUMN_DATE+" = ?", | ||
// where argument | ||
new String[]{Long.toString(timestamp)}, | ||
// group by | ||
null, | ||
// having | ||
null, | ||
// order by | ||
null | ||
); | ||
System.out.println("Cursor rows: " + cursor.getCount()); | ||
// check for single record | ||
assertEquals(2, cursor.getCount()); | ||
|
||
|
||
} | ||
|
||
// TODO validate bad data | ||
// TODO validate we have a unique id for the inserted record | ||
public void testTimeDataValid() throws Exception { | ||
long timestamp = 0; | ||
// timestamp= new Date().getTime(); | ||
assertNotNull(timestamp); | ||
if (timestamp == 0){ | ||
fail(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Този тест би трябвало да проверява кода от |
||
} | ||
public void testforecastStrValid() throws Exception { | ||
|
||
String forecastStr = null; | ||
// forecastStr = "sunny all day long with chance for pizza"; | ||
|
||
assertNotNull(forecastStr); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Този тест би трябвало да извика saveNewForecast с невалидна стойност (като |
||
} | ||
// TODO validate we have a unique id for the inserted record | ||
//??????????? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,24 +13,28 @@ | |
public class WeatherDbHelper extends SQLiteOpenHelper { | ||
|
||
private static final String DATABASE_NAME = "weather.db"; | ||
static final int DATABASE_VERSION = 4; | ||
static final int DATABASE_VERSION = 8; | ||
|
||
public WeatherDbHelper(Context context) { | ||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL("DROP TABLE IF EXISTS forecast;"); | ||
System.out.println("onCreate"); | ||
System.out.println("Drop"); | ||
db.execSQL("DROP TABLE IF EXISTS forecasts;"); | ||
System.out.println("Create"); | ||
db.execSQL("CREATE TABLE \"forecasts\" (\n" + | ||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" + | ||
"weather TEXT NOT NULL," + | ||
"fordate INTEGER NOT NULL" + | ||
");\n"); | ||
"fordate INTEGER NOT NULL, " + | ||
" UNIQUE(fordate) ON CONFLICT IGNORE);\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
System.out.println("onUpgrade"); | ||
onCreate(db); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
| ||
# Please enter the commit message for your changes. Lines starting | ||
# with '#' will be ignored, and an empty message aborts the commit. | ||
# On branch hw6.0419 | ||
# Your branch is up-to-date with 'upstream/lesson6-sqlite'. | ||
# | ||
# Changes to be committed: | ||
# modified: .idea/modules.xml | ||
# modified: app/app.iml | ||
# modified: app/src/androidTest/java/org/hackafe/sunshine/data/TestDatabase.java | ||
# modified: app/src/main/java/org/hackafe/sunshine/data/WeatherDbHelper.java | ||
# new file: hw6-0419.iml | ||
# | ||
# Changes not staged for commit: | ||
# deleted: android-fundamentals-course.iml | ||
# | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В Pull Request-а теста трябва да минава