Friday, November 16, 2012

Getting Dirty Dart

‹prev | My Chain | next›

If I am going to run a Dart backend for my Dart Comics sample app, I am going to need a simple data store. Over in node.js-land, I am a big fan of node-dirty. It's ridiculously easy to use and serves as a filesystem datastore for small data sets. So let's see if I can build a passable copy in Dart.

I believe that I am going to need to build this with unit tests. I cannot conceive of a way to spike this into existence. So I get started by creating a dart-dirty directory with a pubspec.yaml file:
name: dart-dirty
version: 0.0.1
description: Dirt, simple nosql DB
author: Chris Strom 
homepage: https://github.com/eee-c/dart-dirty
dependencies:
  unittest:
    sdk: unittest
The reason that I start here is the dependencies. I use Dart Pub to pull down the SDK's unit test library. I install this dependency with pub install:
➜  dart-dirty git:(master) ✗ pub install
Resolving dependencies...
Dependencies installed!
Then, in test/dirty_test.dart, I can import the unittest library like so:
#import('package:unittest/unittest.dart');

test_create() {
}

main() {
  test_create();
}
For tonight, I will be satisfied if I can create a new (empty) database. If a Dirty object is instantiated with the required path, then a database file at that path should exist:
#import('package:unittest/unittest.dart');
#import('package:dart_dirty/dirty.dart');

#import('dart:io');

test_create() {
  test("creates a new DB", () {
    new Dirty('test/test.db');
    expect(
      new File('test/test.db').existsSync(),
      equals(true)
    );
  });
}

main() {
  test_create();
}
I add a skeleton Dirty class in lib/dirty.dart:
#library('dart_dirty');

class Dirty {
  Dirty(path) {}
}
With that, I have my failing test:
➜  dart-dirty git:(master) ✗ dart test/dirty_test.dart
unittest-suite-wait-for-done
FAIL: creates a new DB
  Expected: <true>
       but: was <false>.
To make this test pass, I instantiate a File object and open an write stream on it:
class Dirty {
  OutputStream _writeStream;
  File db;

  Dirty(path) {
    db = new File(path);
    _load();
  }

  _load() {
    _writeStream = db.openOutputStream(FileMode.APPEND);
  }
}
And that does the trick:
➜  dart-dirty git:(master) ✗ dart test/dirty_test.dart
unittest-suite-wait-for-done
PASS: creates a new DB

All 1 tests passed.
unittest-suite-success
Yay!

I do have a bit of a problem. At the start of subsequent test runs, the test/test.db database file still exists. I can get rid of it with:
main() {
  before();
  test_create();
}

before() {
  var db = new File('test/test.db');
  if (!db.existsSync()) return;
  db.deleteSync();
}
I try to delete that in an "after" function as well, but that does not work. The expect() function seems to be asynchronous, which means that I try to delete the DB file before it even exists. I will have to investigate if there is a convention for cleaning up after tests in Dart. Tomorrow.

To be sure, I have tons to do here, but this a good start. I have both a test file and an actual library file. Tomorrow, I will push through as much functionality as I can.


Day #572

No comments:

Post a Comment