Skip to content

Commit

Permalink
Added remove() function tests to the Collection, DB and Wire. Tests f…
Browse files Browse the repository at this point in the history
…or Wire are missing, pending a refactor here. Added makeRemoveOpDeleteMessage() and tests for MessageFactory. Removed an unnecessary endian setting in OpInsert. The remove() method is now working.

Signed-off-by: Omar Gonzalez <omar@almerblank.com>
  • Loading branch information
s9tpepper committed Jul 4, 2011
1 parent 1bc007c commit 3c28825
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/as3/mongo/db/DB.as
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ package as3.mongo.db

public function authenticate():Boolean
{
trace("authenticate()");
DBMethodInputValidator.canAuthenticate(this);
authenticationFactory.getAuthentication(this);
return true;
Expand Down Expand Up @@ -137,7 +136,7 @@ package as3.mongo.db

public function runCommand(command:Document, readCommandReplyCallback:Function=null):Signal
{
return _wire.runCommand(command);
return wire.runCommand(command);
}

public function connect():void
Expand All @@ -149,5 +148,10 @@ package as3.mongo.db
{
wire.insert(name, collectionName, document);
}

public function remove(collectionName:String, selector:Document):void
{
wire.remove(name, collectionName, selector);
}
}
}
18 changes: 16 additions & 2 deletions src/as3/mongo/db/collection/Collection.as
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,24 @@ package as3.mongo.db.collection

public function insert(document:Document):void
{
if (null == document)
throw new MongoError(MongoError.DOCUMENT_MUST_NOT_BE_NULL);
_validateDocumentInstance(document);

_db.insert(_name, document);
}

public function remove(selector:Document):void
{
_validateDocumentInstance(selector);

_db.remove(name, selector);
}

private function _validateDocumentInstance(document:Document):void
{
if (null == document)
throw new MongoError(MongoError.DOCUMENT_MUST_NOT_BE_NULL);
}


}
}
10 changes: 10 additions & 0 deletions src/as3/mongo/wire/Wire.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package as3.mongo.wire
import as3.mongo.error.MongoError;
import as3.mongo.wire.cursor.Cursor;
import as3.mongo.wire.messages.MessageFactory;
import as3.mongo.wire.messages.client.OpDelete;
import as3.mongo.wire.messages.client.OpInsert;
import as3.mongo.wire.messages.client.OpQuery;
import as3.mongo.wire.messages.database.FindOneOpReplyLoader;
Expand Down Expand Up @@ -129,11 +130,20 @@ package as3.mongo.wire
throw new MongoError(MongoError.SOCKET_NOT_CONNECTED);
}

// TODO: Write integration tests for this
public function insert(dbName:String, collectionName:String, document:Document):void
{
_checkIfSocketIsConnected();
const opInsert:OpInsert = messageFactory.makeSaveOpInsertMessage(dbName, collectionName, document);
_messenger.sendMessage(opInsert);
}

// TODO: Write integration tests for this
public function remove(dbName:String, collectionName:String, selector:Document):void
{
_checkIfSocketIsConnected();
const opDelete:OpDelete = messageFactory.makeRemoveOpDeleteMessage(dbName, collectionName, selector);
_messenger.sendMessage(opDelete);
}
}
}
6 changes: 6 additions & 0 deletions src/as3/mongo/wire/messages/MessageFactory.as
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package as3.mongo.wire.messages
{
import as3.mongo.db.document.Document;
import as3.mongo.wire.messages.client.OpDelete;
import as3.mongo.wire.messages.client.OpInsert;
import as3.mongo.wire.messages.client.OpQuery;

Expand Down Expand Up @@ -34,5 +35,10 @@ package as3.mongo.wire.messages
opInsert.addDocument(document);
return opInsert;
}

public function makeRemoveOpDeleteMessage(dbName:String, collectionName:String, selector:Document):OpDelete
{
return new OpDelete(_getFullCollectionName(dbName, collectionName), selector);
}
}
}
1 change: 0 additions & 1 deletion src/as3/mongo/wire/messages/client/OpInsert.as
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ package as3.mongo.wire.messages.client
msgHeader.updateMessageLength(byteArray);

