|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "dummy_rails_app" |
| 4 | +require "rails_helper" |
| 5 | + |
| 6 | +RSpec.describe TinyAdmin::Plugins::ActiveRecordRepository do |
| 7 | + let(:repository) { described_class.new(Author) } |
| 8 | + |
| 9 | + before { setup_data(posts_count: 12) } |
| 10 | + |
| 11 | + describe "#index_title" do |
| 12 | + it "returns the pluralized model name" do |
| 13 | + expect(repository.index_title).to eq("Authors") |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + describe "#show_title" do |
| 18 | + it "returns the model name with the record id" do |
| 19 | + author = Author.first |
| 20 | + expect(repository.show_title(author)).to eq("Author ##{author.id}") |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe "#fields" do |
| 25 | + it "returns all model columns as Field objects when no options given", :aggregate_failures do |
| 26 | + fields = repository.fields |
| 27 | + expect(fields).to be_a(Hash) |
| 28 | + expect(fields.keys).to include("id", "name", "age", "email") |
| 29 | + expect(fields["name"]).to be_a(TinyAdmin::Field) |
| 30 | + expect(fields["name"].type).to eq(:string) |
| 31 | + end |
| 32 | + |
| 33 | + it "returns only specified fields when options given" do |
| 34 | + options = {"name" => {}, "email" => {}} |
| 35 | + fields = repository.fields(options: options) |
| 36 | + expect(fields.keys).to eq(["name", "email"]) |
| 37 | + end |
| 38 | + |
| 39 | + it "maps column types correctly", :aggregate_failures do |
| 40 | + fields = repository.fields |
| 41 | + expect(fields["id"].type).to eq(:integer) |
| 42 | + expect(fields["name"].type).to eq(:string) |
| 43 | + expect(fields["age"].type).to eq(:integer) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "#find" do |
| 48 | + it "returns the record for a valid id" do |
| 49 | + author = Author.first |
| 50 | + expect(repository.find(author.id)).to eq(author) |
| 51 | + end |
| 52 | + |
| 53 | + it "raises RecordNotFound for an invalid id" do |
| 54 | + expect { repository.find(999_999) } |
| 55 | + .to raise_error(TinyAdmin::Plugins::BaseRepository::RecordNotFound) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe "#collection" do |
| 60 | + it "returns all records" do |
| 61 | + expect(repository.collection.count).to eq(Author.count) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + describe "#index_record_attrs" do |
| 66 | + let(:author) { Author.first } |
| 67 | + |
| 68 | + it "returns all attributes as strings when no fields given", :aggregate_failures do |
| 69 | + attrs = repository.index_record_attrs(author) |
| 70 | + expect(attrs["name"]).to eq(author.name) |
| 71 | + expect(attrs["id"]).to eq(author.id.to_s) |
| 72 | + end |
| 73 | + |
| 74 | + it "returns only specified fields when fields given", :aggregate_failures do |
| 75 | + attrs = repository.index_record_attrs(author, fields: {"name" => nil, "email" => nil}) |
| 76 | + expect(attrs.keys).to eq(["name", "email"]) |
| 77 | + expect(attrs["name"]).to eq(author.name) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + describe "#list" do |
| 82 | + it "returns records and total count", :aggregate_failures do |
| 83 | + records, count = repository.list(page: 1, limit: 2) |
| 84 | + expect(records.size).to eq(2) |
| 85 | + expect(count).to eq(3) |
| 86 | + end |
| 87 | + |
| 88 | + it "paginates correctly", :aggregate_failures do |
| 89 | + records_page1, = repository.list(page: 1, limit: 2) |
| 90 | + records_page2, = repository.list(page: 2, limit: 2) |
| 91 | + expect(records_page1).not_to eq(records_page2) |
| 92 | + expect(records_page2.size).to eq(1) |
| 93 | + end |
| 94 | + |
| 95 | + it "sorts when sort option given" do |
| 96 | + records, = repository.list(page: 1, limit: 10, sort: {name: :desc}) |
| 97 | + names = records.map(&:name) |
| 98 | + expect(names).to eq(names.sort.reverse) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe "#apply_filters" do |
| 103 | + let(:post_repository) { described_class.new(Post) } |
| 104 | + |
| 105 | + it "filters string fields with LIKE" do |
| 106 | + title_field = TinyAdmin::Field.new(name: "title", title: "Title", type: :string) |
| 107 | + filters = {title_field => {value: "post 1"}} |
| 108 | + results = post_repository.apply_filters(Post.all, filters) |
| 109 | + results.each do |post| |
| 110 | + expect(post.title.downcase).to include("post 1") |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + it "filters non-string fields with equality" do |
| 115 | + author = Author.first |
| 116 | + author_field = TinyAdmin::Field.new(name: "author_id", title: "Author", type: :integer) |
| 117 | + filters = {author_field => {value: author.id}} |
| 118 | + results = post_repository.apply_filters(Post.all, filters) |
| 119 | + results.each do |post| |
| 120 | + expect(post.author_id).to eq(author.id) |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + it "skips filters with nil or empty values" do |
| 125 | + title_field = TinyAdmin::Field.new(name: "title", title: "Title", type: :string) |
| 126 | + filters = {title_field => {value: nil}} |
| 127 | + results = post_repository.apply_filters(Post.all, filters) |
| 128 | + expect(results.count).to eq(Post.count) |
| 129 | + end |
| 130 | + |
| 131 | + it "sanitizes SQL LIKE input" do |
| 132 | + title_field = TinyAdmin::Field.new(name: "title", title: "Title", type: :string) |
| 133 | + filters = {title_field => {value: "100%"}} |
| 134 | + # Should not raise or cause SQL injection |
| 135 | + expect { post_repository.apply_filters(Post.all, filters).to_a }.not_to raise_error |
| 136 | + end |
| 137 | + end |
| 138 | +end |
0 commit comments