From 07bc9854e978245c05b4512875b4536df69fb5ec Mon Sep 17 00:00:00 2001 From: hirakiuc Date: Sat, 15 Jul 2017 14:20:59 +0900 Subject: [PATCH] add testcases --- lib/tinybucket/model/project.rb | 19 +++- lib/tinybucket/model/team.rb | 14 ++- lib/tinybucket/resource.rb | 2 +- lib/tinybucket/resource/projects.rb | 49 ++++++++++ lib/tinybucket/resource/team/projects.rb | 28 ------ .../teams/test_team/projects/get.json | 89 +++++++++++++++++++ .../teams/test_team/projects/myprj/get.json | 41 +++++++++ spec/lib/tinybucket/api/projects_api_spec.rb | 29 ++++++ spec/lib/tinybucket/model/project_spec.rb | 22 +++++ spec/lib/tinybucket/model/team_spec.rb | 25 +++++- spec/lib/tinybucket/resource/projects_spec.rb | 42 +++++++++ 11 files changed, 325 insertions(+), 35 deletions(-) create mode 100644 lib/tinybucket/resource/projects.rb delete mode 100644 lib/tinybucket/resource/team/projects.rb create mode 100644 spec/fixtures/teams/test_team/projects/get.json create mode 100644 spec/fixtures/teams/test_team/projects/myprj/get.json create mode 100644 spec/lib/tinybucket/api/projects_api_spec.rb create mode 100644 spec/lib/tinybucket/model/project_spec.rb create mode 100644 spec/lib/tinybucket/resource/projects_spec.rb diff --git a/lib/tinybucket/model/project.rb b/lib/tinybucket/model/project.rb index 82abf8f..44cf9e8 100644 --- a/lib/tinybucket/model/project.rb +++ b/lib/tinybucket/model/project.rb @@ -4,8 +4,23 @@ module Tinybucket module Model class Project < Base acceptable_attributes \ - :description, :links, :uuid, :created_on, - :key, :updated_on, :is_private, :name + :type, :description, :links, :uuid, :created_on, + :key, :updated_on, :is_private, :name, :owner + + # Update this project + # + # @param _params [Hash] + # @raise [NotImplementedError] to be implemented + def update(_params) + raise NotImplementedError + end + + # Destroy this project + # + # @raise [NotImplementedError] to be implemented. + def destroy + raise NotImplementedError + end end end end diff --git a/lib/tinybucket/model/team.rb b/lib/tinybucket/model/team.rb index 739d9b7..903d3dc 100644 --- a/lib/tinybucket/model/team.rb +++ b/lib/tinybucket/model/team.rb @@ -59,7 +59,15 @@ def following(options = {}) # @param options [Hash] # @return [Tinybucket::Resource::Team::Project] def projects(options = {}) - Tinybucket::Resource::Team::Projects.new(username, options) + projects_resource(options) + end + + # Get the project + # + # @param project_key [String] + # @return [Tinybucket::Model::Project] + def project(project_key, options = {}) + projects_resource().find(project_key, options) end # Get this team's repositories. @@ -72,6 +80,10 @@ def repos(options = {}) private + def projects_resource(options = {}) + Tinybucket::Resource::Projects.new(username, options) + end + def team_api create_api('Team') end diff --git a/lib/tinybucket/resource.rb b/lib/tinybucket/resource.rb index df15f45..8ef89cf 100644 --- a/lib/tinybucket/resource.rb +++ b/lib/tinybucket/resource.rb @@ -11,6 +11,7 @@ module Resource :Commits, :Forks, :OwnersRepos, + :Projects, :PublicRepos, :PullRequests, :Repos, @@ -28,7 +29,6 @@ module Team :Followers, :Following, :Members, - :Projects, :Repos ].each do |klass_name| autoload klass_name diff --git a/lib/tinybucket/resource/projects.rb b/lib/tinybucket/resource/projects.rb new file mode 100644 index 0000000..07886de --- /dev/null +++ b/lib/tinybucket/resource/projects.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Tinybucket + module Resource + class Projects < Base + attr_accessor :owner + + # Initialize + # + # @param owner_name [String] + # @param options [Hash] + def initialize(owner_name, options = {}) + @owner = owner_name + @args = [options] + end + + # Find the project + # + # @param project_key [String] + # @param options [Hash] + # @return [Tinybucket::Model::Project] + def find(project_key, options = {}) + projects_api.find(project_key, options) + end + + # Create a new project + # + # NOTE: Not Implemented yet. + # + # @param params [Hash] + # @raise [NotImplementedError] to be implemented + def create(params) + raise NotImplementedError + end + + private + + def projects_api + create_api('Projects').tap do |api| + api.owner = @owner + end + end + + def enumerator + create_enumerator(projects_api, :list, *@args) + end + end + end +end diff --git a/lib/tinybucket/resource/team/projects.rb b/lib/tinybucket/resource/team/projects.rb deleted file mode 100644 index 89c0ed6..0000000 --- a/lib/tinybucket/resource/team/projects.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -module Tinybucket - module Resource - module Team - class Projects < Base - attr_accessor :owner - - def initialize(owner, options = {}) - @owner = owner - @args = [options] - end - - private - - def projects_api - create_api('Projects').tap do |api| - api.owner = owner - end - end - - def enumerator - create_enumerator(projects_api, :list, *@args) - end - end - end - end -end diff --git a/spec/fixtures/teams/test_team/projects/get.json b/spec/fixtures/teams/test_team/projects/get.json new file mode 100644 index 0000000..94b0f23 --- /dev/null +++ b/spec/fixtures/teams/test_team/projects/get.json @@ -0,0 +1,89 @@ +{ + "pagelen": 10, + "values": [ + { + "uuid": "{45d24c3d-efa0-4c2a-be4e-78c1b30a30d0}", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/teams/test_team/projects/TEST" + }, + "html": { + "href": "https://bitbucket.org/account/user/test_team/projects/TEST" + }, + "repositories": { + "href": "https://api.bitbucket.org/2.0/repositories/test_team?q='project.key=\"TEST\"'" + }, + "avatar": { + "href": "https://bitbucket.org/account/user/test_team/projects/TEST/avatar/32" + } + }, + "description": null, + "created_on": "2016-03-19T03:34:27.792230+00:00", + "key": "TEST", + "owner": { + "username": "test_team", + "display_name": "test team", + "type": "team", + "uuid": "{e0f17982-effb-43eb-8589-7bacabb37cab}", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/teams/test_team" + }, + "html": { + "href": "https://bitbucket.org/test_team/" + }, + "avatar": { + "href": "https://bitbucket.org/account/test_team/avatar/32/" + } + } + }, + "updated_on": "2016-03-19T03:34:27.792256+00:00", + "type": "project", + "is_private": false, + "name": "test" + }, + { + "uuid": "{59278b96-1238-4418-a2b9-8f438234ae45}", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/teams/test_team/projects/PROJ" + }, + "html": { + "href": "https://bitbucket.org/account/user/test_team/projects/PROJ" + }, + "repositories": { + "href": "https://api.bitbucket.org/2.0/repositories/test_team?q='project.key=\"PROJ\"'" + }, + "avatar": { + "href": "https://bitbucket.org/account/user/test_team/projects/PROJ/avatar/32" + } + }, + "description": "Project created by Bitbucket for test", + "created_on": "2015-12-04T02:27:25.909083+00:00", + "key": "PROJ", + "owner": { + "username": "test_team", + "display_name": "test team", + "type": "team", + "uuid": "{e0f17982-effb-41cb-8589-7bacabb37cab}", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/teams/test_team" + }, + "html": { + "href": "https://bitbucket.org/test_team/" + }, + "avatar": { + "href": "https://bitbucket.org/account/test_team/avatar/32/" + } + } + }, + "updated_on": "2015-12-04T02:27:25.909123+00:00", + "type": "project", + "is_private": false, + "name": "Untitled project" + } + ], + "page": 1, + "size": 2 +} diff --git a/spec/fixtures/teams/test_team/projects/myprj/get.json b/spec/fixtures/teams/test_team/projects/myprj/get.json new file mode 100644 index 0000000..1cfc3c4 --- /dev/null +++ b/spec/fixtures/teams/test_team/projects/myprj/get.json @@ -0,0 +1,41 @@ +{ + "uuid": "{45234c3d-efa0-4c2a-be4e-78c1b30a30d0}", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/teams/test_team/projects/myprj" + }, + "html": { + "href": "https://bitbucket.org/account/user/test_team/projects/myprj" + }, + "repositories": { + "href": "https://api.bitbucket.org/2.0/repositories/test_team?q='project.key=\"myprj\"'" + }, + "avatar": { + "href": "https://bitbucket.org/account/user/test_team/projects/myprj/avatar/32" + } + }, + "description": null, + "created_on": "2016-03-19T03:34:27.792230+00:00", + "key": "myprj", + "owner": { + "username": "test_team", + "display_name": "test team", + "type": "team", + "uuid": "{e0f17982-efab-43cb-8589-7bacabb37cab}", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/teams/test_team" + }, + "html": { + "href": "https://bitbucket.org/test_team/" + }, + "avatar": { + "href": "https://bitbucket.org/account/test_team/avatar/32/" + } + } + }, + "updated_on": "2016-03-19T03:34:27.792256+00:00", + "type": "project", + "is_private": false, + "name": "myprj" +} diff --git a/spec/lib/tinybucket/api/projects_api_spec.rb b/spec/lib/tinybucket/api/projects_api_spec.rb new file mode 100644 index 0000000..7428174 --- /dev/null +++ b/spec/lib/tinybucket/api/projects_api_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +RSpec.describe Tinybucket::Api::ProjectsApi do + include ApiResponseMacros + + let(:api) { Tinybucket::Api::ProjectsApi.new } + let(:owner) { 'test_team' } + let(:request_path) { nil } + before do + api.owner = owner + stub_apiresponse(:get, request_path) if request_path + end + + it { expect(api).to be_a_kind_of(Tinybucket::Api::BaseApi) } + + describe 'list' do + subject { api.list() } + let(:request_path) { '/teams/test_team/projects/' } + it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) } + end + + describe 'find' do + let(:project_key) { 'myprj' } + subject { api.find(project_key) } + + let(:request_path) { "/teams/test_team/projects/#{project_key}" } + it { expect(subject).to be_an_instance_of(Tinybucket::Model::Project) } + end +end diff --git a/spec/lib/tinybucket/model/project_spec.rb b/spec/lib/tinybucket/model/project_spec.rb new file mode 100644 index 0000000..ef5fbd9 --- /dev/null +++ b/spec/lib/tinybucket/model/project_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +RSpec.describe Tinybucket::Model::Project do + include ApiResponseMacros + + let(:model_json) { load_json_fixture('teams/test_team/projects/myprj/get') } + let(:instance) { Tinybucket::Model::Project.new(model_json) } + + it_behaves_like 'model has acceptable_attributes', + Tinybucket::Model::Project, + load_json_fixture('teams/test_team/projects/myprj/get') + + describe '#update' do + subject { instance.update({}) } + it { expect { subject }.to raise_error(NotImplementedError) } + end + + describe '#destroy' do + subject { instance.destroy() } + it { expect { subject }.to raise_error(NotImplementedError) } + end +end diff --git a/spec/lib/tinybucket/model/team_spec.rb b/spec/lib/tinybucket/model/team_spec.rb index d3b8abc..beebdf7 100644 --- a/spec/lib/tinybucket/model/team_spec.rb +++ b/spec/lib/tinybucket/model/team_spec.rb @@ -11,9 +11,9 @@ let(:teamname) { 'test_team' } let(:model) do - m = Tinybucket::Model::Team.new(model_json) - m.username = teamname - m + Tinybucket::Model::Team.new(model_json).tap do |m| + m.username = teamname + end end before { stub_apiresponse(:get, request_path) if request_path } @@ -67,4 +67,23 @@ Tinybucket::Resource::Team::Repos) end end + + describe '#projects' do + let(:request_path) { "/teams/#{teamname}/projects/" } + subject { model.projects } + it 'return resource' do + expect(subject).to be_an_instance_of( + Tinybucket::Resource::Projects) + end + end + + describe '#project' do + let(:project_key) { 'myprj' } + let(:request_path) { "/teams/#{teamname}/projects/#{project_key}" } + subject { model.project(project_key) } + it 'return Project model' do + expect(subject).to be_an_instance_of( + Tinybucket::Model::Project) + end + end end diff --git a/spec/lib/tinybucket/resource/projects_spec.rb b/spec/lib/tinybucket/resource/projects_spec.rb new file mode 100644 index 0000000..42f8b74 --- /dev/null +++ b/spec/lib/tinybucket/resource/projects_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +RSpec.describe Tinybucket::Resource::Projects do + include ApiResponseMacros + + let(:owner) { 'test_team' } + let(:options) { {} } + let(:resource) { Tinybucket::Resource::Projects.new(owner, options) } + + describe '#find' do + let(:project_key) { 'myprj' } + let(:request_path) { "/teams/#{owner}/projects/#{project_key}" } + before { stub_apiresponse(:get, request_path) } + + subject { resource.find(project_key) } + it { expect(subject).to be_an_instance_of(Tinybucket::Model::Project) } + end + + describe '#create' do + let(:params) { {} } + subject { resource.create(params) } + it { expect { subject }.to raise_error(NotImplementedError) } + end + + describe 'Enumerable Methods' do + let(:request_path) { "/teams/#{owner}/projects/" } + before { stub_enum_response(:get, request_path) } + + describe '#take(1)' do + subject { resource.take(1) } + it { expect(subject).to be_an_instance_of(Array) } + end + + describe '#each' do + it 'iterate models' do + resource.each do |m| + expect(m).to be_an_instance_of(Tinybucket::Model::Project) + end + end + end + end +end