|
| 1 | +package org.mongodb.command |
| 2 | + |
| 3 | +import org.mongodb.Document |
| 4 | +import org.mongodb.connection.ServerAddress |
| 5 | +import org.mongodb.operation.CommandResult |
| 6 | +import spock.lang.Specification |
| 7 | +import spock.lang.Subject |
| 8 | + |
| 9 | +class MapReduceCommandResultSpecification extends Specification { |
| 10 | + |
| 11 | + def setupSpec() { |
| 12 | + Map.metaClass.asType = { Class type -> |
| 13 | + if (type == Document) { |
| 14 | + return new Document(delegate) |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + @Subject |
| 20 | + private MapReduceCommandResult<Document> commandResult |
| 21 | + |
| 22 | + def 'should extract correct value from "results" field'() { |
| 23 | + when: |
| 24 | + commandResult = new MapReduceCommandResult<>(new CommandResult( |
| 25 | + [:] as Document, |
| 26 | + new ServerAddress(), |
| 27 | + ['results': [new Document('a', 1), new Document('b', 2)]] as Document, |
| 28 | + 0 |
| 29 | + )); |
| 30 | + |
| 31 | + then: |
| 32 | + commandResult.isInline() |
| 33 | + commandResult.getValue() instanceof Iterable<Document> |
| 34 | + } |
| 35 | + |
| 36 | + def 'should extract collectionName from result'() { |
| 37 | + when: |
| 38 | + commandResult = new MapReduceCommandResult<>(new CommandResult( |
| 39 | + [:] as Document, |
| 40 | + new ServerAddress(), |
| 41 | + ['result': 'foo'] as Document, |
| 42 | + 0 |
| 43 | + )); |
| 44 | + |
| 45 | + then: |
| 46 | + commandResult.getTargetCollectionName() == 'foo' |
| 47 | + } |
| 48 | + |
| 49 | + def 'should extract databaseName and collectionName from result'() { |
| 50 | + when: |
| 51 | + commandResult = new MapReduceCommandResult<>(new CommandResult( |
| 52 | + [:] as Document, |
| 53 | + new ServerAddress(), |
| 54 | + ['result': ['collection': 'foo', 'db': 'bar'] as Document] as Document, |
| 55 | + 0 |
| 56 | + )); |
| 57 | + |
| 58 | + then: |
| 59 | + commandResult.getTargetCollectionName() == 'foo' |
| 60 | + commandResult.getTargetDatabaseName() == 'bar' |
| 61 | + } |
| 62 | + |
| 63 | + def 'should return null if there is no database name in result'() { |
| 64 | + when: |
| 65 | + commandResult = new MapReduceCommandResult<>(new CommandResult( |
| 66 | + [:] as Document, |
| 67 | + new ServerAddress(), |
| 68 | + ['result': ['collection': 'foo'] as Document] as Document, |
| 69 | + 0 |
| 70 | + )); |
| 71 | + |
| 72 | + then: |
| 73 | + commandResult.getTargetDatabaseName() == null |
| 74 | + } |
| 75 | + |
| 76 | + def 'should throw on getting collectionName for result with inlined data'(){ |
| 77 | + setup: |
| 78 | + commandResult = new MapReduceCommandResult<>(new CommandResult( |
| 79 | + [:] as Document, |
| 80 | + new ServerAddress(), |
| 81 | + ['results': [new Document('a', 1), new Document('b', 2)]] as Document, |
| 82 | + 0 |
| 83 | + )); |
| 84 | + |
| 85 | + when: |
| 86 | + commandResult.getTargetCollectionName() |
| 87 | + |
| 88 | + then: |
| 89 | + thrown(IllegalAccessError) |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments