Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 310 lines (242 sloc) 8.682 kb
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
require 'spec_helper'

describe "A Resource" do

  before(:all) do
    CloudKit.setup_storage_adapter unless CloudKit.storage_adapter
  end

  after(:each) do
    CloudKit.storage_adapter.clear
  end

  describe "on initialization" do

    before(:each) do
      @resource = CloudKit::Resource.new(
        CloudKit::URI.new('/items/123'),
        JSON.generate({:foo => 'bar'}),
        'http://eric.dolphy.info')
    end

    it "should know its uri" do
      @resource.uri.string.should == CloudKit::URI.new('/items/123').string
    end

    it "should know its json" do
      @resource.json.should == "{\"foo\":\"bar\"}"
    end

    it "should know its remote user" do
      @resource.remote_user.should == 'http://eric.dolphy.info'
    end

    it "should default its deleted status to false" do
      @resource.should_not be_deleted
    end

    it "should default its archived status to false" do
      @resource.should_not be_archived
    end

    it "should default its etag to nil" do
      @resource.etag.should be_nil
    end

    it "should default its last-modified date to nil" do
      @resource.last_modified.should be_nil
    end

    it "should know if it is current" do
      @resource.should be_current
    end

  end

  describe "on save" do

    before(:each) do
      @resource = CloudKit::Resource.new(
        CloudKit::URI.new('/items/123'),
        JSON.generate({:foo => 'bar'}),
        'http://eric.dolphy.info')
      @resource.save
    end

    it "should set its etag" do
      @resource.etag.should_not be_nil
    end

    it "should set its last modified date" do
      @resource.last_modified.should_not be_nil
    end

    it "should adjust its URI when adding to a resource collection" do
      resource = CloudKit::Resource.new(
        CloudKit::URI.new('/items'),
        JSON.generate({:foo => 'bar'}),
        'http://eric.dolphy.info')
      resource.save
      resource.uri.string.should_not == '/items'
    end

    it "should flatten its json structure for querying" do
      hash = CloudKit.storage_adapter.query.first
      JSON(hash['json']).keys.include?('foo').should be_true
    end

    it "should know it is current" do
      @resource.should be_current
    end

  end

  describe "on create" do

    def store_json(hash)
      CloudKit::Resource.create(
        CloudKit::URI.new('/items/123'),
        JSON.generate(hash),
        'http://eric.dolphy.info')
      CloudKit.storage_adapter.query { |q|
        q.add_condition 'uri', :eql, '/items/123'
      }
    end

    it "should save the resource" do
      result = store_json({:foo => 'bar'})
      result.size.should == 1
      result.first['json'].should == "{\"foo\":\"bar\"}"
    end

    it "should accept nested array values" do
      result = store_json({:foo => [1,2]})
      result.size.should == 1
      result.first['json'].should == '{"foo":[1,2]}'
    end

    it "should accept nested hash values" do
      result = store_json({:foo => {:bar => 'baz'}})
      result.size.should == 1
      result.first['json'].should == '{"foo":{"bar":"baz"}}'
    end

    it "should accept recursively nested array/hash values" do
      result = store_json({:foo => [1,{:bar => [2,3]}]})
      result.size.should == 1
      result.first['json'].should == '{"foo":[1,{"bar":[2,3]}]}'
    end

  end

  describe "on update" do

    before(:each) do
      @resource = CloudKit::Resource.create(
        CloudKit::URI.new('/items/123'),
        JSON.generate({:foo => 'bar'}),
        'http://eric.dolphy.info')
      @original_resource = @resource.dup
      now = Time.now
      Time.stub!(:now).and_return(now+1)
      @resource.update(JSON.generate({:foo => 'baz'}))
    end

    it "should version the resource" do
      @resource.versions.size.should == 2
      @resource.versions[-1].should be_archived
    end

    it "should set a new etag" do
      @resource.etag.should_not == @original_resource.etag
    end

    it "should set a new last modified date" do
      @resource.last_modified.should_not == @original_resource.last_modified
    end

    it "should fail on archived resource versions" do
      lambda {
        @resource.versions[-1].update({:foo => 'box'})
      }.should raise_error(CloudKit::HistoricalIntegrityViolation)
    end

    it "should fail on deleted resource versions" do
      lambda {
        @resource.delete
        @resource.update({:foo => 'box'})
      }.should raise_error(CloudKit::HistoricalIntegrityViolation)
    end

  end

  describe "on delete" do

    before(:each) do
      @resource = CloudKit::Resource.create(
        CloudKit::URI.new('/items/123'),
        JSON.generate({:foo => 'bar'}),
        'http://eric.dolphy.info')
      now = Time.now
      Time.stub!(:now).and_return(now+1)
      @resource.delete
    end

    it "should version the resource" do
      @resource.versions.size.should == 2
      @resource.versions[-1].should be_archived
    end

    it "should set the etag on the main resource to nil" do
      @resource.etag.should be_nil
    end

    it "should know it has been deleted" do
      @resource.deleted?.should be_true
    end

    it "should fail on archived resource versions" do
      lambda {
        @resource.versions[-1].update({:foo => 'box'})
      }.should raise_error(CloudKit::HistoricalIntegrityViolation)
    end

  end

  describe "with versions" do

    before(:each) do
      @resource = CloudKit::Resource.create(
        CloudKit::URI.new('/items/123'),
        JSON.generate({:foo => 'bar'}),
        'http://eric.dolphy.info')
      @resource_list = [@resource.dup]

      2.times { |i|
        now = Time.now
        Time.stub!(:now).and_return(now+1)
        @resource.update(JSON.generate({:foo => i}))
        @resource_list << @resource.dup
      }
      @resource_list.reverse!
    end

    it "should keep an ordered list of versions" do
      @resource.versions.map { |version| version.last_modified }.
        should == @resource_list.map { |version| version.last_modified }
    end

    it "should include the current version in the version list" do
      current_ts = @resource.last_modified
      @resource_list.map { |version| version.last_modified }.should include(current_ts)
    end

    it "should know its previous version" do
      @resource.previous_version.last_modified.should == @resource_list[1].last_modified
    end

    it "should know its previous versions" do
      expected_times = @resource_list[1..-1].map { |version| version.last_modified }
      @resource.previous_versions.map { |version| version.last_modified }.should == expected_times
    end

  end

  describe "when finding" do

    before(:each) do
      ['bar', 'baz'].each do |value|
        CloudKit::Resource.create(
          CloudKit::URI.new('/items'),
          JSON.generate({:foo => value}),
          "http://eric.dolphy.info/#{value}_user")
      end
      CloudKit::Resource.create(
        CloudKit::URI.new('/items'),
        JSON.generate({:foo => 'box'}),
        "http://eric.dolphy.info/bar_user")
    end

    describe "using #all" do

      it "should find matching resources" do
        result = CloudKit::Resource.all(
          :remote_user => 'http://eric.dolphy.info/bar_user')
        result.size.should == 2
        result.map { |item| item.remote_user.should == 'http://eric.dolphy.info/bar_user' }
      end

      it "should return all elements if no restrictions are given" do
        CloudKit::Resource.all.size.should == 3
      end

      it "should return an empty array if no resources are found" do
        CloudKit::Resource.all(:uri => 'fail').should be_empty
      end

      it "should find with query parameters referencing JSON elements" do
        resources = CloudKit::Resource.all(
          :collection_reference => '/items',
          :search => { :foo => 'bar' } )
        resources.size.should == 1
        resources.first.json.should == "{\"foo\":\"bar\"}"
      end

    end

    describe "on #first" do

      it "should find the first matching resource" do
        result = CloudKit::Resource.first(:remote_user => 'http://eric.dolphy.info/bar_user')
        result.should_not === Array
        result.remote_user.should == 'http://eric.dolphy.info/bar_user'
        result.parsed_json['foo'].should == 'box' # all listings are reverse ordered
      end

    end

    describe "on #current" do

      it "should find only current matching resources" do
        resource = CloudKit::Resource.first(:remote_user => 'http://eric.dolphy.info/bar_user')
        resource.update(JSON.generate({:foo => 'x'}))
        CloudKit::Resource.current(:collection_reference => '/items').size.should == 3
      end

    end

  end

end
Something went wrong with that request. Please try again.