From 3deef01b13dc8c8413d251983cabb6b26fdb6943 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Wed, 21 Dec 2011 23:41:57 -0700
Subject: [PATCH] add several database tests for new database APIs, fix several
 small bugs in db.emailType()

---
 lib/db/json.js   |  2 +-
 lib/db/mysql.js  |  3 +--
 tests/db-test.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/lib/db/json.js b/lib/db/json.js
index 3dace705a..4af6a5453 100644
--- a/lib/db/json.js
+++ b/lib/db/json.js
@@ -130,7 +130,7 @@ exports.emailKnown = function(email, cb) {
 exports.emailType = function(email, cb) {
   sync();
   var m = jsel.match(".emails ." + ESC(email), db.users);
-  process.nextTick(function() { cb(m.length ? m.type : undefined); });
+  process.nextTick(function() { cb(m.length ? m[0].type : undefined); });
 };
 
 exports.isStaged = function(email, cb) {
diff --git a/lib/db/mysql.js b/lib/db/mysql.js
index ac93d50f4..7c46cd64b 100644
--- a/lib/db/mysql.js
+++ b/lib/db/mysql.js
@@ -239,7 +239,7 @@ exports.emailType = function(email, cb) {
     "SELECT type FROM email WHERE address = ?", [ email ],
     function(err, rows) {
       if (err) logUnexpectedError(err);
-      cb((rows && rows.length > 0) ? rows[0].type : null);
+      cb((rows && rows.length > 0) ? rows[0].type : undefined);
     }
   );
 }
@@ -368,7 +368,6 @@ exports.createUserWithPrimaryEmail = function(email, cb) {
   client.query(
     "INSERT INTO user() VALUES()",
     function(err, info) {
-      console.log(info.insertId);
       if (err) { logUnexpectedError(err); cb(err); return; }
       client.query(
         "INSERT INTO email(user, address, type) VALUES(?, ?, ?)",
diff --git a/tests/db-test.js b/tests/db-test.js
index 3fdba7a34..f917c67b7 100755
--- a/tests/db-test.js
+++ b/tests/db-test.js
@@ -245,6 +245,34 @@ suite.addBatch({
   }
 });
 
+
+suite.addBatch({
+  "emailType of lloyd@anywhe.re": {
+    topic: function() {
+      db.emailType('lloyd@anywhe.re', this.callback);
+    },
+    "is null": function (r) {
+      assert.isUndefined(r);
+    }
+  },
+  "emailType of lloyd@somewhe.re": {
+    topic: function() {
+      db.emailType('lloyd@somewhe.re', this.callback);
+    },
+    "is 'secondary'": function (r) {
+      assert.strictEqual(r, 'secondary');
+    }
+  },
+  "emailType of lloyd@nowhe.re": {
+    topic: function() {
+      db.emailType('lloyd@nowhe.re', this.callback);
+    },
+    "is 'secondary'": function (r) {
+      assert.strictEqual(r, 'secondary');
+    }
+  }
+});
+
 suite.addBatch({
   "removing an existing email": {
     topic: function() {
@@ -283,6 +311,33 @@ suite.addBatch({
   }
 });
 
+suite.addBatch({
+  "creating a primary account": {
+    topic: function() {
+      db.createUserWithPrimaryEmail("lloyd@primary.domain", this.callback);
+    },
+    "returns no error": function(r) {
+      assert.isUndefined(r);
+    },
+    "causes emailKnown": {
+      topic: function() {
+        db.emailKnown('lloyd@primary.domain', this.callback);
+      },
+      "to return true": function (r) {
+        assert.strictEqual(r, true);
+      }
+    },
+    "causes emailType": {
+      topic: function() {
+        db.emailType('lloyd@primary.domain', this.callback);
+      },
+      "to return 'primary'": function (r) {
+        assert.strictEqual(r, 'primary');
+      }
+    }
+  }
+});
+
 suite.addBatch({
   "closing the database": {
     topic: function() {
-- 
GitLab