summaryrefslogtreecommitdiff
path: root/jstests/geo_array2.js
blob: 28cb15295bbf946a95f9c0a894039ce8ea1b8735 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Check the semantics of near calls with multiple locations

t = db.geoarray2
t.drop();

var numObjs = 10;
var numLocs = 100;

// Test the semantics of near / nearSphere / etc. queries with multiple keys per object

for( var i = -1; i < 2; i++ ){
	for(var j = -1; j < 2; j++ ){
		
		locObj = []
		
		if( i != 0 || j != 0 )
			locObj.push( { x : i * 50 + Random.rand(), 
			               y : j * 50 + Random.rand() } )
	    locObj.push( { x : Random.rand(), 
	    			   y : Random.rand() } )
	    locObj.push( { x : Random.rand(),
	    			   y : Random.rand() } )
	    			   
	    t.insert({ name : "" + i + "" + j , loc : locObj , type : "A" })
	    t.insert({ name : "" + i + "" + j , loc : locObj , type : "B" })
	}
}

t.ensureIndex({ loc : "2d" , type : 1 })

assert.isnull( db.getLastError() )

print( "Starting testing phase... ")

for( var t = 0; t < 2; t++ ){
	
var type = t == 0 ? "A" : "B"

for( var i = -1; i < 2; i++ ){
	for(var j = -1; j < 2; j++ ){
		
		var center = [ i * 50 , j * 50 ]
		var count = i == 0 && j == 0 ? 2 * 9 : 1
		var objCount = i == 0 && j == 0 ? 2 : 1
		
		// Do near check
				
		var nearResults = db.runCommand( { geoNear : "geoarray2" , 
											  near : center ,
											   num : count,
											 query : { type : type } } ).results
		//printjson( nearResults )								 
			
	    var objsFound = {}
		var lastResult = 0;
		for( var k = 0; k < nearResults.length; k++ ){
			
			// All distances should be small, for the # of results
			assert.gt( 1.5 , nearResults[k].dis )
			// Distances should be increasing
			assert.lte( lastResult, nearResults[k].dis )
			// Objs should be of the right type
			assert.eq( type, nearResults[k].obj.type )
			
			lastResult = nearResults[k].dis
			
			var objKey = "" + nearResults[k].obj._id
			
			if( objKey in objsFound ) objsFound[ objKey ]++
			else objsFound[ objKey ] = 1
			
		}
				
		// Make sure we found the right objects each time
		// Note: Multiple objects could be found for diff distances.
		for( var q in objsFound ){
			assert.eq( objCount , objsFound[q] )
		}
		
				
		// Do nearSphere check
		
		// Earth Radius
		var eRad = 6371
				
		nearResults = db.geoarray2.find( { loc : { $nearSphere : center , $maxDistance : 500 /* km */ / eRad }, type : type } ).toArray()
		
		assert.eq( nearResults.length , count )
			
	    objsFound = {}
		lastResult = 0;
		for( var k = 0; k < nearResults.length; k++ ){			
			var objKey = "" + nearResults[k]._id
			if( objKey in objsFound ) objsFound[ objKey ]++
			else objsFound[ objKey ] = 1
			
		}
		
		// Make sure we found the right objects each time
		for( var q in objsFound ){
			assert.eq( objCount , objsFound[q] )
		}
		
		
		
		// Within results do not return duplicate documents
		
		var count = i == 0 && j == 0 ? 9 : 1
		var objCount = i == 0 && j == 0 ? 1 : 1
		
		// Do within check
		objsFound = {}
		
		var box = [ [center[0] - 1, center[1] - 1] , [center[0] + 1, center[1] + 1] ]
		
		//printjson( box )
		
		var withinResults = db.geoarray2.find({ loc : { $within : { $box :  box } } , type : type }).toArray()
		
		assert.eq( withinResults.length , count )
		
		for( var k = 0; k < withinResults.length; k++ ){
			var objKey = "" + withinResults[k]._id
			if( objKey in objsFound ) objsFound[ objKey ]++
			else objsFound[ objKey ] = 1
		}
		
		//printjson( objsFound )
		
		// Make sure we found the right objects each time
		for( var q in objsFound ){
			assert.eq( objCount , objsFound[q] )
		}
		
		
		// Do within check (circle)
		objsFound = {}
		
		withinResults = db.geoarray2.find({ loc : { $within : { $center :  [ center, 1.5 ] } } , type : type }).toArray()
		
		assert.eq( withinResults.length , count )
		
		for( var k = 0; k < withinResults.length; k++ ){
			var objKey = "" + withinResults[k]._id
			if( objKey in objsFound ) objsFound[ objKey ]++
			else objsFound[ objKey ] = 1
		}
		
		// Make sure we found the right objects each time
		for( var q in objsFound ){
			assert.eq( objCount , objsFound[q] )
		}
		
		
		
	}
}

}