/
connection.lua
executable file
·70 lines (55 loc) · 2.06 KB
/
connection.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/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
-- query for a single result from the namespace
local result = db:find_one( test_ns, {} )
assertNotNil( result, 'could not find result' )
assertEqual( result.a, data.a )
assertEqual( result.b, data.b )
end
runTests()