-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spec for from_sql, and a big mock for the DB class #133
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,41 @@ | ||
require "../../spec_helper" | ||
|
||
macro build_review_emitter(driver) | ||
{% | ||
timestamp = if driver == "sqlite" | ||
"2018-04-09 13:33:46" | ||
else | ||
"Time.now".id | ||
end | ||
%} | ||
|
||
FieldEmitter.new.tap do |e| | ||
e._set_values( | ||
[ | ||
8_i64, | ||
"name", | ||
nil, # downvotes | ||
nil, # upvotes | ||
nil, # sentiment | ||
nil, # interest | ||
true, # published | ||
{{ timestamp }} # created_at | ||
] | ||
) | ||
end | ||
end | ||
|
||
def method_which_takes_any_model(model : Granite::ORM::Base.class) | ||
model.as(Granite::ORM::Base).from_sql build_review_emitter | ||
end | ||
|
||
{% for adapter in GraniteExample::ADAPTERS %} | ||
module {{ adapter.capitalize.id }} | ||
describe "{{ adapter.id }} #from_sql" do | ||
it "Builds a model from a resultset" do | ||
model = Review.from_sql build_review_emitter({{ adapter }}) | ||
model.class.should eq Review | ||
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,89 @@ | ||
class FakeStatement < DB::Statement | ||
protected def perform_query(args : Enumerable) : DB::ResultSet | ||
FieldEmitter.new | ||
end | ||
|
||
protected def perform_exec(args : Enumerable) | ||
DB::ExecResult.new 0_i64, 0_i64 | ||
end | ||
end | ||
|
||
class FakeContext | ||
include DB::ConnectionContext | ||
|
||
def uri | ||
URI.new "" | ||
end | ||
|
||
def prepared_statements? | ||
false | ||
end | ||
|
||
def discard(connection); end | ||
def release(connection); end | ||
end | ||
|
||
class FakeConnection < DB::Connection | ||
def initialize | ||
@context = FakeContext.new | ||
@prepared_statements = false | ||
end | ||
|
||
def build_unprepared_statement(query : String) | ||
FakeStatement.new self | ||
end | ||
|
||
def build_prepared_statement(query : String) | ||
FakeStatement.new self | ||
end | ||
end | ||
|
||
# FieldEmitter emulates the subtle and uninformed way that | ||
# DB::ResultSet emits data. To be used in testing interactions | ||
# with raw data sets. | ||
class FieldEmitter < DB::ResultSet | ||
# 1. Override `#move_next` to move to the next row. | ||
# 2. Override `#read` returning the next value in the row. | ||
# 3. (Optional) Override `#read(t)` for some types `t` for which custom logic other than a simple cast is needed. | ||
# 4. Override `#column_count`, `#column_name`. | ||
|
||
@position = 0 | ||
@field_position = 0 | ||
@values = [] of DB::Any | ||
|
||
def initialize | ||
@statement = FakeStatement.new FakeConnection.new | ||
end | ||
|
||
def _set_values(values : Array(DB::Any)) | ||
@values = [] of DB::Any | ||
values.each do |v| | ||
@values << v | ||
end | ||
end | ||
|
||
def move_next | ||
@position += 1 | ||
@field_position = 0 | ||
end | ||
|
||
def read | ||
if @position >= @values.size | ||
raise "Overread" | ||
end | ||
|
||
|
||
@values[@position].tap do |v| | ||
@position += 1 | ||
end | ||
end | ||
|
||
def column_count | ||
@values.size | ||
end | ||
|
||
def column_name(index : Int32) | ||
"Column #{index}" | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra line