File: serialization_spec.rb

package info (click to toggle)
ruby-ice-cube 0.16.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 7,823; makefile: 6
file content (65 lines) | stat: -rw-r--r-- 1,772 bytes parent folder | download
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
require 'active_support/time'
require File.dirname(__FILE__) + '/../spec_helper'

describe IceCube::Schedule do

  let(:start_time) { Time.now }
  let(:schedule) { IceCube::Schedule.new(start_time) }
  let(:yaml)     { described_class.dump(schedule) }

  describe "::dump(schedule)" do

    it "serializes a Schedule object as YAML string" do
      expect(yaml).to start_with "---\n"
    end

    context "with ActiveSupport::TimeWithZone" do
      let(:start_time) { Time.now.in_time_zone("America/Vancouver") }

      it "serializes time as a Hash" do
        hash = YAML.safe_load(yaml, permitted_classes: [Symbol, Time])
        expect(hash[:start_time][:time]).to eq start_time.utc
        expect(hash[:start_time][:zone]).to eq "America/Vancouver"
      end
    end

    [nil, ""].each do |blank|
      context "when schedule is #{blank.inspect}" do
        let(:schedule) { blank }

        it "returns #{blank.inspect}" do
          expect(yaml).to be blank
        end
      end
    end

  end

  describe "::load(yaml)" do
    let(:new_schedule) { described_class.load yaml }

    it "creates a new object from a YAML string" do
      expect(new_schedule.start_time.to_s).to eq schedule.start_time.to_s
    end

    context "with ActiveSupport::TimeWithZone" do
      let(:start_time) { Time.now.in_time_zone("America/Vancouver") }

      it "deserializes time from Hash" do
        expect(new_schedule.start_time).to eq start_time
        expect(new_schedule.start_time.time_zone).to eq start_time.time_zone
      end
    end

    [nil, ""].each do |blank|
      context "when yaml is #{blank.inspect}" do
        let(:yaml) { blank }

        it "returns #{blank.inspect}" do
          expect(new_schedule).to be blank
        end
      end
    end

  end
end