tests

package
v0.0.0-...-a3dd185 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

Testing

Mocks

Mocks are generated using mockery.

To regenerate gateway mock go to the gateway directory /pkg/flowkit/gateway and run the command:

mockery --name=Gateway --output ../tests/mocks 

Documentation

Index

Constants

View Source
const (
	GetAccountFunc            = "GetAccount"
	SendSignedTransactionFunc = "SendSignedTransaction"
	GetCollectionFunc         = "GetCollection"
	GetTransactionResultFunc  = "GetTransactionResult"
	GetEventsFunc             = "GetEvents"
	GetLatestBlockFunc        = "GetLatestBlock"
	GetBlockByHeightFunc      = "GetBlockByHeight"
	GetBlockByIDFunc          = "GetBlockByID"
	ExecuteScriptFunc         = "ExecuteScript"
	GetTransactionFunc        = "GetTransaction"
)

Variables

View Source
var ContractA = Resource{
	Name:     "ContractA",
	Filename: "contractA.cdc",
	Source:   []byte(`pub contract ContractA {}`),
}
View Source
var ContractAA = Resource{
	Name:     "ContractAA",
	Filename: "contractAA.cdc",
	Source:   []byte(`pub contract ContractAA {}`),
}
View Source
var ContractB = Resource{
	Name:     "ContractB",
	Filename: "contractB.cdc",
	Source: []byte(`
		import ContractA from "./contractA.cdc"
		pub contract ContractB {}
	`),
}
View Source
var ContractBB = Resource{
	Name:     "ContractBB",
	Filename: "contractBB.cdc",
	Source: []byte(`
		import "ContractAA"
		pub contract ContractB {}
	`),
}
View Source
var ContractC = Resource{
	Name:     "ContractC",
	Filename: "contractC.cdc",
	Source: []byte(`
		import ContractB from "./contractB.cdc"
		import ContractA from "./contractA.cdc"

		pub contract ContractC {
			pub let x: String
			init(x: String) {
				self.x = x
			}
		}
	`),
}
View Source
var ContractCC = Resource{
	Name:     "ContractCC",
	Filename: "contractCC.cdc",
	Source: []byte(`
		import "ContractBB"
		import "ContractAA"

		pub contract ContractC {
			pub let x: String
			init(x: String) {
				self.x = x
			}
		}
	`),
}
View Source
var ContractEvents = Resource{
	Name:     "ContractEvents",
	Filename: "contractEvents.cdc",
	Source: []byte(`
		pub contract ContractEvents {
			pub struct S {
				pub var x: Int
				pub var y: String
				
				init(x: Int, y: String) {
					self.x = x
					self.y = y
				}
			}

			pub event EventA(x: Int)
			pub event EventB(x: Int, y: Int)
			pub event EventC(x: UInt8)
			pub event EventD(x: String)
			pub event EventE(x: UFix64) 
			pub event EventF(x: Address)
			pub event EventG(x: [UInt8])
			pub event EventH(x: [[UInt8]])
			pub event EventI(x: {String: Int})
			pub event EventJ(x: S)
			
			init() {
				emit EventA(x: 1)				
				emit EventB(x: 4, y: 2)	
				emit EventC(x: 1)
				emit EventD(x: "hello")
				emit EventE(x: 10.2)
				emit EventF(x: 0x436164656E636521)
				emit EventG(x: "hello".utf8)
				emit EventH(x: ["hello".utf8, "world".utf8])
				emit EventI(x: { "hello": 1337 })
				emit EventJ(x: S(x: 1, y: "hello world"))
			}
		}
	`),
}
View Source
var ContractFooCoverage = Resource{
	Name:     "FooContract",
	Filename: "FooContract.cdc",
	Source: []byte(`
		pub contract FooContract {
			pub let specialNumbers: {Int: String}

			init() {
				self.specialNumbers = {
					1729: "Harshad",
					8128: "Harmonic",
					41041: "Carmichael"
				}
			}

			pub fun addSpecialNumber(_ n: Int, _ trait: String) {
				self.specialNumbers[n] = trait
			}

			pub fun getIntegerTrait(_ n: Int): String {
				if n < 0 {
					return "Negative"
				} else if n == 0 {
					return "Zero"
				} else if n < 10 {
					return "Small"
				} else if n < 100 {
					return "Big"
				} else if n < 1000 {
					return "Huge"
				}

				if self.specialNumbers.containsKey(n) {
					return self.specialNumbers[n]!
				}

				return "Enormous"
			}
		}
	`),
}
View Source
var ContractHelloString = Resource{
	Name:     "Hello",
	Filename: "contractHello.cdc",
	Source: []byte(`
		pub contract Hello {
			pub let greeting: String
			init() {
				self.greeting = "Hello, World!"
			}
			pub fun hello(): String {
				return self.greeting
			}
		}
	`),
}
View Source
var ContractSimple = Resource{
	Name:     "Simple",
	Filename: "contractSimple.cdc",
	Source: []byte(`
		pub contract Simple {}
	`),
}
View Source
var ContractSimpleUpdated = Resource{
	Name:     "Simple",
	Filename: "contractSimpleUpdated.cdc",
	Source: []byte(`
		pub contract Simple {
			pub fun newFunc() {}
		}
	`),
}
View Source
var ContractSimpleWithArgs = Resource{
	Name:     "Simple",
	Filename: "contractArgs.cdc",
	Source: []byte(`
		pub contract Simple {
			pub let id: UInt64
			init(initId: UInt64) {
				self.id = initId
			}
		}
	`),
}
View Source
var ScriptArgString = Resource{
	Filename: "scriptArg.cdc",
	Source: []byte(`
		pub fun main(name: String): String {
		  return "Hello ".concat(name)
		}
	`),
}
View Source
var ScriptImport = Resource{
	Filename: "scriptImport.cdc",
	Source: []byte(`
		import Hello from "./contractHello.cdc"

		pub fun main(): String {
		  return "Hello ".concat(Hello.greeting)
		}
	`),
}
View Source
var ScriptWithError = Resource{
	Filename: "scriptError.cdc",
	Source: []byte(`
	    	pub fun main(name: String): Strin {
		  return "Hello ".concat(name)
		}
	`),
}
View Source
var SomeFile = Resource{
	Filename: "someFile.cdc",
	Source:   []byte(`This was read from a file!`),
}
View Source
var TestScriptSimple = Resource{
	Filename: "./testScriptSimple.cdc",
	Source: []byte(`
        pub fun testSimple() {
            assert(true)
        }
    `),
}
View Source
var TestScriptSimpleFailing = Resource{
	Filename: "./testScriptSimpleFailing.cdc",
	Source: []byte(`
        pub fun testSimple() {
            assert(false)
        }
    `),
}
View Source
var TestScriptWithCoverage = Resource{
	Filename: "testScriptWithCoverage.cdc",
	Source: []byte(`
		import FooContract from "FooContract.cdc"

		pub let foo = FooContract()

		pub fun testGetIntegerTrait() {
			// Arrange
			let testInputs: {Int: String} = {
				-1: "Negative",
				0: "Zero",
				9: "Small",
				99: "Big",
				999: "Huge",
				1001: "Enormous",
				1729: "Harshad",
				8128: "Harmonic",
				41041: "Carmichael"
			}

			for input in testInputs.keys {
				// Act
				let result = foo.getIntegerTrait(input)

				// Assert
				assert(result == testInputs[input])
			}
		}

		pub fun testAddSpecialNumber() {
			// Act
			foo.addSpecialNumber(78557, "Sierpinski")

			// Assert
			assert("Sierpinski" == foo.getIntegerTrait(78557))
		}
    `),
}
View Source
var TestScriptWithFileRead = Resource{
	Filename: "testScriptWithFileRead.cdc",
	Source: []byte(`
        import Test

        pub fun testSimple() {
            let content = Test.readFile("./someFile.cdc")
            assert(content == "This was read from a file!")
        }
    `),
}
View Source
var TestScriptWithImport = Resource{
	Filename: "testScriptWithImport.cdc",
	Source: []byte(`
        import Hello from "contractHello.cdc"

        pub fun testSimple() {
            let hello = Hello()
            assert(hello.greeting == "Hello, World!")
        }
    `),
}
View Source
var TestScriptWithRelativeImports = Resource{
	Filename: "testScriptWithRelativeImport.cdc",
	Source: []byte(`
        import FooContract from "../contracts/FooContract.cdc"
        import Hello from "../contracts/contractHello.cdc"

        pub fun testSimple() {
            let hello = Hello()
            assert(hello.greeting == "Hello, World!")

            let fooContract = FooContract()
            assert("Carmichael" == fooContract.getIntegerTrait(41041))
        }
    `),
}
View Source
var TransactionArgString = Resource{
	Filename: "transactionArg.cdc",
	Source: []byte(`
		transaction(greeting: String) {
			let guest: Address
			
			prepare(authorizer: AuthAccount) {
				self.guest = authorizer.address
			}
			
			execute {
				log(greeting.concat(",").concat(self.guest.toString()))
			}
		}
	`),
}
View Source
var TransactionImports = Resource{
	Filename: "transactionImport.cdc",
	Source: []byte(`
		import Hello from "./contractHello.cdc"
		
		transaction() {
			prepare(authorizer: AuthAccount) {}
			execute {
				Hello.hello()
			}
		}
	`),
}
View Source
var TransactionMultipleDeclarations = Resource{
	Filename: "transactionMultipleDec.cdc",
	Source: []byte(`
		pub fun dummy(_ address: Address): Void {}

		transaction() {
			prepare(authorizer: AuthAccount) {}
		}
	`),
}
View Source
var TransactionSimple = Resource{
	Filename: "transactionSimple.cdc",
	Source: []byte(`
		transaction() { }
	`),
}
View Source
var TransactionSingleAuth = Resource{
	Filename: "transactionAuth1.cdc",
	Source: []byte(`
		transaction() {
			prepare(authorizer: AuthAccount) {}
		}
	`),
}
View Source
var TransactionTwoAuth = Resource{
	Filename: "transactionAuth2.cdc",
	Source: []byte(`
		transaction() {
			prepare(auth1: AuthAccount, auth2: AuthAccount) {}
		}
	`),
}

