-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write tests for the annotations logic
- Loading branch information
Showing
6 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory(:annotation, class: Annotation) do | ||
association :annotated, factory: :provider_account | ||
name { Annotation::SUPPORTED_ANNOTATIONS.sample } | ||
value { 'operator' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AnnotatingTest < ActionDispatch::IntegrationTest | ||
|
||
class AnnotatedFeature < Feature | ||
annotated | ||
end | ||
|
||
class AnnotatingController < ApplicationController | ||
def update | ||
@annotated_feature = AnnotatedFeature.find params[:id] | ||
annotations = params.permit(annotations: {}) | ||
@annotated_feature.update(annotations) | ||
end | ||
end | ||
|
||
def with_test_routes | ||
Rails.application.routes.draw do | ||
put '/test/update' => 'annotating_test/annotating#update' | ||
end | ||
yield | ||
ensure | ||
Rails.application.routes_reloader.reload! | ||
end | ||
|
||
class Update < AnnotatingTest | ||
setup do | ||
@annotated_feature = AnnotatedFeature.new | ||
@annotated_feature.save! | ||
end | ||
|
||
test "creates a new annotations when it doesn't exist" do | ||
name = 'managed_by' | ||
value = 'operator' | ||
|
||
with_test_routes do | ||
put '/test/update', params: { id: @annotated_feature.id, annotations: { "#{name}": value } } | ||
end | ||
|
||
annotations = @annotated_feature.reload.annotations | ||
assert 1, annotations.size | ||
assert_equal name, annotations.first.name | ||
assert_equal value, annotations.first.value | ||
end | ||
|
||
test 'updates an existing annotation' do | ||
name = 'managed_by' | ||
value = 'admin' | ||
|
||
@annotated_feature.annotations << Annotation.new.tap do |a| | ||
a.name = name | ||
a.value = 'operator' | ||
end | ||
@annotated_feature.save! | ||
|
||
with_test_routes do | ||
put '/test/update', params: { id: @annotated_feature.id, annotations: { "#{name}": value } } | ||
end | ||
|
||
annotations = @annotated_feature.reload.annotations | ||
assert 1, annotations.size | ||
assert_equal name, annotations.first.name | ||
assert_equal value, annotations.first.value | ||
end | ||
|
||
['', ' ', nil].each do |value| | ||
test "removes an annotation when it's set to #{value.inspect}" do | ||
name = 'managed_by' | ||
|
||
@annotated_feature.annotations << Annotation.new.tap do |a| | ||
a.name = name | ||
a.value = 'operator' | ||
end | ||
@annotated_feature.save! | ||
|
||
with_test_routes do | ||
put '/test/update', params: { id: @annotated_feature.id, annotations: [{ name: name, value: value }] } | ||
end | ||
|
||
annotations = @annotated_feature.reload.annotations | ||
assert 0, annotations.size | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AnnotationTest < ActiveSupport::TestCase | ||
test 'not supported annotations are not allowed' do | ||
subject = Annotation.new | ||
|
||
subject.name = 'not_supported' | ||
|
||
assert_not subject.valid? | ||
assert subject.errors[:name].present? | ||
assert subject.errors[:name].include? 'is not included in the list' | ||
end | ||
|
||
test ':name is mandatory' do | ||
subject = Annotation.new | ||
|
||
assert_not subject.valid? | ||
assert subject.errors[:name].present? | ||
assert subject.errors[:name].include? "can't be blank" | ||
end | ||
|
||
test 'a valid :name is accepted' do | ||
subject = Annotation.new | ||
|
||
subject.name = 'managed_by' | ||
subject.valid? | ||
|
||
assert_not subject.errors[:name].present? | ||
end | ||
|
||
test ':value is mandatory' do | ||
subject = Annotation.new | ||
|
||
assert_not subject.valid? | ||
assert subject.errors[:value].present? | ||
assert subject.errors[:value].include? "can't be blank" | ||
end | ||
|
||
test 'a valid :value is accepted' do | ||
subject = Annotation.new | ||
|
||
subject.value = 'operator' | ||
subject.valid? | ||
|
||
assert_not subject.errors[:value].present? | ||
end | ||
|
||
test ':annotated is mandatory' do | ||
subject = Annotation.new | ||
|
||
assert_not subject.valid? | ||
assert subject.errors[:annotated].present? | ||
assert subject.errors[:annotated].include? "must exist" | ||
end | ||
|
||
test 'a valid :annotated is accepted' do | ||
subject = Annotation.new | ||
|
||
subject.annotated = Account.new | ||
subject.valid? | ||
|
||
assert_not subject.errors[:annotated].present? | ||
end | ||
|
||
%i[simple_provider backend_api simple_service].each do |factory| | ||
test "tenant_id trigger for #{factory}" do | ||
annotated = FactoryBot.create(factory) | ||
annotation = FactoryBot.create(:annotation, annotated: annotated) | ||
assert annotation.reload.tenant_id | ||
assert_equal annotated.reload.tenant_id, annotation.tenant_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
module Annotating | ||
class ManagedByTest < ActiveSupport::TestCase | ||
class AnnotatedFeature< Feature | ||
annotated | ||
end | ||
|
||
test "#managed_by returns the value of the 'managed_by' annotation" do | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
subject.annotate('managed_by', value) | ||
|
||
result = subject.managed_by | ||
|
||
assert_equal value, result | ||
end | ||
|
||
test "#managed_by returns nil when the 'managed_by' annotation doesn't exist" do | ||
subject = AnnotatedFeature.new | ||
|
||
result = subject.managed_by | ||
|
||
assert_nil result | ||
end | ||
|
||
test "#managed_by= sets the 'managed_by' annotation" do | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
|
||
subject.managed_by = value | ||
|
||
assert_equal value, subject.value_of_annotation('managed_by') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
module Annotating | ||
class ModelTest < ActiveSupport::TestCase | ||
class AnnotatedFeature < Feature | ||
annotated | ||
end | ||
|
||
class AnnotateTest < ModelTest | ||
test "creates a new annotation when it doesn't exist" do | ||
name = 'managed_by' | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
|
||
subject.annotations.expects(:build).with(name: name, value: value) | ||
|
||
subject.annotate(name, value) | ||
end | ||
|
||
test 'updates an annotation when it exists' do | ||
name = 'managed_by' | ||
value = 'admin' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, 'operator') | ||
|
||
subject.annotate(name, value) | ||
|
||
assert_equal 1, subject.annotations.size | ||
assert_equal name, subject.annotations.first.name | ||
assert_equal value, subject.annotations.first.value | ||
end | ||
|
||
['', ' ', nil].each do |value| | ||
test "removes an annotation when it's set to #{value.inspect}" do | ||
name = 'managed_by' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, 'operator') | ||
|
||
subject.expects(:remove_annotation).with(name) | ||
|
||
subject.annotate(name, value) | ||
end | ||
end | ||
end | ||
|
||
class RemoveAnnotationTest < ModelTest | ||
test 'marks annotation for destruction' do | ||
name = 'managed_by' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, 'operator') | ||
|
||
subject.remove_annotation(name) | ||
|
||
assert subject.annotations.first.marked_for_destruction? | ||
end | ||
end | ||
|
||
class AnnotationTest < ModelTest | ||
test 'returns the annotation model' do | ||
name = 'managed_by' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, 'operator') | ||
|
||
result = subject.annotation(name) | ||
|
||
assert_instance_of Annotation, result | ||
end | ||
|
||
test "returns nil if it doesn't exist" do | ||
subject = AnnotatedFeature.new | ||
subject.annotate('managed_by', 'operator') | ||
|
||
result = subject.annotation('test') | ||
|
||
assert_nil result | ||
end | ||
end | ||
|
||
class ValueOfAnnotationTest < ModelTest | ||
test 'returns the annotation value' do | ||
name = 'managed_by' | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, value) | ||
|
||
result = subject.value_of_annotation(name) | ||
|
||
assert_equal value, result | ||
end | ||
|
||
test "returns nil if it doesn't exist" do | ||
subject = AnnotatedFeature.new | ||
subject.annotate('managed_by', 'operator') | ||
|
||
result = subject.value_of_annotation('test') | ||
|
||
assert_nil result | ||
end | ||
end | ||
|
||
class AnnotationsTest < ModelTest | ||
test 'extracts the proper parameters from the given hash' do | ||
name = :managed_by | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
|
||
subject.expects(:annotate).with(name, value) | ||
|
||
subject.annotations = { "#{name}": value } | ||
end | ||
|
||
test 'annotates once per each received key' do | ||
subject = AnnotatedFeature.new | ||
|
||
subject.expects(:annotate).times(3) | ||
|
||
subject.annotations = { managed_by: 'operator', something: 'else', foo: 'bar' } | ||
end | ||
end | ||
|
||
class AnnotationsHash < ModelTest | ||
test 'it properly builds a hash' do | ||
name = 'managed_by' | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, value) | ||
subject.save! | ||
|
||
result = subject.reload.annotations_hash | ||
|
||
assert_equal({ name => value }, result) | ||
end | ||
end | ||
|
||
class AnnotationsXML < ModelTest | ||
test 'it properly builds a xml' do | ||
name = 'managed_by' | ||
value = 'operator' | ||
subject = AnnotatedFeature.new | ||
subject.annotate(name, value) | ||
|
||
result = subject.annotations_xml | ||
|
||
assert_match "<annotations><#{name}>#{value}</#{name}></annotations>", result | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AnnotatingTest < ActiveSupport::TestCase | ||
|
||
class AnnotatedFeature < Feature | ||
annotated | ||
end | ||
|
||
test 'includes the proper modules' do | ||
%w[Annotating::Model Annotating::ManagedBy].each do |mod| | ||
assert_includes AnnotatedFeature.ancestors.map(&:name), mod | ||
end | ||
end | ||
|
||
test '#models returns the annotated models' do | ||
%w[Account Service BackendApi].each do |model| | ||
assert_includes Annotating.models.map(&:name), model | ||
end | ||
end | ||
end |