Permalink
Browse files

Merge pull request #2 from neomantra/master

added unit tests
  • Loading branch information...
2 parents 04dabd6 + e83f6bd commit 0cbcebf5b571ef0303959fbfb97b9aa82698b43b @mtowers mtowers committed Aug 2, 2011
Showing with 494 additions and 0 deletions.
  1. +64 −0 tests/connection.lua
  2. +366 −0 tests/lunity.lua
  3. +64 −0 tests/replicaset.lua
View
@@ -0,0 +1,64 @@
+#!/usr/bin/lua
+-- Tests DB connection bindings
+--
+-- Configuration can be set with the following environment variables:
+-- TEST_SERVER ('localhost')
+-- TEST_DB ('test')
+-- TEST_USER (nil, no auth will be done)
+-- TEST_PASS ('')
+
+require 'mongo'
+require 'os'
+
+require 'tests/lunity'
+module( 'LUAMONGO_TEST_CONNECTION', lunity )
+
+function setup()
+ test_server = os.getenv('TEST_SERVER') or 'localhost'
+ test_user = os.getenv('TEST_USER') or nil
+ test_password = os.getenv('TEST_PASS') or ''
+ test_db = os.getenv('TEST_DB') or 'test'
+ test_ns = test_db .. '.conn_values'
+end
+
+function teardown()
+end
+
+function test_ReplicaSet()
+ -- Create a Connection object
+ local db = mongo.Connection.New()
+ assertNotNil( db, 'unable to create mongo.Connection' )
+ assert( db:connect(test_server), 'unable to forcefully connect to mongo instance' )
+
+ -- test data
+ local data = { a = 10, b = 'str1' }
+ local data_as_json = "{'a': 10, 'b': 'str1'}"
+
+ -- auth if we need to
+ if test_user then
+ assertTrue( db:auth{dbname=test_db, username=test_user, password=test_password}, "unable to auth to db" )
+ end
+
+ -- drop existing data
+ assertTrue( db:drop_collection(test_ns) )
+
+ -- insert a value into the namespace 'test.values'
+ assertTrue( db:insert(test_ns, data), 'unable to insert table-based data' )
+
+ -- insert the same using a JSON string
+ assertTrue( db:insert(test_ns, data_as_json), 'unable to insert JSON-based data' )
+
+ -- check the data
+ assertEqual( 2, db:count(test_ns) )
+
+ -- query all the values in the namespace, ensuring the values are equal to the inserted values
+ local q = db:query( test_ns, {} )
+ assertNotNil( q, 'unable to create Query object' )
+ for result in q:results() do
+ assertEqual( result.a, data.a )
+ assertEqual( result.b, data.b )
+ end
+end
+
+
+runTests()
Oops, something went wrong.

0 comments on commit 0cbcebf

Please sign in to comment.