Functions

func Alice

func Alice() *flowkit.Account

func Bob

func Bob() *flowkit.Account

func Charlie

func Charlie() *flowkit.Account

func Donald

func Donald() *flowkit.Account

func HashAlgos

func HashAlgos() []crypto.HashAlgorithm

func NewAccountCreateResult

func NewAccountCreateResult(address flow.Address) *flow.TransactionResult

func NewAccountWithAddress

func NewAccountWithAddress(address string) *flow.Account

func NewBlock

func NewBlock() *flow.Block

func NewCollection

func NewCollection() *flow.Collection

func NewEvent

func NewEvent(index int, eventId string, fields []cadence.Field, values []cadence.Value) *flow.Event

func NewTransaction

func NewTransaction() *flow.Transaction

func NewTransactionResult

func NewTransactionResult(events []flow.Event) *flow.TransactionResult

func PrivKeys

func PrivKeys() []crypto.PrivateKey

func PubKeys

func PubKeys() []crypto.PublicKey

func ReaderWriter

func ReaderWriter() (afero.Afero, afero.Fs)

func SigAlgos

func SigAlgos() []crypto.SignatureAlgorithm

Types

type Resource

type Resource struct {
	Name     string
	Filename string
	Source   []byte
}

type TestGateway

type TestGateway struct {
	Mock                  *mocks.Gateway
	SendSignedTransaction *mock.Call
	GetAccount            *mock.Call
	GetCollection         *mock.Call
	GetTransactionResult  *mock.Call
	GetEvents             *mock.Call
	GetLatestBlock        *mock.Call
	GetBlockByHeight      *mock.Call
	GetBlockByID          *mock.Call
	ExecuteScript         *mock.Call
	GetTransaction        *mock.Call
}

func DefaultMockGateway

func DefaultMockGateway() *TestGateway

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL