From 0ea0c07974fb65cfc7801ee2f843b4f6974d6003 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Thu, 28 Oct 2010 21:08:21 -0500
Subject: [PATCH] start with a simple test

---
 tests/database_test.info    |    6 +++
 tests/database_test.install |   74 +++++++++++++++++++++++++++++++++++++++++++
 tests/database_test.module  |    1 +
 tests/database_test.test    |   48 ++++++++++++++++++++++++++++
 4 files changed, 129 insertions(+), 0 deletions(-)
 create mode 100644 tests/database_test.info
 create mode 100644 tests/database_test.install
 create mode 100644 tests/database_test.module
 create mode 100644 tests/database_test.test

diff --git tests/database_test.info tests/database_test.info
new file mode 100644
index 0000000..407626a
--- /dev/null
+++ tests/database_test.info
@@ -0,0 +1,6 @@
+; $Id$
+name = "Database Test"
+description = "Support module for Database layer tests."
+core = 6.x
+package = Testing
+dependencies[] = dbtng
diff --git tests/database_test.install tests/database_test.install
new file mode 100644
index 0000000..4058884
--- /dev/null
+++ tests/database_test.install
@@ -0,0 +1,74 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Install, update and uninstall functions for the database_test module.
+ */
+
+/**
+ * Implements hook_schema().
+ */
+function database_test_schema() {
+  $schema['test'] = array(
+    'description' => 'Basic test table for the database unit tests.',
+    'fields' => array(
+      'id' => array(
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'name' => array(
+        'description' => "A person's name",
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'age' => array(
+        'description' => "The person's age",
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'job' => array(
+        'description' => "The person's job",
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => 'Undefined',
+      ),
+    ),
+    'primary key' => array('id'),
+    'unique keys' => array(
+      'name' => array('name')
+    ),
+    'indexes' => array(
+      'ages' => array('age'),
+    ),
+  );
+
+  return $schema;
+}
+
+/**
+ * Implemetation of hook_install().
+ */
+function database_test_install() {
+  drupal_install_schema('database_test');
+}
+
+/**
+ * Implemetation of hook_enable().
+ */
+function database_test_enable() {
+  cache_clear_all();
+}
+
+/**
+ * Implemetation of hook_uninstall().
+ */
+function database_test_uninstall() {
+  drupal_uninstall_schema('database_test');
+}
diff --git tests/database_test.module tests/database_test.module
new file mode 100644
index 0000000..b3d9bbc
--- /dev/null
+++ tests/database_test.module
@@ -0,0 +1 @@
+<?php
diff --git tests/database_test.test tests/database_test.test
new file mode 100644
index 0000000..06deb8e
--- /dev/null
+++ tests/database_test.test
@@ -0,0 +1,48 @@
+<?php
+// $Id$
+
+class DatabaseFetchTestCase extends DrupalWebTestCase {
+  function setUp() {
+    parent::setUp('autoload', 'dbtng', 'database_test');
+  }
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Fetch tests',
+      'description' => 'Test the Database system\'s various fetch capabilities.',
+      'group' => 'DBTNG',
+    );
+  }
+
+  function testSimpleQuery() {
+    // use normal query to insert a record
+    $item = array(
+      'name' => 'John',
+      'age' => 25,
+      'job' => 'Singer',
+    );
+    drupal_write_record('test', $item);
+
+    // try reading it using normal db api
+    $records = array();
+    $result = db_query('SELECT name FROM {test} WHERE age = %d', 25);
+    while ($record = db_fetch_object($result)) {
+      $records[] = $record;
+      $this->assertTrue(is_object($record), t('Record is an object.'));
+      $this->assertIdentical($record->name, 'John', t('25 year old is John.'));
+    }
+    $this->assertIdentical(count($records), 1, t('There is only one record.'));
+    // now try the same select with dbtng
+    $records = array();
+    $result = db_select('test', 't')
+      ->fields('t', array('name'))
+      ->condition('t.age', 25)
+      ->execute();
+    foreach ($result as $record) {
+      $records[] = $record;
+      $this->assertTrue(is_object($record), t('Record is an object.'));
+      $this->assertIdentical($record->name, 'John', t('25 year old is John.'));
+    }
+    $this->assertIdentical(count($records), 1, t('There is only one record.'));
+  }
+}
-- 
1.7.1

