Skip to content

Commit ce95ea8

Browse files
authored
Merge pull request #38 from julioalucero/print_schema_stats
Updates test and dummy app with models and schema.rb
2 parents 49893ab + be590a4 commit ce95ea8

File tree

7 files changed

+70
-39
lines changed

7 files changed

+70
-39
lines changed

lib/rails_stats/json_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ def schema_info
7979
end
8080
end
8181
end
82-
end
82+
end

test/dummy/app/models/comment.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Comments < ApplicationRecord
2+
belongs_to :commentable, polymorphic: true
3+
end

test/dummy/app/models/pet.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Pets < User
2+
end

test/dummy/app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Users < ApplicationRecord
2+
end

test/dummy/db/schema.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
ActiveRecord::Schema[7.0].define(version: 2023_04_25_154701) do
3+
create_table "users", force: :cascade do |t|
4+
t.string "email"
5+
end
6+
create_table "comments", force: :cascade do |t|
7+
t.bigint :commentable_id
8+
t.string :commentable_type
9+
end
10+
end

test/fixtures/console-output.txt

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
+-----------------------|------------|----------------+
22
| Name | Total Deps | 1st Level Deps |
33
+-----------------------|------------|----------------+
4-
| simplecov-console | 7 | 3 |
5-
| codecov | 5 | 2 |
4+
| rails | 50 | 14 |
5+
| sass-rails | 31 | 1 |
6+
| web-console | 26 | 4 |
7+
| webpacker | 25 | 3 |
8+
| jbuilder | 17 | 2 |
9+
| spring-watcher-listen | 5 | 2 |
610
| rails_stats | 4 | 2 |
7-
| simplecov | 3 | 3 |
8-
| minitest-around | 1 | 1 |
9-
| bundler | 0 | 0 |
11+
| listen | 3 | 2 |
12+
| bootsnap | 1 | 1 |
13+
| puma | 1 | 1 |
1014
| byebug | 0 | 0 |
11-
| minitest | 0 | 0 |
12-
| minitest-spec-context | 0 | 0 |
15+
| spring | 0 | 0 |
16+
| tzinfo-data | 0 | 0 |
1317
+-----------------------|------------|----------------+
1418

15-
Declared Gems 9
16-
Total Gems 18
17-
Unpinned Versions 8
19+
Declared Gems 13
20+
Total Gems 71
21+
Unpinned Versions 7
1822
Github Refs 0
1923

2024
+----------------------+---------+---------+---------+---------+---------+-----+-------+
2125
| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |
2226
+----------------------+---------+---------+---------+---------+---------+-----+-------+
2327
| Channels | 2 | 8 | 8 | 2 | 0 | 0 | 0 |
24-
| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 |
28+
| Configuration | 19 | 419 | 113 | 1 | 0 | 0 | 0 |
2529
| Controllers | 1 | 7 | 6 | 1 | 1 | 1 | 4 |
2630
| Helpers | 1 | 3 | 3 | 0 | 0 | 0 | 0 |
2731
| Javascripts | 3 | 27 | 7 | 0 | 0 | 0 | 0 |
2832
| Jobs | 1 | 7 | 2 | 1 | 0 | 0 | 0 |
2933
| Libraries | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3034
| Mailers | 1 | 4 | 4 | 1 | 0 | 0 | 0 |
3135
| Model Tests | 2 | 5 | 4 | 2 | 0 | 0 | 0 |
32-
| Models | 1 | 3 | 3 | 1 | 0 | 0 | 0 |
36+
| Models | 4 | 10 | 10 | 4 | 0 | 0 | 0 |
3337
| Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3438
| Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3539
+----------------------+---------+---------+---------+---------+---------+-----+-------+
36-
| Code | 30 | 477 | 145 | 7 | 1 | 0 | 143 |
40+
| Code | 33 | 486 | 154 | 10 | 1 | 0 | 152 |
3741
| Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 |
38-
| Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 |
42+
| Total | 37 | 493 | 160 | 12 | 1 | 0 | 158 |
3943
+----------------------+---------+---------+---------+---------+---------+-----+-------+
40-
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34
44+
Code LOC: 154 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 37
4145

