1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

added unit tests, added code coverage

This commit is contained in:
Sebastian Gerske 2018-10-09 15:24:36 +02:00
parent 2d7cb27b37
commit bac7c9e4f0
14 changed files with 1522 additions and 48 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
node_modules/
dist/
test/**/*.js
src/**/*.js
src/**/*.js
.nyc_output/
coverage/

1303
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
"description": "JavaScript client library for Ocean Protocol",
"main": "dist/squid.js",
"scripts": {
"test": "mocha -r ts-node/register test/**/*.test.ts",
"test": "nyc mocha",
"linkt": "tslint -c tslint.json 'src/**/*.ts'",
"start": "tsc -w",
"build": "tsc",
@ -13,6 +13,23 @@
"release-major": "./node_modules/release-it/bin/release-it.js major --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"prepublishOnly": "npm run build"
},
"nyc": {
"include": [
"src/**/*.ts"
],
"extension": [
".ts"
],
"require": [
"ts-node/register"
],
"reporter": [
"text-summary",
"html"
],
"sourceMap": true,
"instrument": true
},
"repository": {
"type": "git",
"url": "git+https://github.com/oceanprotocol/squid-js.git"
@ -43,6 +60,8 @@
"@types/node": "^8.10.34",
"chai": "^4.2.0",
"mocha": "^5.2.0",
"nyc": "^13.0.1",
"source-map-support": "^0.5.9",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "^3.1.1"

6
src/models/Account.ts Normal file
View File

@ -0,0 +1,6 @@
import Balance from "./Balance"
export default class Account {
public name: string
public balance: Balance
}

4
src/models/Balance.ts Normal file
View File

@ -0,0 +1,4 @@
export default class Balance {
public eth: number
public ocn: number
}

View File

@ -1,5 +1,5 @@
import BigNumber from "bignumber.js"
import Keeper from "../keeper/Keeper"
import AccountModel from "../models/Account"
import OceanBase from "./OceanBase"
export default class Account extends OceanBase {
@ -21,16 +21,18 @@ export default class Account extends OceanBase {
public async list() {
const {web3Helper} = this.keeper
return Promise.all((await web3Helper.getAccounts()).map(async (account: string) => {
// await ocean.market.requestTokens(account, 1000)
return {
name: account,
balance: {
eth: await this.getEthBalance(account),
ocn: await this.getTokenBalance(account),
},
}
}))
const ethAccounts = await web3Helper.getAccounts()
return Promise.all(ethAccounts
.map(async (account: string) => {
// await ocean.market.requestTokens(account, 1000)
return {
name: account,
balance: {
eth: await this.getEthBalance(account),
ocn: await this.getTokenBalance(account),
},
} as AccountModel
}))
}
// Transactions with gas cost

View File

@ -1,4 +1,5 @@
import Keeper from "../keeper/Keeper"
import AssetModel from "../models/Asset"
import Logger from "../utils/Logger"
import OceanBase from "./OceanBase"
@ -10,18 +11,22 @@ export default class Asset extends OceanBase {
}
public async registerAsset(name: string, description: string,
price: number, publisherAddress: string): Promise<string> {
price: number, publisherAddress: string): Promise<AssetModel> {
const {market} = this.keeper
// generate an id
const assetId = await market.generateId(name + description)
Logger.log("Registering: ", assetId)
Logger.log(`Registering: ${assetId} with price ${price}`)
// register asset in the market
const result = await market.register(assetId, price, publisherAddress)
Logger.log("Registered: ", assetId, "in block", result.blockNumber)
Logger.log("Registered:", assetId, "in block", result.blockNumber)
return assetId
return {
assetId,
publisherId: publisherAddress,
price,
} as AssetModel
}
}

View File

@ -7,4 +7,4 @@ export default class OceanBase {
constructor(keeper: Keeper) {
this.keeper = keeper
}
}
}

View File

@ -8,7 +8,9 @@ import Logger from "../../src/utils/Logger"
let keeper: Keeper
before(async () => {
const config: Config = {nodeUri: "http://localhost:8545"} as Config
const config: Config = {
nodeUri: "http://localhost:8545",
} as Config
const web3Helper = new Web3Helper(config)
await ContractHandler.deployContracts(web3Helper)
keeper = await Keeper.getInstance(config, web3Helper)

View File

@ -1,28 +0,0 @@
import ContractHandler from "../../src/keeper/ContractHandler"
import Token from "../../src/keeper/Token"
import Web3Helper from "../../src/keeper/Web3Helper"
import Config from "../../src/models/Config"
import Logger from "../../src/utils/Logger"
let token: Token
before(async () => {
const config: Config = {nodeUri: "http://localhost:8545"} as Config
const web3Helper = new Web3Helper(config)
await ContractHandler.deployContracts(web3Helper)
token = await Token.getInstance(config, web3Helper)
})
describe("Token", () => {
describe("#balanceOf()", () => {
it("should get balance", async () => {
const balance = await token.balanceOf("0xB0EdD05A5874c5c1Fcd6bCB4E52143fB7134b7EE")
Logger.log(balance)
})
})
})

5
test/mocha.opts Normal file
View File

@ -0,0 +1,5 @@
--require ts-node/register
--require source-map-support/register
--full-trace
--bail
test/**/*.test.ts

View File

@ -0,0 +1,75 @@
import * as assert from "assert"
import BigNumber from "bignumber.js"
import ContractHandler from "../../src/keeper/ContractHandler"
import Keeper from "../../src/keeper/Keeper"
import Web3Helper from "../../src/keeper/Web3Helper"
import Config from "../../src/models/Config"
import Account from "../../src/ocean/Account"
let keeper: Keeper
const config: Config = {
nodeUri: "http://localhost:8545",
} as Config
const web3Helper = new Web3Helper(config)
before(async () => {
await ContractHandler.deployContracts(web3Helper)
keeper = await Keeper.getInstance(config, web3Helper)
})
describe("Account", () => {
describe("#getTokenBalance()", () => {
it("should get initial balance", async () => {
const account = new Account(keeper)
const accounts = await account.list()
const addr = accounts[1].name
const balance = await account.getTokenBalance(addr)
assert(0 === balance)
})
it("should get balance the correct balance", async () => {
const account = new Account(keeper)
const amount: number = 100
const accounts = await account.list()
const addr = accounts[0].name
await account.requestTokens(amount, addr)
const balance = await account.getTokenBalance(addr)
assert(amount === balance)
})
})
describe("#getEthBalance()", () => {
it("should get initial balance", async () => {
const account = new Account(keeper)
const accounts = await account.list()
const addr = accounts[5].name
const balance = await account.getEthBalance(addr)
const web3 = web3Helper.getWeb3()
assert(Number(web3.utils.toWei("100", "ether")) === balance)
})
})
describe("#list()", () => {
it("should list accounts", async () => {
const account = new Account(keeper)
const accounts = await account.list()
assert(10 === accounts.length)
assert(0 === accounts[5].balance.ocn)
assert("string" === typeof accounts[0].name)
})
})
})

77
test/ocean/Asset.test.ts Normal file
View File

@ -0,0 +1,77 @@
import * as assert from "assert"
import ContractHandler from "../../src/keeper/ContractHandler"
import Keeper from "../../src/keeper/Keeper"
import Web3Helper from "../../src/keeper/Web3Helper"
import AssetModel from "../../src/models/Asset"
import Config from "../../src/models/Config"
import Account from "../../src/ocean/Account"
import Asset from "../../src/ocean/Asset"
let keeper: Keeper
const config: Config = {
nodeUri: "http://localhost:8545",
} as Config
const web3Helper = new Web3Helper(config)
before(async () => {
await ContractHandler.deployContracts(web3Helper)
keeper = await Keeper.getInstance(config, web3Helper)
})
describe("Asset", () => {
describe("#register()", () => {
it("should register asset", async () => {
const account = new Account(keeper)
const accounts = await account.list()
const addr = accounts[0].name
const name = "Test Asset"
const description = "This asset is pure owange"
const price = 100
const asset = new Asset(keeper)
const finalAsset: AssetModel = await asset.registerAsset(name, description, price, addr)
assert(finalAsset.assetId.length === 66)
assert(finalAsset.assetId.startsWith("0x"))
assert(finalAsset.publisherId === addr)
assert(finalAsset.price === price)
})
})
describe("#isAssetActive()", () => {
it("should return true on new asset", async () => {
const account = new Account(keeper)
const accounts = await account.list()
const addr = accounts[0].name
const name = "Test Asset 2"
const description = "This asset is pure owange"
const price = 100
const asset = new Asset(keeper)
const finalAsset = await asset.registerAsset(name, description, price, addr)
const isAssetActive = await asset.isAssetActive(finalAsset.assetId)
assert(true === isAssetActive)
})
it("should return false on unknown asset", async () => {
const account = new Account(keeper)
const accounts = await account.list()
const addr = accounts[0].name
const asset = new Asset(keeper)
const isAssetActive = await asset.isAssetActive("0x0000")
assert(false === isAssetActive)
})
})
})

View File

@ -8,7 +8,9 @@ import Logger from "../../src/utils/Logger"
let ocean: Ocean
before(async () => {
const config: Config = {nodeUri: "http://localhost:8545"} as Config
const config: Config = {
nodeUri: "http://localhost:8545",
} as Config
const web3Helper = new Web3Helper(config)
await ContractHandler.deployContracts(web3Helper)
ocean = await Ocean.getInstance(config)