Skip to content

Commit

Permalink
Update Cadence tests to latest API features
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Oct 31, 2023
1 parent 03914e9 commit d435ae8
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 238 deletions.
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
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"

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

0 comments on commit d435ae8

Please sign in to comment.