1 |
|
2 | var should = require("should");
|
3 | var ObjectID = require("../");
|
4 |
|
5 |
|
6 | describe("ObjectIDs", function() {
|
7 | it("should construct with no arguments", function() {
|
8 | var o = new ObjectID();
|
9 | o.should.be.instanceof(ObjectID);
|
10 | });
|
11 |
|
12 | it("should have an `id` property", function() {
|
13 | var o = new ObjectID();
|
14 | o.should.have.property("id");
|
15 | o.id.should.have.length(12);
|
16 | ObjectID.isValid(o.id).should.not.be.ok;
|
17 | });
|
18 |
|
19 | it("should have a `str` property", function() {
|
20 | var o = new ObjectID();
|
21 | o.should.have.property("str");
|
22 | o.str.should.have.length(24);
|
23 | ObjectID.isValid(o.str).should.be.ok;
|
24 | });
|
25 |
|
26 | it("should construct with a `time` argument", function() {
|
27 | var time = 1414093117;
|
28 | var o = new ObjectID(time);
|
29 | o.should.be.instanceof(ObjectID);
|
30 | o.str.substr(0,8).should.eql("5449593d");
|
31 | });
|
32 |
|
33 | it("should construct with an `array` argument", function() {
|
34 | var array = [ 84, 73, 90, 217, 76, 147, 71, 33, 237, 231, 109, 144 ];
|
35 | var o = new ObjectID(array);
|
36 | o.should.be.instanceof(ObjectID);
|
37 | o.str.should.eql("54495ad94c934721ede76d90");
|
38 | });
|
39 |
|
40 | it("should construct with a `buffer` argument", function() {
|
41 | var buffer = new Buffer([ 84, 73, 90, 217, 76, 147, 71, 33, 237, 231, 109, 144 ]);
|
42 | var o = new ObjectID(buffer);
|
43 | o.should.be.instanceof(ObjectID);
|
44 | o.str.should.eql("54495ad94c934721ede76d90");
|
45 | });
|
46 |
|
47 | it("should construct with a `hexString` argument", function() {
|
48 | var hexString = "54495ad94c934721ede76d90";
|
49 | var o = new ObjectID(hexString);
|
50 | o.should.be.instanceof(ObjectID);
|
51 | o.str.should.eql(hexString);
|
52 | });
|
53 |
|
54 | it("should construct with a `idString` argument", function() {
|
55 | var idString = "TIZÙLG!íçm";
|
56 | var o = new ObjectID(idString);
|
57 | o.should.be.instanceof(ObjectID);
|
58 | o.id.should.eql(idString);
|
59 | });
|
60 |
|
61 | it("should construct with `ObjectID.createFromTime(time)` and should have 0's at the end", function() {
|
62 | var time = 1414093117;
|
63 | var o = ObjectID.createFromTime(time);
|
64 | o.should.be.instanceof(ObjectID);
|
65 | o.str.should.eql("5449593d0000000000000000");
|
66 | });
|
67 |
|
68 | it("should construct with `ObjectID.createFromHexString(hexString)`", function() {
|
69 | var hexString = "54495ad94c934721ede76d90";
|
70 | var o = ObjectID.createFromHexString(hexString);
|
71 | o.should.be.instanceof(ObjectID);
|
72 | o.str.should.eql(hexString);
|
73 | });
|
74 |
|
75 | it("should correctly retrieve timestamp", function() {
|
76 | var testDate = new Date();
|
77 | var object1 = new ObjectID();
|
78 | var seconds1 = Math.floor(testDate.getTime()/1000);
|
79 | var seconds2 = Math.floor(object1.getTimestamp().getTime()/1000);
|
80 | seconds1.should.eql(seconds2);
|
81 | });
|
82 |
|
83 | it("should validate valid hex strings", function() {
|
84 | ObjectID.isValid("54495ad94c934721ede76d90").should.be.ok;
|
85 | ObjectID.isValid("aaaaaaaaaaaaaaaaaaaaaaaa").should.be.ok;
|
86 | ObjectID.isValid("AAAAAAAAAAAAAAAAAAAAAAAA").should.be.ok;
|
87 | ObjectID.isValid("000000000000000000000000").should.be.ok;
|
88 | });
|
89 |
|
90 | it("should validate legit ObjectID objects", function() {
|
91 | var o = new ObjectID();
|
92 | ObjectID.isValid(o).should.be.ok;
|
93 | });
|
94 |
|
95 | it("should invalidate bad strings", function() {
|
96 | ObjectID.isValid().should.not.be.ok;
|
97 | ObjectID.isValid(null).should.not.be.ok;
|
98 | ObjectID.isValid({}).should.not.be.ok;
|
99 | ObjectID.isValid([]).should.not.be.ok;
|
100 | ObjectID.isValid(true).should.not.be.ok;
|
101 | ObjectID.isValid("invalid").should.not.be.ok;
|
102 | ObjectID.isValid("").should.not.be.ok;
|
103 | ObjectID.isValid("zzzzzzzzzzzzzzzzzzzzzzzz").should.not.be.ok;
|
104 | ObjectID.isValid("54495-ad94c934721ede76d9").should.not.be.ok;
|
105 | });
|
106 |
|
107 | it("should evaluate equality with .equals()", function() {
|
108 | var id1 = ObjectID();
|
109 | var id2 = ObjectID(id1.str);
|
110 | (id1.equals(id2)).should.be.true;
|
111 | });
|
112 |
|
113 | it("should evaluate equality with via deepEqual", function() {
|
114 | var id1 = ObjectID();
|
115 | var id2 = ObjectID(id1.str);
|
116 | id1.should.eql(id2);
|
117 |
|
118 | var id3 = ObjectID();
|
119 | id1.should.not.eql(id3, "id1 is not the same as id3");
|
120 | });
|
121 |
|
122 | it("should generate valid hex strings", function() {
|
123 | var h = ObjectID.generate();
|
124 | ObjectID.isValid(h).should.be.ok;
|
125 | });
|
126 |
|
127 | it("should convert to a hex string for JSON.stringify", function() {
|
128 | var hexString = "54495ad94c934721ede76d90";
|
129 | var o = {o:new ObjectID(hexString)};
|
130 | var strngd = JSON.stringify(o);
|
131 | strngd.should.eql('{"o":"54495ad94c934721ede76d90"}');
|
132 | });
|
133 |
|
134 | it("should convert to a hex string for ObjectID.toString()", function() {
|
135 | var hexString = "54495ad94c934721ede76d90";
|
136 | var o = new ObjectID(hexString);
|
137 | o.toString().should.eql("54495ad94c934721ede76d90");
|
138 | });
|
139 |
|
140 | it("should throw and error if constructing with an invalid string", function() {
|
141 | (function(){
|
142 | var o = new ObjectID("tttttttttttttttttttttttt");
|
143 | }).should.throw();
|
144 | });
|
145 |
|
146 | it("should should set/get a custom machineID", function() {
|
147 | var tests = [
|
148 | { machineID: 0x1, expected: 0x000001 },
|
149 | { machineID: 0x123456, expected: 0x123456 },
|
150 | { machineID: 0x0987654321, expected: 0x654321 },
|
151 |
|
152 | { machineID: 'TIZÙLG!íçm', expected: 0xe41e05 },
|
153 |
|
154 | { machineID: '01', expected: 0x000001 },
|
155 | { machineID: '01a2b3', expected: 0x01a2b3 },
|
156 | { machineID: '7f6e5d4c3b2a0', expected: 0xc3b2a0 },
|
157 | ];
|
158 |
|
159 | tests.forEach(function(test) {
|
160 | ObjectID.setMachineID(test.machineID);
|
161 |
|
162 | ObjectID.getMachineID().should.eql(test.expected);
|
163 | });
|
164 | });
|
165 |
|
166 | it("should work with a specific machineID", function() {
|
167 | var testPattern = /^([0-9a-f]{8})([0-9a-f]{6})([0-9a-f]{4})([0-9a-f]{6})$/i;
|
168 |
|
169 | var mid = parseInt(Math.random() * 0xFFFFFF, 10);
|
170 | ObjectID.setMachineID(mid);
|
171 |
|
172 | var o = ObjectID.generate();
|
173 | var result = o.match(testPattern);
|
174 |
|
175 | result[2].should.eql(mid.toString(16));
|
176 | });
|
177 |
|
178 | it("should not throw an error for objects without toString", function() {
|
179 | var obj = Object.create({}, { toString: { value: false, writeable: false } });
|
180 | obj.toString.should.not.be.ok;
|
181 | ObjectID.isValid(obj).should.not.be.ok;
|
182 | });
|
183 | });
|
184 |
|