Skip to content

Commit

Permalink
test: fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid committed Dec 3, 2024
1 parent b735a36 commit 8721f00
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 304 deletions.
2 changes: 1 addition & 1 deletion app/models/cell_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class CellDependency < ApplicationRecord
belongs_to :ckb_transaction
belongs_to :cell_output, foreign_key: "contract_cell_id", class_name: "CellOutput"
belongs_to :cell_deps_out_point, foreign_key: :contract_cell_id, primary_key: :contract_cell_id
belongs_to :cell_deps_out_point, foreign_key: :contract_cell_id, primary_key: :contract_cell_id, optional: true

enum :dep_type, %i[code dep_group]

Expand Down
2 changes: 1 addition & 1 deletion app/models/cell_deps_out_point.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CellDepsOutPoint < ApplicationRecord
belongs_to :contract, foreign_key: :deployed_cell_output_id, primary_key: :deployed_cell_output_id
belongs_to :contract, foreign_key: :deployed_cell_output_id, primary_key: :deployed_cell_output_id, optional: true
has_many :cell_dependencies, foreign_key: :contract_cell_id, primary_key: :contract_cell_id

scope :list_contract_cell_ids_by_contract, ->(contract_ids) { joins(:contract).where(contracts: { id: contract_ids }).pluck(:contract_cell_id) }
Expand Down
11 changes: 2 additions & 9 deletions test/controllers/api/v2/scripts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ class ScriptsControllerTest < ActionDispatch::IntegrationTest
@code_hash = "0x00000000000000000000000000000000000000000000000000545950455f4944"
@hash_type = "type"
@block = create :block
@contract = create :contract, code_hash: @code_hash, hash_type: @hash_type
@script = create :script, contract_id: @contract.id
@type_script = create :type_script, code_hash: @code_hash, hash_type: @hash_type, script_id: @script.id
@type_script = create :type_script, code_hash: @code_hash, hash_type: @hash_type
@cell_output1 = create :cell_output, :with_full_transaction, block: @block
@cell_output2 = create :cell_output, :with_full_transaction, block: @block
@cell_output3 = create :cell_output, :with_full_transaction, block: @block
create :deployed_cell, contract_id: @contract.id, cell_output_id: @cell_output1.id
create :deployed_cell, contract_id: @contract.id, cell_output_id: @cell_output2.id
create :deployed_cell, contract_id: @contract.id, cell_output_id: @cell_output3.id
create :referring_cell, contract_id: @contract.id, cell_output_id: @cell_output1.id, ckb_transaction_id: @cell_output1.ckb_transaction_id
create :referring_cell, contract_id: @contract.id, cell_output_id: @cell_output2.id, ckb_transaction_id: @cell_output2.ckb_transaction_id
create :referring_cell, contract_id: @contract.id, cell_output_id: @cell_output3.id, ckb_transaction_id: @cell_output3.ckb_transaction_id
@contract = create :contract, type_hash: @code_hash, hash_type: @hash_type, deployed_cell_output_id: @cell_output1.id
end

test "should get ckb_transactions" do
Expand Down
2 changes: 1 addition & 1 deletion test/factories/cell_dependency.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :cell_dependency do
dep_type { :dep_group }
dep_type { :code }
end
end
6 changes: 6 additions & 0 deletions test/factories/cell_deps_out_point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :cell_deps_out_point do
contract_cell_id { 1 }
deployed_cell_output_id { 1 }
end
end
6 changes: 3 additions & 3 deletions test/factories/contract.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FactoryBot.define do
factory :contract do
code_hash { "0x#{SecureRandom.hex(32)}" }
hash_type { "type" }
deployed_args { "0x#{SecureRandom.hex(32)}" }
role { "type_script" }
Expand All @@ -12,6 +11,7 @@
total_referring_cells_capacity { SecureRandom.random_number(10**10) }
ckb_transactions_count { SecureRandom.random_number(10**10) }
addresses_count { SecureRandom.random_number(100_000_000) }
type_hash { "0x#{SecureRandom.hex(32)}" }

after(:create) do |contract, _eval|
tx = create :ckb_transaction, :with_single_output
Expand All @@ -22,8 +22,8 @@
when "data"
co.update data_hash: contract.code_hash
end
script = create :script, contract_id: contract.id, is_contract: true
contract.deployed_cells.create cell_output_id: co.id
contract.deployed_cell_output_id = co.id
contract.save
end
end
end
20 changes: 4 additions & 16 deletions test/models/cell_dependency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,22 @@

class CellDependencyTest < ActiveSupport::TestCase
context "associations" do
should belong_to(:script)
should belong_to(:cell_output)
should belong_to(:ckb_transaction)
end

setup do
@contract = create :contract
@block = create(:block, :with_block_hash)
@ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, block: @block)
@cell_output = create :cell_output, :with_full_transaction, block: @block
@script = create :script
@cell_dependency = create :cell_dependency, contract_id: @contract.id, ckb_transaction_id: @ckb_transaction.id, contract_cell_id: @cell_output.id,
script_id: @script.id
@contract = create :contract, deployed_cell_output_id: @cell_output.id
@cell_dependency = create :cell_dependency, ckb_transaction_id: @ckb_transaction.id, contract_cell_id: @cell_output.id
create :cell_deps_out_point, contract_cell_id: @cell_output.id, deployed_cell_output_id: @cell_output.id
end

test "it should create contract" do
assert_equal @contract.id, @cell_dependency.contract_id
assert_equal "dep_group", @cell_dependency.dep_type
assert_equal "code", @cell_dependency.dep_type
assert_equal @cell_output.id, @cell_dependency.contract_cell_id
assert_equal @ckb_transaction.id, @cell_dependency.ckb_transaction_id
end

test "it should update contract" do
@cell_dependency.update dep_type: :dep_group
assert_equal "dep_group", @cell_dependency.dep_type
end

test "it should belongs_to contract" do
assert_equal @contract, @cell_dependency.contract
end
end
18 changes: 2 additions & 16 deletions test/models/contract_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class ContractTest < ActiveSupport::TestCase
end

context "associations" do
should have_many(:referring_cells)
should have_many(:deployed_cells)
should have_many(:scripts)
should have_many(:ckb_transactions)
should have_many(:cell_dependencies)
end

Expand All @@ -25,7 +21,8 @@ class ContractTest < ActiveSupport::TestCase
end

test "it should update contract" do
@contract.update deprecated: true, verified: true, hash_type: 'type1', code_hash: '0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a81', name: 'CKB COIN TEST1', role: 'lock_script', symbol: 'TTF1', deployed_args: '0x284c65a608e8e280aaa9c119a1a8fe0463a171511', description: 'Source Code is a script which allows a group of users to sign a single transaction.'
@contract.update deprecated: true, verified: true, hash_type: "type1", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a81", name: "CKB COIN TEST1",
role: "lock_script", symbol: "TTF1", deployed_args: "0x284c65a608e8e280aaa9c119a1a8fe0463a171511", description: "Source Code is a script which allows a group of users to sign a single transaction."
assert_equal true, @contract.verified
assert_equal true, @contract.deprecated
assert_equal "type1", @contract.hash_type
Expand All @@ -36,15 +33,4 @@ class ContractTest < ActiveSupport::TestCase
assert_equal "CKB COIN TEST1", @contract.name
assert_equal "TTF1", @contract.symbol
end
test "it should create initial data" do
Script.delete_all
LockScript.delete_all
TypeScript.delete_all
create :lock_script
create :type_script
Script.create_initial_data
Contract.create_initial_data
assert_equal 3, Contract.count
end

end
Loading

0 comments on commit 8721f00

Please sign in to comment.