-
-
Save rrecuero/ff1f3b63cb637305f81bc5d99873448d to your computer and use it in GitHub Desktop.
Example test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
var Cache = artifacts.require("Cache"); | |
var CacheUser = artifacts.require("CacheUser"); | |
var encoding = require('../../util/encoding.js'); | |
contract('Cache', function(accounts) { | |
var cache; | |
before(function(done) { | |
Cache.deployed().then(function(instance) { | |
cache = instance; | |
done(); | |
}); | |
}) | |
it("should create a cache", function(done) { | |
var mycache; | |
cache.create({from: accounts[0]}) | |
.then(function() { | |
return cache.getCache.call({from: accounts[0]}); | |
}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(myNewCache) { | |
mycache = myNewCache; | |
assert.ok(myNewCache); | |
done(); | |
}); | |
}); | |
it("should not get a cache from account that doesn't have it", function(done) { | |
cache.getCache.call({from: accounts[1]}).then(function(cacheuser) { | |
assert.equal(cacheuser, 0x0); | |
done(); | |
}); | |
}); | |
it("should get no data for empty key", function(done) { | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
return cacheuser.get.call('a', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(res[1], '0x'); | |
done(); | |
}); | |
}); | |
it("should set string value correctly", function(done) { | |
var mycache; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = "hola"; | |
return mycache.set('test20', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('test20', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(encoding.decodeType(res[0], res[1]), 'hola'); | |
done(); | |
}); | |
}); | |
it("should override string value correctly", function(done) { | |
var mycache; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = "hola2"; | |
return mycache.set('test20', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('test20', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(encoding.decodeType(res[0], res[1]), 'hola2'); | |
done(); | |
}); | |
}); | |
it("should override string several times", function(done) { | |
var mycache; | |
var value = "hola2"; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
return mycache.set('test20', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.set('test20', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('test20', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(encoding.decodeType(res[0], res[1]), 'hola2'); | |
done(); | |
}); | |
}); | |
it("should set object correctly", function(done) { | |
var mycache; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = {c: 3}; | |
return mycache.set('testobject', 'object', encoding.encodeType('object', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('testobject', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(encoding.decodeType(res[0], res[1]).c, 3); | |
done(); | |
}); | |
}); | |
it("should set boolean correctly", function(done) { | |
var mycache; | |
var testValue = true; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
return mycache.set('testbool', 'boolean', encoding.encodeType('boolean', testValue), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('testbool', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(encoding.decodeType('boolean', res[1]), testValue); | |
done(); | |
}); | |
}); | |
it("should set int value correctly", function(done) { | |
var mycache; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = 15; | |
return mycache.set('testint', 'number', encoding.encodeType('number', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('testint', {from: accounts[0]}); | |
}).then(function(res) { | |
encoding.decodeType(res[0], res[1]); | |
assert.equal(encoding.decodeType(res[0], res[1]), 15) | |
done(); | |
}); | |
}); | |
it("should set decimal (fixed) value correctly", function(done) { | |
var mycache; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = 15.5; | |
return mycache.set('testdecimal', 'number', encoding.encodeType('number', value), {from: accounts[0]}); | |
}).then(function(res) { | |
return mycache.get.call('testdecimal', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(encoding.decodeType(res[0], res[1]), 15.5); | |
done(); | |
}); | |
}); | |
it("should not be able to set key from other address", function(done) { | |
var mycache; | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = "hola"; | |
return mycache.set('test0', 'string', encoding.encodeType('string', value), {from: accounts[1]}); | |
}).then(function(res) { | |
return mycache.get.call('test0', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(res[1], '0x'); | |
done(); | |
}); | |
}); | |
it("contains should return false for random key", function(done) { | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
return cacheuser.contains.call('petete', {from: accounts[0]}); | |
}).then(function(resp) { | |
assert.equal(resp, false); | |
done(); | |
}); | |
}); | |
it("contains should return true for key that exists", function(done) { | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = "hola"; | |
return mycache.set('testkey', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(result) { | |
return mycache.contains.call('testkey', {from: accounts[0]}); | |
}).then(function(resp) { | |
assert.equal(resp, true); | |
done(); | |
}); | |
}); | |
it("should iterate two keys", function(done) { | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = "hi guy"; | |
return mycache.set('testiteration', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(result) { | |
return mycache.iterate_start.call({from: accounts[0]}); | |
}).then(function(index) { | |
return mycache.iterate_next.call(index, {from: accounts[0]}); | |
}).then(function(indexnext) { | |
return mycache.iterate_get_key.call(indexnext, {from: accounts[0]}); | |
}).then(function(key) { | |
assert.equal(key, 'testobject'); | |
done(); | |
}); | |
}); | |
it("should return memsize", function(done) { | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
return mycache.getMemSize.call({from: accounts [0]}); | |
}).then(function(memsize) { | |
assert.equal(memsize.c[0], 106); | |
done(); | |
}); | |
}); | |
it("should remove the key item", function(done) { | |
cache.getCache.call({from: accounts [0]}).then(function(address) { | |
assert.ok(address); | |
return CacheUser.at(address); | |
}).then(function(cacheuser) { | |
mycache = cacheuser; | |
var value = "hi guy"; | |
return mycache.set('testremove', 'string', encoding.encodeType('string', value), {from: accounts[0]}); | |
}).then(function(result) { | |
return mycache.remove('testremove', {from: accounts[0]}); | |
}).then(function(resp) { | |
return mycache.contains.call('testremove', {from: accounts[0]}); | |
}).then(function(res) { | |
assert.equal(res, false); | |
done(); | |
}); | |
}); | |
it("should delete the user cache", function(done) { | |
cache.deleteCache({from: accounts[0]}).then(function() { | |
cache.getCache.call({from: accounts[0]}).then(function(address) { | |
assert.equal(address, 0x0); | |
done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment