Skip to content
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

Update Cadence tests to latest API features #192

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Install Flow CLI
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.3.1
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0
- name: Flow CLI Version
run: flow version
- name: Update PATH
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
test:
$(MAKE) generate -C lib/go
$(MAKE) test -C lib/go
flow test --cover tests/test_example_nft.cdc
flow test --cover --covercode="contracts" tests/test_*.cdc

.PHONY: ci
ci:
$(MAKE) ci -C lib/go
flow test --cover tests/test_example_nft.cdc
flow test --cover --covercode="contracts" tests/test_*.cdc
37 changes: 32 additions & 5 deletions flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,45 @@
"source": "./contracts/NonFungibleToken.cdc",
"aliases": {
"emulator": "0xf8d6e0586b0a20c7",
"testing": "0x0000000000000001",
"testnet": "0x631e88ae7f1d7c20",
"mainnet": "0x1d7e57aa55817448"
}
},
"MetadataViews": "./contracts/MetadataViews.cdc",
"ViewResolver": "./contracts/ViewResolver.cdc",
"ExampleNFT": "./contracts/ExampleNFT.cdc",
"FungibleToken": "./contracts/utility/FungibleToken.cdc",
"NFTForwarding": "./contracts/utility/NFTForwarding.cdc"
"MetadataViews": {
"source": "./contracts/MetadataViews.cdc",
"aliases": {
"testing": "0x0000000000000001"
}
},
"ViewResolver": {
"source": "./contracts/ViewResolver.cdc",
"aliases": {
"testing": "0x0000000000000001"
}
},
"ExampleNFT": {
"source": "./contracts/ExampleNFT.cdc",
"aliases": {
"testing": "0x0000000000000007"
}
},
"FungibleToken": {
"source": "./contracts/utility/FungibleToken.cdc",
"aliases": {
"testing": "0x0000000000000002"
}
},
"NFTForwarding": {
"source": "./contracts/utility/NFTForwarding.cdc",
"aliases": {
"testing": "0x0000000000000007"
}
}
},
"networks": {
"emulator": "127.0.0.1:3569",
"testing": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"testnet": "access.devnet.nodes.onflow.org:9000"
},
Expand Down
66 changes: 33 additions & 33 deletions lib/go/templates/internal/assets/assets.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions scripts/borrow_nft.cdc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This script borrows an NFT from a collection

import NonFungibleToken from "NonFungibleToken"
import ExampleNFT from "ExampleNFT"
import "NonFungibleToken"
import "ExampleNFT"
Comment on lines +3 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why changing these to this kind of import doesn't break the go tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the specific script is not part of a test case? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant for all of them. The go tests expect everything to be in the form of import NonFungibleToken from "NonFungibleToken" and it replaces the placeholder with an actual address, so in this case it would just replace it with import 0x.... Is that valid if there is only one contract at that address?

It is used in the tests here: https://github.com/onflow/flow-nft/blob/master/lib/go/test/nft_test.go#L56

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @joshuahannan, good point.

I did check how that works, and the end result of the script looks like:

Script:  // This script borrows an NFT from a collection

import 0x01cf0e2f2f715450
import 0xe03daebed8ca0615

pub fun main(address: Address, id: UInt64) {
    let account = getAccount(address)

    let collectionRef = account
        .getCapability(ExampleNFT.CollectionPublicPath)
        .borrow<&{NonFungibleToken.CollectionPublic}>()
        ?? panic("Could not borrow capability from public collection")

    // Borrow a reference to a specific NFT in the collection
    let _ = collectionRef.borrowNFT(id: id)
}

An import of the form import 0xe03daebed8ca0615 means import every contract from that address. However, you can't have the same import for an address twice:

import 0xe03daebed8ca0615
import 0xe03daebed8ca0615

This would throw an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, that makes sense. So fine for now, but we might need to update it in the future if we decide to have contracts at the same address


