@@ -2269,6 +2269,177 @@ def self.ages; self; end
22692269 end
22702270 end
22712271
2272+ describe '#raw' do
2273+ let ( :result ) { results [ 0 ] }
2274+
2275+ context 'when the parameters are inconsistent' do
2276+ let ( :results ) { criteria . raw ( false , typed : false ) . to_a }
2277+ let ( :criteria ) { Person }
2278+
2279+ it 'raises an ArgumentError' do
2280+ expect { result } . to raise_error ( ArgumentError )
2281+ end
2282+ end
2283+
2284+ context 'when returning untyped results' do
2285+ let ( :results ) { criteria . raw . to_a }
2286+
2287+ context 'without associations' do
2288+ before do
2289+ Band . create ( name : 'the band' ,
2290+ active : true ,
2291+ genres : %w[ abc def ] ,
2292+ member_count : 112 ,
2293+ rating : 4.2 ,
2294+ created : Time . now ,
2295+ updated : Time . now ,
2296+ sales : 1_234_567.89 ,
2297+ decimal : 9_876_543.21 ,
2298+ decibels : 140 ..170 ,
2299+ deleted : false ,
2300+ mojo : Math ::PI ,
2301+ tags : { 'one' => 1 , 'two' => 2 } ,
2302+ location : LatLng . new ( 41.74 , -111.83 ) )
2303+ end
2304+
2305+ let ( :criteria ) { Band . where ( name : 'the band' ) }
2306+
2307+ it 'returns a hash' do
2308+ expect ( result ) . to be_a ( Hash )
2309+ end
2310+
2311+ it 'does not demongoize the result' do
2312+ expect ( result [ 'genres' ] ) . to be_a ( Array )
2313+ expect ( result [ 'decibels' ] ) . to be == { 'min' => 140 , 'max' => 170 }
2314+ expect ( result [ 'location' ] ) . to be == [ -111.83 , 41.74 ]
2315+ end
2316+ end
2317+
2318+ context 'with associations' do
2319+ before do
2320+ Person . create ( {
2321+ addresses : [ Address . new ( end_date : 2 . months . from_now ) ] ,
2322+ passport : Passport . new ( exp : 1 . year . from_now )
2323+ } )
2324+ end
2325+
2326+ let ( :criteria ) { Person }
2327+
2328+ it 'demongoizes the embedded relation' do
2329+ expect ( result [ 'addresses' ] ) . to be_a ( Array )
2330+ expect ( result [ 'addresses' ] [ 0 ] [ 'end_date' ] ) . to be_a ( Time )
2331+
2332+ # `pass` is how it is stored, `passport` is how it is aliased
2333+ expect ( result [ 'pass' ] ) . to be_a ( Hash )
2334+ expect ( result [ 'pass' ] [ 'exp' ] ) . to be_a ( Time )
2335+ end
2336+ end
2337+
2338+ context 'with projections' do
2339+ before { Person . create ( title : 'sir' , dob : Date . new ( 1980 , 1 , 1 ) ) }
2340+
2341+ context 'using #only' do
2342+ let ( :criteria ) { Person . only ( :dob ) }
2343+
2344+ it 'produces a hash with only the _id and the requested key' do
2345+ expect ( result ) . to be_a ( Hash )
2346+ expect ( result . keys ) . to be == %w[ _id dob ]
2347+ expect ( result [ 'dob' ] ) . to be == Date . new ( 1980 , 1 , 1 )
2348+ end
2349+ end
2350+
2351+ context 'using #without' do
2352+ let ( :criteria ) { Person . without ( :dob ) }
2353+
2354+ it 'produces a hash that excludes requested key' do
2355+ expect ( result ) . to be_a ( Hash )
2356+ expect ( result . keys ) . not_to include ( 'dob' )
2357+ expect ( result . keys ) . to be_present
2358+ end
2359+ end
2360+ end
2361+ end
2362+
2363+ context 'when returning typed results' do
2364+ let ( :results ) { criteria . raw ( typed : true ) . to_a }
2365+
2366+ context 'without associations' do
2367+ before do
2368+ Band . create ( name : 'the band' ,
2369+ active : true ,
2370+ genres : %w[ abc def ] ,
2371+ member_count : 112 ,
2372+ rating : 4.2 ,
2373+ created : Time . now ,
2374+ updated : Time . now ,
2375+ sales : 1_234_567.89 ,
2376+ decimal : 9_876_543.21 ,
2377+ decibels : 140 ..170 ,
2378+ deleted : false ,
2379+ mojo : Math ::PI ,
2380+ tags : { 'one' => 1 , 'two' => 2 } ,
2381+ location : LatLng . new ( 41.74 , -111.83 ) )
2382+ end
2383+
2384+ let ( :criteria ) { Band . where ( name : 'the band' ) }
2385+
2386+ it 'returns a hash' do
2387+ expect ( result ) . to be_a ( Hash )
2388+ end
2389+
2390+ it 'demongoizes the result' do
2391+ expect ( result [ 'genres' ] ) . to be_a ( Array )
2392+ expect ( result [ 'decibels' ] ) . to be_a ( Range )
2393+ expect ( result [ 'location' ] ) . to be_a ( LatLng )
2394+ end
2395+ end
2396+
2397+ context 'with associations' do
2398+ before do
2399+ Person . create ( {
2400+ addresses : [ Address . new ( end_date : 2 . months . from_now ) ] ,
2401+ passport : Passport . new ( exp : 1 . year . from_now )
2402+ } )
2403+ end
2404+
2405+ let ( :criteria ) { Person }
2406+
2407+ it 'demongoizes the embedded relation' do
2408+ expect ( result [ 'addresses' ] ) . to be_a ( Array )
2409+ expect ( result [ 'addresses' ] [ 0 ] [ 'end_date' ] ) . to be_a ( Date )
2410+
2411+ # `pass` is how it is stored, `passport` is how it is aliased
2412+ expect ( result [ 'pass' ] ) . to be_a ( Hash )
2413+ expect ( result [ 'pass' ] [ 'exp' ] ) . to be_a ( Date )
2414+ end
2415+ end
2416+
2417+ context 'with projections' do
2418+ before { Person . create ( title : 'sir' , dob : Date . new ( 1980 , 1 , 1 ) ) }
2419+
2420+ context 'using #only' do
2421+ let ( :criteria ) { Person . only ( :dob ) }
2422+
2423+ it 'produces a hash with only the _id and the requested key' do
2424+ expect ( result ) . to be_a ( Hash )
2425+ expect ( result . keys ) . to be == %w[ _id dob ]
2426+ expect ( result [ 'dob' ] ) . to be == Date . new ( 1980 , 1 , 1 )
2427+ end
2428+ end
2429+
2430+ context 'using #without' do
2431+ let ( :criteria ) { Person . without ( :dob ) }
2432+
2433+ it 'produces a hash that excludes requested key' do
2434+ expect ( result ) . to be_a ( Hash )
2435+ expect ( result . keys ) . not_to include ( 'dob' )
2436+ expect ( result . keys ) . to be_present
2437+ end
2438+ end
2439+ end
2440+ end
2441+ end
2442+
22722443 describe "#max_scan" do
22732444 max_server_version '4.0'
22742445
0 commit comments