Showing posts with label TDD. Show all posts
Showing posts with label TDD. Show all posts

Saturday, 22 December 2018

TravelTimeCalculator: Further Improvements to the Database (TDD)

Test: Check if dbHelper can add information to an existing entry (continued)
Currently, it is only possible to add information to the database if there is no entry at all for the coordinates.

If I add a direction for the mode "WALKING" to an empty SQLite database using INSERT, it will look like this:
There is data only for the mode "WALKING" in this entry. The other ones are "null".
If I'd like to fill in information about the mode "DRIVING" using INSERT for the same coordinate, the constraint that I used to define the database will prevent me from doing that.

Instead, I need to use the SQLite "UPDATE" command when there is already entries for that direction. 

Using the method checkIfCoordinateExist, to decide whether to insert or update entries, with or without reversed coordinates, I was able to add directions in reversed diretions.


With those changes, I was able to pass the new TDD test case.

Test: Get Duration and Distance for All Modes for Coordinates
I made similar changes in the code to be able to get reversed directions for all modes.

Finally, I added a method for printing the contents of the database to the console.

Next step
Soon, I'll be able to continue working with the app itself, instead of focusing on the database. 

First, I need to define which coordinates the app should scan for, given the center and zoom of the current map view.

Saturday, 1 December 2018

TravelTimeCalculator: API Issues, TDD and Checking Records In Database

After making the app target API level 28, I got a strange crash. I found that I need to specify the requirement for Apache HTTP Legacy library, and after adding that dependency, the app was running as before.


The next issue that I saw was that my database method dbHelper:onCreate wasn't called as intended. dbHelper is inherited from SQLiteOpenHelper.

That behaviour was actually correct. The onCreate method is only called when Android needs the database.

Many of the current constants in dbHelper are hardcoded. I need to replace them with strings that I can change in the top of the file.


First TDD Test
The first TDD test checks that I can add a simple entry to the database (containing only one direction) and that I can read it out from the database.

The test intially failed since I've stubbed the addEntry method. After fixing that, I got an exception when querying the database.

That issue was caused by a typo. I missed to add a white space in the SQLite query.

Once that was fixed, I was able to pass the first Test Driven Development test case. I've updated the Github repo.

The next TDD test cases will cover:
  • How to add data to an existing entry
  • How to handle queries for non-existing entries
  • Ensuring that the database won't add entries in the opposite direction. If "From A, B to C, D" exists, "From C, D to A, B" shouldn't be added.