Skip to content

Commit

Permalink
Refactor testing to use new TableDB schema
Browse files Browse the repository at this point in the history
Added TableDB schema for testing purposes, including table definitions for 'customers', 'users', 'address', and 'employees'. Updated tests to utilize TableDB instead of Northwind for schema consistency in the specs. Adjusted file permissions setup in the workflow to accommodate the new test database. Included .vscode directory in .gitignore for local environment isolation.

Enhances test database structure clarity and modularity.
  • Loading branch information
eliasjpr committed Aug 13, 2024
1 parent 78b1aea commit 0d436aa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/crystal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ jobs:
touch ./spec/db/northwind.db
touch ./spec/db/data.db
touch ./spec/db/billing.db
touch ./spec/db/tabledb.db
chmod 664 ./spec/db/northwind.db
chmod 664 ./spec/db/data.db
chmod 664 ./spec/db/billing.db
chmod 664 ./spec/db/tabledb.db
- name: Install dependencies
run: shards install
- name: Check code style
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/bin/
/.shards/
*.dwarf

/.vscode/
/spec/db/*.db
# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
Expand Down
38 changes: 38 additions & 0 deletions spec/schemas/tabledb.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TableDB = Cql::Schema.build(
name: :tabledb,
adapter: Cql::Adapter::Sqlite,
uri: "sqlite3://spec/db/tabledb.db") do
table :customers do
primary :id, Int32
column :name, String
column :city, String
column :balance, Int32
timestamps
end

table :users do
primary :id, Int32
column :name, String
column :email, String
column :age, Int32
timestamps
end

table :address do
primary :id, Int32
column :user_id, Int64, null: false
column :street, String
column :city, String
column :zip, String
timestamps
end

table :employees do
primary :id, Int32
column :name, String
column :email, String
column :phone, String
column :department, String
timestamps
end
end
24 changes: 12 additions & 12 deletions spec/table_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ require "./spec_helper"

describe Cql::Table do
it "creates table" do
Northwind.customers.drop!
Northwind.customers.create!
TableDB.customers.drop!
TableDB.customers.create!

customer = CustomerModel.new(1, "John", "New York", 100)

insert_query = Northwind.insert.into(:customers).values(
insert_query = TableDB.insert.into(:customers).values(
id: customer.id,
name: customer.name,
city: customer.city,
balance: customer.balance
).commit

persisted = Northwind.query.from(:customers).first!(as: CustomerModel)
persisted = TableDB.query.from(:customers).first!(as: CustomerModel)

persisted.id.should eq customer.id
persisted.name.should eq customer.name
Expand All @@ -23,34 +23,34 @@ describe Cql::Table do
end

it "truncates table" do
Northwind.customers.drop!
Northwind.customers.create!
TableDB.customers.drop!
TableDB.customers.create!

customers = [
{:name => "John", :city => "New York", :balance => 100},
{:name => "Jane", :city => "New York", :balance => 200},
]

Northwind
TableDB
.insert
.into(:customers)
.values(customers)
.commit

count_query = Northwind.query.from(:customers).count
count_query = TableDB.query.from(:customers).count
count_query.first!(as: Int32).should eq 2

result = Northwind.delete.from(:customers).commit
result = TableDB.delete.from(:customers).commit
count_query.first!(as: Int32).should eq 0
end

it "drops table" do
Northwind.customers.drop!
table = Northwind.customers.table_name.to_s
TableDB.customers.drop!
table = TableDB.customers.table_name.to_s
check_query = "SELECT name FROM sqlite_master WHERE type='table' AND name='#{table}'"

expect_raises(DB::NoResultsError) do
name = Northwind.db.query_one(check_query, as: String)
name = TableDB.db.query_one(check_query, as: String)
end
end
end

0 comments on commit 0d436aa

Please sign in to comment.