-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to compute the average of a given column in query sets (
#149) * Add the ability to compute the average of a given column in query sets * Fix floating check * Handle PG::Numeric * Change float check * Add model class * Add spec * Fix findings * Change return type * Fix spec * Use ameba
- Loading branch information
Showing
8 changed files
with
166 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
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 |
---|---|---|
|
@@ -43,6 +43,16 @@ describe Marten::DB::Model::Querying do | |
end | ||
end | ||
|
||
describe "::average" do | ||
it "properly calculates the average" do | ||
user = TestUser.create!(username: "jd1", email: "[email protected]", first_name: "John", last_name: "Doe") | ||
Post.create!(author: user, title: "Example post 1", score: 5.0) | ||
Post.create!(author: user, title: "Example post 2", score: 5.0) | ||
|
||
Post.average(:score).not_nil!.should be_close(5.0, 0.00001) | ||
end | ||
end | ||
|
||
describe "::bulk_create" do | ||
it "allows to insert an array of records without specifying a batch size" do | ||
objects = (1..100).map do |i| | ||
|
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,8 @@ | ||
module Marten::DB::Query::SetSpec | ||
class Product < Marten::Model | ||
field :id, :big_int, primary_key: true, auto: true | ||
field :name, :string, max_size: 255 | ||
field :price, :int | ||
field :rating, :float, blank: true, null: true | ||
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
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,8 @@ | ||
module Marten::DB::Query::SQL::QuerySpec | ||
class Product < Marten::Model | ||
field :id, :big_int, primary_key: true, auto: true | ||
field :name, :string, max_size: 255 | ||
field :price, :int | ||
field :rating, :float, blank: true, null: true | ||
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
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
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