Skip to content
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

Open
wants to merge 3 commits into
base: lesson6-sqlite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="android-fundamentals-course" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="hw6-0419" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ protected void setUp() throws Exception {
db = helper.getReadableDatabase();
}





public void _testDatabaseCanOpenWritableWhileOpenReadable() throws Exception {

new Thread(new Runnable() {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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!");
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В Pull Request-а теста трябва да минава



}

// 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();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Този тест би трябвало да проверява кода от WeatherDBHelper-а. Тоест трябва да извика saveNewForecast с невалидна дата (примерно 0) и да очаква че ще получи IllegalArgumentException

}
public void testforecastStrValid() throws Exception {

String forecastStr = null;
// forecastStr = "sunny all day long with chance for pizza";

assertNotNull(forecastStr);


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Този тест би трябвало да извика saveNewForecast с невалидна стойност (като null) за прогнозата и да очаква да се получи IllegalArgumentException

}
// TODO validate we have a unique id for the inserted record
//???????????
}
12 changes: 8 additions & 4 deletions app/src/main/java/org/hackafe/sunshine/data/WeatherDbHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ON CONFLICT ROLLBACK е по-правилно за да имаме явна грешка. Вече в кода можем да решим да я игнорираме или не.

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("onUpgrade");
onCreate(db);
}

Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions ulorm0p3.bfd.txt
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
#