-
Notifications
You must be signed in to change notification settings - Fork 202
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
working example or tutorial about creating a sql kata #104
Comments
You can forfeit an existing kata, then open a fork of an existing solution to get the full example/structure |
If you do use an existing SQL kata, be sure to avoid exposing the solution in the preloaded code file. It's easy for a code warrior to read the file in the sample tests and view the solution. Also, drop and recreate the database per Here's an opinionated suggestion of how to test SQL which is similar to the "insert example" button in the new kata editor. In both cases, sample tests are explicitly visible to the code warrior rather than buried in the interface with Disadvantages of this approach are that the challenger might be surprised or confused by the Ruby code, so offering an explanatory note at the end of the instructions can go a long way to mitigating this. Another possible downside is that the test file can become verbose, so you can strategically move some of the content to helper functions in the preloaded file. Other than Codewars' Random tests are important to include for the submission to prevent cheating and can use Example instructions:Write a query to retrieve all rows from the Complete solution:SELECT * FROM widgets WHERE widgets.name LIKE 'foo%'; Initial solution:-- Write your query here Preloaded code:def show_diff_table(actual, expected)
if actual.empty?
puts "<LOG::Results: Actual>No rows returned"
puts "<TAB:TABLE:Results: Expected>#{expected.to_json()}"
else
daff_data = DaffWrapper.new(
actual,
expected,
index: true
).serializable
Display.daff_table(
daff_data,
label: "Diff",
tab: true,
allow_preview: true
)
end
end Test cases and example test cases:describe "Query tests" do
after(:each) {DB.drop_table?(:widgets)}
before(:each) do
DB.create_table(:widgets) do
primary_key(:id)
varchar(:name)
end
end
it "should work on an example test" do
DB[:widgets].insert(name: "foo")
DB[:widgets].insert(name: "quux")
DB[:widgets].insert(name: "foobar")
expected = [{:id => 1, :name => "foo"},
{:id => 3, :name => "foobar"}]
actual = run_sql.to_a()
show_diff_table(actual, expected)
expect(actual).to eq(expected)
end
end |
@ggorlen : could you provide an example of random tests with this, plz? |
Sure, either Faker or # ...
it "should work on a random test" do
40.times do
name = (1..rand(1..4)).map {"foo".chars.sample}.join
DB[:widgets].insert(name: name)
end
ref_soln_query = "SELECT * FROM widgets WHERE widgets.name LIKE 'foo%';"
expected = DB[ref_soln_query].to_a()
actual = run_sql.to_a()
show_diff_table(actual, expected)
expect(actual).to eq(expected)
end
# ... Happy to hear any improvements/suggestions you might have. |
thx! 👍 (unforunately, I cannot suggest anything since I didn't ever write a sql test site... ;/ ) PR: #105. If wanna take a quick look to it. ;) cheers & thx again. |
There's a problem with the approach as I originally posted. The way |
thx! I updated the PR. |
* Create sql-writting-tests.md * Autoformat with Prettier * Update sql-writting-tests.md * Update sql-writting-tests.md * Autoformat with Prettier * Update sql-writting-tests.md * Update sql-writting-tests.md * Update content/languages/sql/sql-writting-tests.md Co-authored-by: kazk <[email protected]> * Apply suggestions from code review Co-authored-by: kazk <[email protected]> * Update sql-writting-tests.md * Update sql-writting-tests.md * Autoformat with Prettier * Update content/languages/sql/sql-writting-tests.md * Correction of the code [see here](#104 (comment)) * Autoformat with Prettier Co-authored-by: Blind4Basics <[email protected]> Co-authored-by: kazk <[email protected]>
I have more than 1500 reputation and already created a couple of Javascript kata
I´d like to create a sql kata, but I have no clue about how to start with
Is there a tutorial or a complete example that I could have a look at?
I can only see the test cases but I don't know how to setup the initial data (create table, inserts, etc) or the expected result.
thanks a lot
The text was updated successfully, but these errors were encountered: