[katello] how to write a test

I have the following method[1] I want to test.

  def create_docker_tag(manifest, tag_json)
    tag = DockerTag.where(:repository_id => id, :name =>

tag_json[:name]).first
if tag
tag.docker_manifest_id = manifest.id
tag.uuid = tag_json[:_id]
tag.save
else
tag = DockerTag.create(:repository_id => id,
:docker_manifest_id => manifest.id,
:name => tag_json[:name], :uuid =>
tag_json[:_id])
end
tag
end

Here is the original test[2] (note the 'expects' is no longer relevant):

def test_create_docker_no_tag
  manifest = FactoryGirl.build(:docker_manifest)

  @fedora_17_x86_64.expects(:unit_search).returns([])

  @fedora_17_x86_64.create_docker_tag(manifest, 'asdf')
end

I would like to write two tests, one where the DockerTag exists already,
and one where it does not and so is created. I am unsure what to
FactoryGirl, or stub, or assert, etc.

Pointers to resources that would assist me, and other devs I assume, in
writing tests for the framework katello uses would be great. Really,
anything would be great since I feel like I'm just iterating on random
guesses to try to figure out what to do.

For example, this doesn't work:

def test_create_docker_no_tag
  manifest = FactoryGirl.build(:docker_manifest)
  manifest.id = 1

  @fedora_17_x86_64.create_docker_tag(manifest, {:name => 'asdf',

:_id => '1234')
end

  1. Error:
    Katello::GluePulpNonVcrTests#test_create_docker_no_tag:
    ActiveRecord::RecordInvalid: Validation failed: Docker manifest can't be blank
    test_after_commit (1.1.0)
    lib/test_after_commit/database_statements.rb:11:in block in transaction' test_after_commit (1.1.0) lib/test_after_commit/database_statements.rb:5:intransaction'
    /home/vagrant/katello/app/models/katello/glue/pulp/repo.rb:539:in
    create_docker_tag' ./katello/test/glue/pulp/repository_test.rb:67:intest_create_docker_no_tag'

[1]

[2]

Hi Tom,
For no_tag case you can probably stub ActiveRecord where clause and
assert tag to be present and tag.name is as expected.

def test_create_docker_no_tag                                           
                                                                        
                                  
  manifest = FactoryGirl.create(:docker_manifest)
 tag_name = "docker-tag-#{rand()}"

  DockerTag.stubs(:where).returns([])
 tag = @fedora_17_x86_64.create_docker_tag(manifest, {:name => tag_name, 

:_id => '1234'})

  assert_not_nil(tag)
 assert_equal tag.name, tag_name

end

For the other case you mostly need to create a docker_tag using factory and
you may stub DockerTag.where to return that object. Once @fedora_17_x86_64.
create_docker_tag has been called, later compare it with expected object
similar to what we did above.

I hope that helps.

··· On Thursday, 17 November 2016 00:46:25 UTC+5:30, Tom McKay wrote: > > I have the following method[1] I want to test. > > > > def create_docker_tag(manifest, tag_json) > tag = DockerTag.where(:repository_id => id, :name => tag_json[:name]).first > if tag > tag.docker_manifest_id = manifest.id > tag.uuid = tag_json[:_id] > tag.save > else > tag = DockerTag.create(:repository_id => id, :docker_manifest_id => manifest.id, > :name => tag_json[:name], :uuid => tag_json[:_id]) > end > tag > end > > > > Here is the original test[2] (note the 'expects' is no longer relevant): > > > > def test_create_docker_no_tag > manifest = FactoryGirl.build(:docker_manifest) > > @fedora_17_x86_64.expects(:unit_search).returns([]) > > @fedora_17_x86_64.create_docker_tag(manifest, 'asdf') > end > > > I would like to write two tests, one where the DockerTag exists already, > and one where it does not and so is created. I am unsure what to > FactoryGirl, or stub, or assert, etc. > > Pointers to resources that would assist me, and other devs I assume, in > writing tests for the framework katello uses would be great. Really, > anything would be great since I feel like I'm just iterating on random > guesses to try to figure out what to do. > > For example, this doesn't work: > > def test_create_docker_no_tag > manifest = FactoryGirl.build(:docker_manifest) > manifest.id = 1 > > @fedora_17_x86_64.create_docker_tag(manifest, {:name => 'asdf', :_id => '1234') > end > > 1) Error: > Katello::GluePulpNonVcrTests#test_create_docker_no_tag: > ActiveRecord::RecordInvalid: Validation failed: Docker manifest can't be blank > test_after_commit (1.1.0) lib/test_after_commit/database_statements.rb:11:in `block in transaction' > test_after_commit (1.1.0) lib/test_after_commit/database_statements.rb:5:in `transaction' > /home/vagrant/katello/app/models/katello/glue/pulp/repo.rb:539:in `create_docker_tag' > ./katello/test/glue/pulp/repository_test.rb:67:in `test_create_docker_no_tag' > > > > [1] > https://github.com/thomasmckay/katello/blob/2ddce0d01b5f50290d446346d7073260e3a3e807/app/models/katello/glue/pulp/repo.rb#L532 > > [2] > https://github.com/Katello/katello/blob/master/test/glue/pulp/repository_test.rb#L61 >

Have prepared the *with_tag *test case for you.

def test_create_docker_with_tag                                         
                                                                        
                                  
  manifest = FactoryGirl.create(:docker_manifest)
  docker_tag = FactoryGirl.create(:docker_tag, :repository => 

@fedora_17_x86_64)

  DockerTag.stubs(:where).returns([docker_tag])
  tag = @fedora_17_x86_64.create_docker_tag(manifest, {:_id => '1234'})

  assert_not_nil(tag)
  assert_equal(tag.id, docker_tag.id)
  assert_equal(tag.docker_manifest_id, manifest.id)
  assert_equal(tag.uuid, '1234')
end

Tested with your changes works fine.

··· On Thursday, 17 November 2016 14:21:52 UTC+5:30, Swapnil Abnave wrote: > > Hi Tom, > For *no_tag* case you can probably stub ActiveRecord where clause and > assert tag to be present and tag.name is as expected. > > def test_create_docker_no_tag > > > manifest = FactoryGirl.create(:docker_manifest) > tag_name = "docker-tag-#{rand()}" > > DockerTag.stubs(:where).returns([]) > tag = @fedora_17_x86_64.create_docker_tag(manifest, {:name => > tag_name, :_id => '1234'}) > > assert_not_nil(tag) > assert_equal tag.name, tag_name > end > > For the other case you mostly need to create a docker_tag using factory > and you may stub DockerTag.where to return that object. Once > @fedora_17_x86_64.create_docker_tag has been called, later compare it > with expected object similar to what we did above. > > I hope that helps. > > On Thursday, 17 November 2016 00:46:25 UTC+5:30, Tom McKay wrote: >> >> I have the following method[1] I want to test. >> >> >> >> def create_docker_tag(manifest, tag_json) >> tag = DockerTag.where(:repository_id => id, :name => tag_json[:name]).first >> if tag >> tag.docker_manifest_id = manifest.id >> tag.uuid = tag_json[:_id] >> tag.save >> else >> tag = DockerTag.create(:repository_id => id, :docker_manifest_id => manifest.id, >> :name => tag_json[:name], :uuid => tag_json[:_id]) >> end >> tag >> end >> >> >> >> Here is the original test[2] (note the 'expects' is no longer relevant): >> >> >> >> def test_create_docker_no_tag >> manifest = FactoryGirl.build(:docker_manifest) >> >> @fedora_17_x86_64.expects(:unit_search).returns([]) >> >> @fedora_17_x86_64.create_docker_tag(manifest, 'asdf') >> end >> >> >> I would like to write two tests, one where the DockerTag exists already, >> and one where it does not and so is created. I am unsure what to >> FactoryGirl, or stub, or assert, etc. >> >> Pointers to resources that would assist me, and other devs I assume, in >> writing tests for the framework katello uses would be great. Really, >> anything would be great since I feel like I'm just iterating on random >> guesses to try to figure out what to do. >> >> For example, this doesn't work: >> >> def test_create_docker_no_tag >> manifest = FactoryGirl.build(:docker_manifest) >> manifest.id = 1 >> >> @fedora_17_x86_64.create_docker_tag(manifest, {:name => 'asdf', :_id => '1234') >> end >> >> 1) Error: >> Katello::GluePulpNonVcrTests#test_create_docker_no_tag: >> ActiveRecord::RecordInvalid: Validation failed: Docker manifest can't be blank >> test_after_commit (1.1.0) lib/test_after_commit/database_statements.rb:11:in `block in transaction' >> test_after_commit (1.1.0) lib/test_after_commit/database_statements.rb:5:in `transaction' >> /home/vagrant/katello/app/models/katello/glue/pulp/repo.rb:539:in `create_docker_tag' >> ./katello/test/glue/pulp/repository_test.rb:67:in `test_create_docker_no_tag' >> >> >> >> [1] >> https://github.com/thomasmckay/katello/blob/2ddce0d01b5f50290d446346d7073260e3a3e807/app/models/katello/glue/pulp/repo.rb#L532 >> >> [2] >> https://github.com/Katello/katello/blob/master/test/glue/pulp/repository_test.rb#L61 >> >