pub fun main(address: Address, id: UInt64) {
let account = getAccount(address)
Expand Down
4 changes: 2 additions & 2 deletions scripts/get_collection_ids.cdc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Script to get NFT IDs in an account's collection

import NonFungibleToken from "NonFungibleToken"
import ExampleNFT from "ExampleNFT"
import "NonFungibleToken"
import "ExampleNFT"

pub fun main(address: Address, collectionPublicPath: PublicPath): [UInt64] {
let account = getAccount(address)
Expand Down
4 changes: 2 additions & 2 deletions scripts/get_collection_length.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import NonFungibleToken from "NonFungibleToken"
import ExampleNFT from "ExampleNFT"
import "NonFungibleToken"
import "ExampleNFT"

pub fun main(address: Address): Int {
let account = getAccount(address)
Expand Down
4 changes: 2 additions & 2 deletions scripts/get_contract_storage_path.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MetadataViews from "MetadataViews"
import ViewResolver from "ViewResolver"
import "MetadataViews"
import "ViewResolver"

pub fun main(addr: Address, name: String): StoragePath? {
let t = Type<MetadataViews.NFTCollectionData>()
Expand Down
4 changes: 2 additions & 2 deletions scripts/get_nft_metadata.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// This script gets all the view-based metadata associated with the specified NFT
/// and returns it as a single struct

import ExampleNFT from "ExampleNFT"
import MetadataViews from "MetadataViews"
import "ExampleNFT"
import "MetadataViews"

pub struct NFT {
pub let name: String
Expand Down
4 changes: 2 additions & 2 deletions scripts/get_nft_view.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ExampleNFT from "ExampleNFT"
import MetadataViews from "MetadataViews"
import "ExampleNFT"
import "MetadataViews"

pub struct NFTView {
pub let id: UInt64
Expand Down
2 changes: 1 addition & 1 deletion scripts/get_total_supply.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ExampleNFT from "ExampleNFT"
import "ExampleNFT"

pub fun main(): UInt64 {
return ExampleNFT.totalSupply
Expand Down
16 changes: 4 additions & 12 deletions tests/scripts/get_example_nft_views.cdc
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
/// This script checks all the supported views from
/// the ExampleNFT contract. Used for testing only.

import ExampleNFT from "ExampleNFT"
import MetadataViews from "MetadataViews"
import "ExampleNFT"
import "MetadataViews"

pub fun main(): Bool {
let views = ExampleNFT.getViews()

let expected = [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
]
assert(expected == views)

return true
pub fun main(): [Type] {
return ExampleNFT.getViews()
}
16 changes: 8 additions & 8 deletions tests/scripts/get_nft_metadata.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// This script checks all views from MetadataViews for
/// a given NFT. Used for testing only.

import ExampleNFT from "ExampleNFT"
import MetadataViews from "MetadataViews"
import "ExampleNFT"
import "MetadataViews"

pub struct NFT {
pub let name: String
Expand Down Expand Up @@ -150,19 +150,19 @@ pub fun main(address: Address, id: UInt64): Bool {
assert("NFT Name" == nftMetadata.name)
assert("NFT Description" == nftMetadata.description)
assert("NFT Thumbnail" == nftMetadata.thumbnail)
assert(Address(0x01cf0e2f2f715450) == nftMetadata.owner)
assert("A.01cf0e2f2f715450.ExampleNFT.NFT" == nftMetadata.type)
assert(Address(0x0000000000000007) == nftMetadata.owner)
assert("A.0000000000000007.ExampleNFT.NFT" == nftMetadata.type)
assert("Creator Royalty" == nftMetadata.royalties[0].description)
assert(Address(0x01cf0e2f2f715450) == nftMetadata.royalties[0].receiver.address)
assert(Address(0x0000000000000007) == nftMetadata.royalties[0].receiver.address)
assert(0.05 == nftMetadata.royalties[0].cut)
assert("https://example-nft.onflow.org/0" == nftMetadata.externalURL)
assert((0 as UInt64) == nftMetadata.serialNumber)
assert(/public/exampleNFTCollection == nftMetadata.collectionPublicPath)
assert(/storage/exampleNFTCollection == nftMetadata.collectionStoragePath)
assert(/private/exampleNFTCollection == nftMetadata.collectionProviderPath)
assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic}" == nftMetadata.collectionPublic)
assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.CollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.Receiver,A.f8d6e0586b0a20c7.MetadataViews.ResolverCollection}" == nftMetadata.collectionPublicLinkedType)
assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.CollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.Provider,A.f8d6e0586b0a20c7.MetadataViews.ResolverCollection}" == nftMetadata.collectionProviderLinkedType)
assert("&A.0000000000000007.ExampleNFT.Collection{A.0000000000000007.ExampleNFT.ExampleNFTCollectionPublic}" == nftMetadata.collectionPublic)
assert("&A.0000000000000007.ExampleNFT.Collection{A.0000000000000007.ExampleNFT.ExampleNFTCollectionPublic,A.0000000000000001.NonFungibleToken.CollectionPublic,A.0000000000000001.NonFungibleToken.Receiver,A.0000000000000001.MetadataViews.ResolverCollection}" == nftMetadata.collectionPublicLinkedType)
assert("&A.0000000000000007.ExampleNFT.Collection{A.0000000000000007.ExampleNFT.ExampleNFTCollectionPublic,A.0000000000000001.NonFungibleToken.CollectionPublic,A.0000000000000001.NonFungibleToken.Provider,A.0000000000000001.MetadataViews.ResolverCollection}" == nftMetadata.collectionProviderLinkedType)
assert("The Example Collection" == nftMetadata.collectionName)
assert("This collection is used as an example to help you develop your next Flow NFT." == nftMetadata.collectionDescription)
assert("https://example-nft.onflow.org" == nftMetadata.collectionExternalURL)
Expand Down
12 changes: 6 additions & 6 deletions tests/scripts/get_nft_view.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// This script checks the NFTView from MetadataViews for
/// a given NFT. Used for testing only.

import ExampleNFT from "ExampleNFT"
import MetadataViews from "MetadataViews"
import "ExampleNFT"
import "MetadataViews"

pub struct NFTView {
pub let id: UInt64
Expand Down Expand Up @@ -118,15 +118,15 @@ pub fun main(address: Address, id: UInt64): Bool {
assert("NFT Description" == nftViewResult.description)
assert("NFT Thumbnail" == nftViewResult.thumbnail)
assert("Creator Royalty" == nftViewResult.royalties[0].description)
assert(Address(0x01cf0e2f2f715450) == nftViewResult.royalties[0].receiver.address)
assert(Address(0x0000000000000007) == nftViewResult.royalties[0].receiver.address)
assert(0.05 == nftViewResult.royalties[0].cut)
assert("https://example-nft.onflow.org/0" == nftViewResult.externalURL)
assert(/public/exampleNFTCollection == nftViewResult.collectionPublicPath)
assert(/storage/exampleNFTCollection == nftViewResult.collectionStoragePath)
assert(/private/exampleNFTCollection == nftViewResult.collectionProviderPath)
assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic}" == nftViewResult.collectionPublic)
assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.CollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.Receiver,A.f8d6e0586b0a20c7.MetadataViews.ResolverCollection}" == nftViewResult.collectionPublicLinkedType)
assert("&A.01cf0e2f2f715450.ExampleNFT.Collection{A.01cf0e2f2f715450.ExampleNFT.ExampleNFTCollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.CollectionPublic,A.f8d6e0586b0a20c7.NonFungibleToken.Provider,A.f8d6e0586b0a20c7.MetadataViews.ResolverCollection}" == nftViewResult.collectionProviderLinkedType)
assert("&A.0000000000000007.ExampleNFT.Collection{A.0000000000000007.ExampleNFT.ExampleNFTCollectionPublic}" == nftViewResult.collectionPublic)
assert("&A.0000000000000007.ExampleNFT.Collection{A.0000000000000007.ExampleNFT.ExampleNFTCollectionPublic,A.0000000000000001.NonFungibleToken.CollectionPublic,A.0000000000000001.NonFungibleToken.Receiver,A.0000000000000001.MetadataViews.ResolverCollection}" == nftViewResult.collectionPublicLinkedType)
assert("&A.0000000000000007.ExampleNFT.Collection{A.0000000000000007.ExampleNFT.ExampleNFTCollectionPublic,A.0000000000000001.NonFungibleToken.CollectionPublic,A.0000000000000001.NonFungibleToken.Provider,A.0000000000000001.MetadataViews.ResolverCollection}" == nftViewResult.collectionProviderLinkedType)
assert("The Example Collection" == nftViewResult.collectionName)
assert("This collection is used as an example to help you develop your next Flow NFT." == nftViewResult.collectionDescription)
assert("https://example-nft.onflow.org" == nftViewResult.collectionExternalURL)
Expand Down
24 changes: 5 additions & 19 deletions tests/scripts/get_views.cdc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// This script checks all the supported views from
/// a given NFT. Used for testing only.

import NonFungibleToken from "NonFungibleToken"
import MetadataViews from "MetadataViews"
import ExampleNFT from "ExampleNFT"
import "NonFungibleToken"
import "MetadataViews"
import "ExampleNFT"

pub fun main(address: Address, id: UInt64): Bool {
pub fun main(address: Address, id: UInt64): [Type] {
let account = getAccount(address)

let collectionRef = account
Expand All @@ -15,19 +15,5 @@ pub fun main(address: Address, id: UInt64): Bool {

// Borrow a reference to a specific NFT in the collection
let nft = collectionRef.borrowNFT(id: id)
let views = nft.getViews()

let expected = [
Type<MetadataViews.Display>(),
Type<MetadataViews.Royalties>(),
Type<MetadataViews.Editions>(),
Type<MetadataViews.ExternalURL>(),
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.Serial>(),
Type<MetadataViews.Traits>()
]
assert(expected == views)

return true
return nft.getViews()
}
9 changes: 6 additions & 3 deletions tests/scripts/resolve_nft_views.cdc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// This script resolves all the supported views from
/// the ExampleNFT contract. Used for testing only.

import ExampleNFT from "ExampleNFT"
import NonFungibleToken from "NonFungibleToken"
import MetadataViews from "MetadataViews"
import "ExampleNFT"
import "NonFungibleToken"
import "MetadataViews"

pub fun main(): Bool {
// Call `resolveView` with invalid Type
Expand All @@ -25,6 +25,9 @@ pub fun main(): Bool {
Type<MetadataViews.NFTCollectionData>()
) as! MetadataViews.NFTCollectionData?)!

// The MetadataViews.NFTCollectionData returns a function (createEmptyCollection),
// so it cannot be the return type of a script. That's why we perform
// the assertions in this script.
assert(ExampleNFT.CollectionStoragePath == collectionData.storagePath)
assert(ExampleNFT.CollectionPublicPath == collectionData.publicPath)
assert(/private/exampleNFTCollection == collectionData.providerPath)
Expand Down
Loading
Loading