Jelajahi Sumber

Document updates and test cases

Jason Walton 11 tahun lalu
induk
melakukan
e75e2cfe90
2 mengubah file dengan 108 tambahan dan 0 penghapusan
  1. 73 0
      lib/pipeline/Document.js
  2. 35 0
      test/lib/pipeline/Document.js

+ 73 - 0
lib/pipeline/Document.js

@@ -22,6 +22,37 @@ var Value = require("./Value");
  **/
 klass.ID_PROPERTY_NAME = "_id";
 
+/**
+ * Return JSON representation of this Document
+ *
+ * @method toJson
+ * @returns JSON representation of this Document
+ **/
+ klass.toJson = function toJson(document){
+ 	return JSON.stringify(document);
+ };
+
+/**
+ * Return JSON representation of this Document
+ *
+ * @method toJsonWithMetaData
+ * @returns JSON representation of this Document, with MetaData
+ **/
+klass.toJsonWithMetaData = function toJsonWithMetaData(document){
+	//NOTE: DEVIATION FROM MONGO: Not sure how we are handling metadata
+	return JSON.stringify(document);
+};
+
+/**
+ * Return Document with MetaData from its JSON representation
+ *
+ * @method fromJsonWithMetaData
+ * @returns Document with MetaData
+ **/
+klass.fromJsonWithMetaData = function fromJsonWithMetaData(json){
+	return JSON.parse(json);
+};
+
 /**
  * Compare two documents.
  *
@@ -84,6 +115,48 @@ klass.clone = function(document){
 	return obj;
 };
 
+//NOTE: not implementing the sorter functions until we understand sorter needs better
+var _waitingOnSorter = function() {
+	//TODO: When working on sorter, decide if this is needed
+	throw new Error("Not implemented.");
+};
+
+klass.serializeForSorter = function(document) {
+	var buffer, size = 0, key;
+
+	//determine size for sorter??
+	//for (key in document) {
+    //	if (document.hasOwnProperty(key)) size++;
+	//};
+	//add size to buffer (don't count size field)
+	//buffer = (size-1).toString();
+
+	//add each field
+	//for (key in document) {
+	//	buffer += document[key]);
+	//};
+	//append text score?
+	//return buffer;
+
+	return JSON.stringify(document);
+};
+
+klass.deserializeForSorter = function(stringifiedDocument) {
+	//_waitingOnSorter();
+
+	//var document, size;
+	//size = int.parse(stringifiedDocument.split(",")[0]);
+	//document["size"] = size;
+	//document = JSON.parse(stringifiedDocument);
+
+	return JSON.parse(stringifiedDocument);
+};
+klass.memUsageForSorter = function(value) {
+	_waitingOnSorter();
+};
+klass.getOwned = function() {
+	return proto;
+};
 //	proto.addField = function addField(){ throw new Error("Instead of `Document#addField(key,val)` you should just use `obj[key] = val`"); }
 //	proto.setField = function addField(){ throw new Error("Instead of `Document#setField(key,val)` you should just use `obj[key] = val`"); }
 //  proto.getField = function getField(){ throw new Error("Instead of `Document#getField(key)` you should just use `var val = obj[key];`"); }

+ 35 - 0
test/lib/pipeline/Document.js

@@ -7,6 +7,25 @@ module.exports = {
 
 	"Document": {
 
+		"Json conversion": {
+			"convert to Json": function toJson() {
+				var aDocument = {"prop1":0},
+					result = Document.toJson(aDocument);
+				assert.equal(result, '{"prop1":0}');
+			},
+			"convert to Json with metadata": function toJsonWithMetaData() {
+				var aDocument = {"prop1": 0,"metadata":"stuff"},
+					result = Document.toJsonWithMetaData(aDocument);
+				assert.equal(result, '{"prop1":0,"metadata":"stuff"}');
+			},
+			"convert from Json": function fromJsonWithMetaData() {
+				var aDocumentString = '{\"prop1\":0,\"metadata\":1}',
+					jsonDocument = {"prop1":0,"metadata":1},
+					result = Document.fromJsonWithMetaData(aDocumentString);
+				assert.deepEqual(result, jsonDocument);
+			}
+		},
+
 		"compare 2 Documents": {
 
 			"should return 0 if Documents are identical": function compareDocumentsIdentical() {
@@ -63,6 +82,22 @@ module.exports = {
 				assert.equal(res.prop1, 17);
 				assert.equal(res.prop2, "a string");
 			}
+		},
+
+		"serialize and deserialize for sorter": {
+
+			"should return a string": function serializeDocument() {
+				var doc = {"prop1":1},
+					res = Document.serializeForSorter(doc);
+				assert.equal(res, "{\"prop1\":1}");
+			},
+
+			"should return a Document": function deserializeToDocument() {
+				var str = "{\"prop1\":1}",
+					doc = {"prop1":1},
+					res = Document.deserializeForSorter(str);
+				assert.deepEqual(res, doc);
+			}
 		}
 
 	}