require 'spec_helper' module Github module Archive describe Event do after(:each) do Event.delete_all end let(:expected_results) {{"url1"=>2, "url2"=>1, "url3"=>1}} let(:json_data){ {"payload"=> {"head"=>"6234f9fcb61c7b03782cf32e232ae1d90bf3c772", "size"=>1, "shas"=> [["6234f9fcb61c7b03782cf32e232ae1d90bf3c772", "dev+commit-bot@koideploy.com", "Display photo \"nuances\" from \"paul bica\"", "Koi Deploy Commit Bot", true]], "ref"=>"refs/heads/master"}, "created_at"=>"2012-12-10T12:00:29-08:00", "repository"=> {"description"=> "Deploy your Rails app straight from GitHub to Heroku in a single click, or continuously triggered by your CI. This is an example application for continuous deployment via Travis CI. ", "stargazers"=>1, "owner"=>"koideploy", "url"=>"https://github.com/koideploy/showcase", "has_downloads"=>true, "language"=>"Ruby", "pushed_at"=>"2012-12-10T12:00:21-08:00", "forks"=>0, "has_issues"=>true, "organization"=>"koideploy", "fork"=>false, "size"=>4368, "has_wiki"=>false, "name"=>"showcase", "id"=>6306116, "homepage"=>"http://showcase.koideploy.com", "private"=>false, "open_issues"=>0, "created_at"=>"2012-10-19T23:38:50-07:00", "watchers"=>1}, "actor_attributes"=> {"login"=>"koideploy-commit-bot", "name"=>"KoiDeploy Commit Bot", "location"=>"Brisbane, Australia", "company"=>"KoiDeploy", "blog"=>"https://koideploy.com", "type"=>"User", "gravatar_id"=>"a1aea43cf721ab953474a0e4c699bc90"}, "url"=> "https://github.com/koideploy/showcase/compare/ec6f77514f...6234f9fcb6", "public"=>true, "actor"=>"koideploy-commit-bot", "type"=>"PushEvent"} } context "when looking up events" do it "returns events in the correct time range" do FactoryGirl.create(:event, gh_created_at: 3.days.ago) FactoryGirl.create(:event, gh_created_at: 2.days.ago) FactoryGirl.create(:event, gh_created_at: 1.days.ago) FactoryGirl.create(:event, gh_created_at: DateTime.now) Event.count.should eq 4 Event.in_time_range(2.days.ago..1.days.ago).count.should eq 2 end it "returns events with the correct type" do FactoryGirl.create(:event, event_type: "test_event") FactoryGirl.create(:event, event_type: "test_event2") Event.with_event_type("test_event").count.should eq 1 end end context "when creating events" do it "creates repo events with json" do Event.create_with_json(json_data) event = Event.first event.event_type.should == "PushEvent" event.gh_created_at.to_s.should == "2012-12-10 12:00:29 UTC" event.url.should == "https://github.com/koideploy/showcase" end ["GistEvent", "FollowEvent" ,"TeamAddEvent"].each do |event_str| it "does not create for #{event_str} events" do test_json = json_data.dup test_json["type"] = event_str Event.create_with_json(test_json) Event.count.should eq 0 end end end context "when getting results" do it "returns a result hash with the correct results" do FactoryGirl.create(:event, gh_created_at: 3.days.ago, event_type: "test_event1", url: "url1") FactoryGirl.create(:event, gh_created_at: 2.days.ago, event_type: "test_event1", url: "url1") FactoryGirl.create(:event, gh_created_at: 2.days.ago, event_type: "test_event1", url: "url2") FactoryGirl.create(:event, gh_created_at: 2.days.ago, event_type: "test_event1", url: "url3") FactoryGirl.create(:event, gh_created_at: 1.days.ago, event_type: "test_event3", url: "url3") FactoryGirl.create(:event, gh_created_at: DateTime.now, event_type: "test_event3", url: "url4") range = (3.days.ago..2.days.ago) results = Event.results_for_range_and_type(range, "test_event1", 4) results.should == expected_results end end end end end