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

Clear Database Test Rule #29

Merged
merged 7 commits into from
Apr 4, 2017
Merged

Clear Database Test Rule #29

merged 7 commits into from
Apr 4, 2017

Conversation

Sloy
Copy link
Member

@Sloy Sloy commented Mar 14, 2017

This Rule works similarly to #24, but for SQLite databases.

It fetches all databases in the app, obtains all tables from them, and performs a "delete from" without where.

I decided to do this instead of a DROP because I think it might break the app, not sure though.

This should be tested against different scenarios and sdk versions. I've seen in Stetho that they have different ways of opening databases depending on the sdk version. Wonder why.

TODO:

  • Check on different sdk versions, and fix any issues
  • Document on the javadoc and readme
  • (Won't do) Maybe join with ClearPreferencesRule into one single rule for clearing app data.

@Sloy Sloy requested a review from rocboronat March 14, 2017 12:47
@Sloy
Copy link
Member Author

Sloy commented Mar 14, 2017

🔴 One Issue found: It doesn't work if the database hasn't been created prior to the test (which will be the usual case). We must only try to delete databases if they exist. 🙌

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.schibsted.spain.barista.sample/com.schibsted.spain.barista.sample.DatabaseActivity}: android.database.sqlite.SQLiteException: no such table: User (code 1): , while compiling: SELECT name FROM User
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such table: User (code 1): , while compiling: SELECT name FROM User)

@Sloy Sloy force-pushed the feature/clear-database-test-rule branch from 150497f to dab7a5d Compare March 14, 2017 13:16
@rocboronat
Copy link
Member

Great work! Let's see how it progress!

By the way @Sloy , what do you think about having one Rule for every kind of thing to be remoded, and another one that just joins all of them? I think that having different rules will make them simpler. So, if we want to ease using all of them at the same tame (that will be the most common way to work with Barista), we can just create a Rule to join them. And... well, we can talk about it later, let's focus on this particular Rule :·D

@Sloy
Copy link
Member Author

Sloy commented Mar 14, 2017

That was my original idea. But I wasn't too sure. I thought about having just one for everything for simplicity, but if you think having both can be useful, let's go with it. I don't have a strong opinion on neither.

@rocboronat
Copy link
Member

rocboronat commented Mar 14, 2017

@Sloy yes... the Rule that clears the Database needs 80 lines. The SharedPrefs one, 80 lines more I guess. Let's have them smaller as possible, and create a new Rule to join these Rules.

By the way, at some point of the future, we will need to remove all .jpg and .png pictures of the filesystem (or whatever other extension) so... let's keep that complex rules isolated.

@Sloy
Copy link
Member Author

Sloy commented Mar 15, 2017

One rule to rule them all!! 💍

@rocboronat
Copy link
Member

😂

@rocboronat rocboronat changed the title [WIP] Clear Database Test Rule Clear Database Test Rule Mar 30, 2017
@rocboronat rocboronat added the wip label Mar 30, 2017
@Sloy
Copy link
Member Author

Sloy commented Apr 3, 2017

🍏 The issue I commented about clearing databases that don't exist is now fixed.
I changed the way of finding databases and opening them. I had to take into account the shadow database files like those ended in "-journal".

*/
public class ClearDatabaseRule implements TestRule {

private static final String[] UNINTERESTING_FILENAME_SUFFIXES = new String[] {
Copy link
Member

Choose a reason for hiding this comment

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

Niiice!

writableDatabase.close();
}

private static class OpenHelper extends SQLiteOpenHelper {
Copy link
Member

Choose a reason for hiding this comment

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

We could call it OpenDatabaseHelper. When I seen the dependencies of this Activity, I went crazy: What's this class?

Several horses and monkey died during my madness.

private static class OpenHelper extends SQLiteOpenHelper {

public OpenHelper(Context context) {
super(context, "mydatabase", null, 1);
Copy link
Member

Choose a reason for hiding this comment

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

What means that 1?

return tidiedList;
}

private static String removeSuffix(String str, String[] suffixesToRemove) {
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about calling this method filterUninterestingSuffixes? And I suggest to avoid passing the list of suffixest to remove. It seems that they will be always the same: UNINTERESTING_FILENAME_SUFFIXES.


private void clearData() {
for (File dbFile : tidyDatabaseList(getDatabaseFiles())) {
SQLiteDatabase database = performOpen(dbFile, 0);
Copy link
Member

Choose a reason for hiding this comment

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

What means this 0?

* @param databaseFiles Raw list of database files.
* @return Tidied list with shadow databases removed.
*/
private static List<File> tidyDatabaseList(List<File> databaseFiles) {
Copy link
Member

Choose a reason for hiding this comment

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

This method name sounds strange... Don't know. I don't have a suggestion, by the way :·)

@rocboronat
Copy link
Member

There are some comments but... great work!

@Sloy Sloy force-pushed the feature/clear-database-test-rule branch from 64bf82f to 967888a Compare April 3, 2017 16:35
@Sloy
Copy link
Member Author

Sloy commented Apr 3, 2017

Rebased an squashed, much of the code on the rule has been changed @rocboronat.
I will check out your comments on the rest of the code later :)

@rocboronat
Copy link
Member

Nice, @Sloy ! Check my comments and go on applying the suggestion or merging :·)

Creates Activity and test to check if database is cleared
@Sloy Sloy force-pushed the feature/clear-database-test-rule branch from 967888a to c2c2895 Compare April 3, 2017 17:14
@Sloy Sloy removed the wip label Apr 3, 2017
@Sloy
Copy link
Member Author

Sloy commented Apr 3, 2017

Travis likes it. I tried a local build with InfoJobs project and everything works as expected.
Any final words @rocboronat?

@rocboronat
Copy link
Member

Ave, @Sloy , morituri te salutant! Great work there :·) It's merge time! Feel free to release a version if you need it! :·)

@Sloy Sloy merged commit 535108e into master Apr 4, 2017
@Sloy Sloy deleted the feature/clear-database-test-rule branch April 4, 2017 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants