Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Testing with real databases #4

@artem-mindrov

Description

@artem-mindrov

I have a database access object of sorts, and I needed to write unit tests for it. Stubbing Ti.Database APIs with empty functions was not an option, since I had to make sure the DAO actually makes changes to the database. Since I've already used mockti for other unit tests, I started looking for a way to make it play with SQLite on Node. This was not an easy task (especially with me being absolutely new to Node and Titanium), and I ended up forking this repo and porting it to a newer version of Node and node-gyp (since I had to build it on Windows).
This is the code I'm using to mock Ti.Database:

class ResultSet
  constructor: (rows) -> 
    @rowIdx = 0
    @rows = []

    if rows.length
      @rowCount = rows.length
      @rows.push rows.item(i) for i in [0..rows.length-1]

  isValidRow: -> @rowIdx < @rowCount
  next: -> @rowIdx++
  fieldByName: (name) -> @rows[@rowIdx][name]
  field: (idx) -> require("lib/underscore").values(@rows[@rowIdx])[idx]
  close: -> 

class DB
  constructor: (@name) ->
    @lastInsertRowId = 0

  execute: (query, args...) ->
    sqlite = require "spec/lib/sqlite/sqlite"
    db = sqlite.openDatabaseSync @name
    console.log("running SQL #{query}")

    arg_array = []
    arg_array.push arg for arg in args
    result = db.query(query, arg_array)
    db.close()

    @resultSet = new ResultSet(result.rows)
    @lastInsertRowId = result.insertId if result.insertId
    @resultSet

  close: ->

Ti.Database = 
  open: (name) -> new DB(name)

Is there a chance something like this will be available? I could try to help if it makes any sense at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions