1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
|
# rubocop:todo all
# Copyright (C) 2009-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "spec_helper"
require "yaml"
describe BSON::ObjectId do
describe "#==" do
context "when data is identical" do
let(:time) do
Time.now
end
let(:object_id) do
described_class.from_time(time)
end
let(:other_id) do
described_class.from_time(time)
end
it "returns true" do
expect(object_id).to eq(other_id)
end
end
context "when the data is different" do
let(:time) do
Time.now
end
let(:object_id) do
described_class.from_time(time)
end
it "returns false" do
expect(object_id).to_not eq(described_class.new)
end
end
context "when other is not an object id" do
it "returns false" do
expect(described_class.new).to_not eq(nil)
end
end
end
describe "#===" do
let(:object_id) do
described_class.new
end
context "when comparing with another object id" do
context "when the data is equal" do
let(:other) do
described_class.from_string(object_id.to_s)
end
it "returns true" do
expect(object_id === other).to be true
end
end
context "when the data is not equal" do
let(:other) do
described_class.new
end
it "returns false" do
expect(object_id === other).to be false
end
end
end
context "when comparing to an object id class" do
it "returns false" do
expect(object_id === BSON::ObjectId).to be false
end
end
context "when comparing with a string" do
context "when the data is equal" do
let(:other) do
object_id.to_s
end
it "returns true" do
expect(object_id === other).to be true
end
end
context "when the data is not equal" do
let(:other) do
described_class.new.to_s
end
it "returns false" do
expect(object_id === other).to be false
end
end
end
context "when comparing with a non string or object id" do
it "returns false" do
expect(object_id === "test").to be false
end
end
context "when comparing with a non object id class" do
it "returns false" do
expect(object_id === String).to be false
end
end
end
describe "#<" do
let(:object_id) do
described_class.from_time(Time.utc(2012, 1, 1))
end
let(:other_id) do
described_class.from_time(Time.utc(2012, 1, 30))
end
context "when the generation time before the other" do
it "returns true" do
expect(object_id < other_id).to be true
end
end
context "when the generation time is after the other" do
it "returns false" do
expect(other_id < object_id).to be false
end
end
end
describe "#>" do
let(:object_id) do
described_class.from_time(Time.utc(2012, 1, 1))
end
let(:other_id) do
described_class.from_time(Time.utc(2012, 1, 30))
end
context "when the generation time before the other" do
it "returns false" do
expect(object_id > other_id).to be false
end
end
context "when the generation time is after the other" do
it "returns true" do
expect(other_id > object_id).to be true
end
end
end
describe "#<=>" do
let(:object_id) do
described_class.from_time(Time.utc(2012, 1, 1))
end
let(:other_id) do
described_class.from_time(Time.utc(2012, 1, 30))
end
context "when the generation time before the other" do
it "returns -1" do
expect(object_id <=> other_id).to eq(-1)
end
end
context "when the generation time is after the other" do
it "returns false" do
expect(other_id <=> object_id).to eq(1)
end
end
end
describe "#as_json" do
let(:object) do
described_class.new
end
it "returns the object id as a string" do
expect(object.as_json).to eq(object.to_s)
end
it_behaves_like "a JSON serializable object"
end
describe "#as_extended_json" do
let(:object) do
described_class.new
end
it "returns the object id with $oid key" do
expect(object.as_extended_json).to eq({ "$oid" => object.to_s })
end
end
describe "::BSON_TYPE" do
it "returns 0x07" do
expect(BSON::ObjectId::BSON_TYPE).to eq(7.chr)
end
end
describe "#bson_type" do
let(:code) do
described_class.new
end
it "returns 0x0D" do
expect(code.bson_type).to eq(BSON::ObjectId::BSON_TYPE)
end
end
describe "#eql" do
context "when data is identical" do
let(:time) do
Time.now
end
let(:object_id) do
described_class.from_time(time)
end
let(:other_id) do
described_class.from_time(time)
end
it "returns true" do
expect(object_id).to eql(other_id)
end
end
context "when the data is different" do
let(:time) do
Time.now
end
let(:object_id) do
described_class.from_time(time)
end
it "returns false" do
expect(object_id).to_not eql(described_class.new)
end
end
context "when other is not an object id" do
it "returns false" do
expect(described_class.new).to_not eql(nil)
end
end
end
describe ".from_string" do
context "when the string is valid" do
let(:string) do
"4e4d66343b39b68407000001"
end
let(:object_id) do
described_class.from_string(string)
end
it "initializes with the string's bytes" do
expect(object_id.to_s).to eq(string)
end
end
context "when the string is not valid" do
it "raises an error" do
expect {
described_class.from_string("asadsf")
}.to raise_error(BSON::Error::InvalidObjectId)
end
end
end
describe ".from_time" do
context "when no unique option is provided" do
let(:time) do
Time.at((Time.now.utc - 64800).to_i).utc
end
let(:object_id) do
described_class.from_time(time)
end
it "sets the generation time" do
expect(object_id.generation_time).to eq(time)
end
it "does not include process or sequence information" do
expect(object_id.to_s =~ /\A[0-9a-f]{8}[0]{16}\Z/).to be_truthy
end
end
context "when a unique option is provided" do
let(:time) do
Time.at((Time.now.utc - 64800).to_i).utc
end
let(:object_id) do
described_class.from_time(time, :unique => true)
end
let(:non_unique) do
described_class.from_time(time, :unique => true)
end
it "creates a new unique object id" do
expect(object_id).to_not eq(non_unique)
end
end
end
describe "#generation_time" do
let(:time) do
Time.utc(2013, 1, 1)
end
let(:object_id) do
described_class.from_time(time)
end
it "returns the generation time" do
expect(object_id.generation_time).to eq(time)
end
end
describe "#hash" do
let(:object_id) do
described_class.new
end
it "returns a hash of the raw bytes" do
expect(object_id.hash).to eq(object_id.to_bson.to_s.hash)
end
end
describe "#initialize" do
it "does not generate duplicate ids" do
100000.times do
expect(BSON::ObjectId.new).to_not eq(BSON::ObjectId.new)
end
end
end
describe "#clone" do
context "when the data has not been generated yet" do
let!(:object_id) do
described_class.new
end
let!(:clone) do
object_id.clone
end
it "generates and copies the data" do
expect(clone).to eq(object_id)
end
end
context "when the data has been generated" do
let!(:object_id) do
described_class.new
end
let(:clone) do
object_id.clone
end
before do
object_id.to_s
end
it "copies the data" do
expect(clone).to eq(object_id)
end
end
end
describe "#inspect" do
let(:object_id) do
described_class.new
end
it "returns the inspection with the object id to_s" do
expect(object_id.inspect).to eq("BSON::ObjectId('#{object_id.to_s}')")
end
it "returns a string that evaluates into an equivalent object id" do
expect(eval object_id.inspect).to eq object_id
end
end
describe ".legal?" do
context "when the string is too short to be an object id" do
it "returns false" do
expect(described_class).to_not be_legal("a" * 23)
end
end
context "when the string contains invalid hex characters" do
it "returns false" do
expect(described_class).to_not be_legal("y" + "a" * 23)
end
end
context "when the string is a valid object id" do
it "returns true" do
expect(described_class).to be_legal("a" * 24)
end
end
context "when the string contains newlines" do
it "returns false" do
expect(described_class).to_not be_legal("\n\n" + "a" * 24 + "\n\n")
end
end
context "when checking against another object id" do
let(:object_id) do
described_class.new
end
it "returns true" do
expect(described_class).to be_legal(object_id)
end
end
end
describe "#marshal_dump" do
let(:object_id) do
described_class.new
end
let(:dumped) do
Marshal.dump(object_id)
end
it "dumps the raw bytes data" do
expect(Marshal.load(dumped)).to eq(object_id)
end
end
describe "#marshal_load" do
context "when the object id was dumped in the old format" do
let(:legacy) do
"\x04\bo:\x13BSON::ObjectId\x06:\n" +
"@data[\x11iUi\x01\xE2i,i\x00i\x00i\x00i\x00i\x00i\x00i\x00i\x00i\x00"
end
let(:object_id) do
Marshal.load(legacy)
end
let(:expected) do
described_class.from_time(Time.utc(2013, 1, 1))
end
it "properly loads the object id" do
expect(object_id).to eq(expected)
end
it "removes the bad legacy data" do
object_id.to_bson
expect(object_id.instance_variable_get(:@data)).to be_nil
end
end
end
describe "#to_bson/#from_bson" do
let(:time) { Time.utc(2013, 1, 1) }
let(:type) { 7.chr }
let(:obj) { described_class.from_time(time) }
let(:bson) { obj.to_bson.to_s }
it_behaves_like "a bson element"
it_behaves_like "a serializable bson element"
it_behaves_like "a deserializable bson element"
end
describe "#to_s" do
let(:time) do
Time.utc(2013, 1, 1)
end
let(:expected) do
"50e227000000000000000000"
end
let(:object_id) do
described_class.from_time(time)
end
it "returns a hex string representation of the id" do
expect(object_id.to_s).to eq(expected)
end
it "returns the string in UTF-8" do
expect(object_id.to_s.encoding).to eq(Encoding.find(BSON::UTF8))
end
it "converts to a readable yaml string" do
expect(YAML.dump(object_id.to_s)).to include(expected)
end
end
context "when the class is loaded" do
let(:registered) do
BSON::Registry.get(BSON::ObjectId::BSON_TYPE, 'field')
end
it "registers the type" do
expect(registered).to eq(described_class)
end
end
context "when the ids are used as keys" do
let(:object_id) do
described_class.new
end
let(:hash) do
{ object_id => 1 }
end
it "raises an exception on serialization" do
expect {
hash.to_bson
}.to raise_error(BSON::Error::InvalidKey)
end
end
context 'when the counter wraps' do
before do
BSON::ObjectId._generator.reset_counter(0xFFFFFF)
end
let(:before) { BSON::ObjectId.new }
let(:after) { BSON::ObjectId.new }
it 'resets the counter portion to 0' do
expect(before._counter_part).to be == "ffffff"
expect(after._counter_part).to be == "000000"
end
end
context 'when the timestamp is larger than a 32-bit integer' do
let(:distant_future) { Time.at(2 ** 32) }
before do
allow(Time).to receive(:now).and_return(distant_future)
end
let(:object_id) { BSON::ObjectId.new }
it 'wraps the timestamp to 0' do
expect(object_id.to_time).to be == Time.at(0)
end
end
context 'when fork changes the pid' do
before do
skip 'requires Process.fork' unless Process.respond_to?(:fork)
end
let(:parent_id) { BSON::ObjectId.new }
let(:child_id) { Utils.perform_in_child { BSON::ObjectId.new } }
it 'changes the process portion of the object-id' do
expect(child_id._process_part).not_to be == parent_id._process_part
end
end
end
|