42-
STI models count: 0 STI classes
43-
Polymorphic models count: 0 polymorphic associations
44-
Schema Stats: No schema.rb or structure.sql file found
46+
STI models count: 1 STI classes
47+
Polymorphic models count: 1 polymorphic associations
48+
Schema Stats: 2 `create_table` calls in schema.rb

test/lib/rails_stats/json_formatter_test.rb

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
"loc_over_m": "0"
3535
}, {
3636
"name": "Models",
37-
"files": "1",
38-
"lines": "3",
39-
"loc": "3",
40-
"classes": "1",
37+
"files": "4",
38+
"lines": "10",
39+
"loc": "10",
40+
"classes": "4",
4141
"methods": "0",
4242
"m_over_c": "0",
4343
"loc_over_m": "0"
@@ -89,8 +89,8 @@
8989
}, {
9090
"name": "Configuration",
9191
"files": "19",
92-
"lines": "417",
93-
"loc": "111",
92+
"lines": "419",
93+
"loc": "113",
9494
"classes": "1",
9595
"methods": "0",
9696
"m_over_c": "0",
@@ -115,27 +115,27 @@
115115
"loc_over_m": "0"
116116
}, {
117117
"name": "Total",
118-
"files": "34",
119-
"lines": "484",
120-
"loc": "151",
121-
"classes": "9",
118+
"files": "37",
119+
"lines": "493",
120+
"loc": "160",
121+
"classes": "12",
122122
"methods": "1",
123123
"m_over_c": "0",
124-
"loc_over_m": "149",
124+
"loc_over_m": "158",
125125
"code_to_test_ratio": "0.0",
126126
"total": true
127127
}, {
128128
"schema_stats": {
129-
"schema_path": "db/schema.rb",
130-
"create_table_calls_count": 5
129+
"schema_path": "#{File.absolute_path("./test/dummy/db/schema.rb")}",
130+
"create_table calls count": 2
131131
}
132132
}, {
133133
"sti_stats": {
134-
"sti_models_count": 3
134+
"sti_models_count": 1
135135
}
136136
}, {
137137
"polymorphic_stats": {
138-
"polymorphic_models_count": 2
138+
"polymorphic_models_count": 1
139139
}
140140
}]
141141
EOS
@@ -146,12 +146,22 @@
146146
calculator = RailsStats::StatsCalculator.new(root_directory)
147147
formatter = RailsStats::JSONFormatter.new(calculator)
148148

149-
sorted_expectation = JSON.parse(JSON_STRING).sort {|x, y| x["name"] <=> y["name"] }
150-
sorted_result = formatter.result.sort {|x, y| x["name"] <=> y["name"] }
149+
parsed_expectation = JSON.parse(JSON_STRING)
150+
parsed_result = formatter.result
151+
152+
named_expectation = parsed_expectation.select { |h| h.key?("name") }.sort_by { |h| h["name"] }
153+
metadata_expectation = parsed_expectation.reject { |h| h.key?("name") }
154+
155+
named_result = parsed_result.select { |h| h.key?("name") }.sort_by { |h| h["name"] }
156+
metadata_result = parsed_result.reject { |h| h.key?("name") }
157+
158+
named_expectation.each_with_index do |x, i|
159+
assert_equal x, named_result[i]
160+
end
151161

152-
sorted_expectation.each_with_index do |x, i|
153-
assert_equal x, sorted_result[i]
162+
metadata_expectation.each_with_index do |x, i|
163+
assert_equal x, metadata_result[i]
154164
end
155165
end
156166
end
157-
end
167+
end

0 commit comments

Comments
 (0)