byteArray.position = 0;
byteArray.endian = Endian.LITTLE_ENDIAN;
return byteArray;
}

Expand Down
45 changes: 45 additions & 0 deletions tests-src/flexUnitTests/as3/mongo/db/DB_removeTests.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package flexUnitTests.as3.mongo.db
{
import as3.mongo.db.document.Document;
import as3.mongo.wire.Wire;

import mockolate.received;
import mockolate.runner.MockolateRule;

import org.flexunit.assertThat;

public class DB_removeTests
{
[Rule]
public var mocks:MockolateRule = new MockolateRule();

[Mock(inject = "true", type = "nice")]
public var mockWire:Wire;

private var _db:TestDB;
private var _testCollectionName:String = "aCollection";
private var _testSelector:Document = new Document();
private var _testDatabaseName:String = "aDatabaseName";

[Before]
public function setUp():void
{
_db = new TestDB(_testDatabaseName, "host", 27017);
_db.mockWire = mockWire;
}

[After]
public function tearDown():void
{
_db = null;
}

[Test]
public function remove_aDocumentInstanceIsPassedIn_removedInvokedOnWire():void
{
_db.remove(_testCollectionName, _testSelector);

assertThat(mockWire, received().method("remove").args(_testDatabaseName, _testCollectionName, _testSelector).once());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package flexUnitTests.as3.mongo.db.collection
{
import as3.mongo.db.DB;
import as3.mongo.db.collection.Collection;
import as3.mongo.db.document.Document;

import mockolate.received;
import mockolate.runner.MockolateRule;

import org.flexunit.assertThat;

public class Collection_removeTests
{
[Rule]
public var mocks:MockolateRule = new MockolateRule();

[Mock(inject = "true", type = "nice")]
public var mockDB:DB;

private var _collection:Collection;

[Before]
public function setUp():void
{
_collection = new Collection("aCollection", mockDB);
}

[After]
public function tearDown():void
{
_collection = null;
}

[Test]
public function remove_aDocumentInstanceIsPassedIn_removeInvokedOnDB():void
{
const testDoc:Document = new Document();
_collection.remove(testDoc);

assertThat(mockDB, received().method("remove").args("aCollection", testDoc).once());
}

[Test(expects = "as3.mongo.error.MongoError")]
public function remove_aNullObjectIsPassedIn_throwsMongoError():void
{
_collection.remove(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package flexUnitTests.as3.mongo.wire.messages
{
import as3.mongo.db.document.Document;
import as3.mongo.wire.messages.MessageFactory;
import as3.mongo.wire.messages.client.OpDelete;

import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertTrue;

public class MessageFactory_makeRemoveOpDeleteMessageTests
{

private var _messageFactory:MessageFactory;
private var _opDelete:OpDelete;
private var _testDBName:String = "aDBName";
private var _testCollectionName:String = "aCollectionName";
private var _testSelector:Document = new Document();
private var _testFullCollectionName:String = "aDBName.aCollectionName";

[Before]
public function setUp():void
{
_messageFactory = new MessageFactory();

_opDelete = _messageFactory.makeRemoveOpDeleteMessage(_testDBName, _testCollectionName, _testSelector);
}

[After]
public function tearDown():void
{
_messageFactory = null;
}

[Test]
public function makeRemoveOpDeleteMessage_validInputs_returnsOpDeleteInstance():void
{
assertTrue(_opDelete is OpDelete);
}

[Test]
public function makeRemoveOpDeleteMessage_validInputs_fullCollectionNameCorrect():void
{
assertEquals(_testFullCollectionName, _opDelete.fullCollectionName);
}

[Test]
public function makeRemoveOpDeleteMessage_validInputs_selectorReferenceSet():void
{
assertEquals(_testSelector, _opDelete.selector);
}
}
}

0 comments on commit 3c28825

Please sign in to comment.