1

I have a contract like this: PublishService.sol, the strings.sol is taken from here.

pragma solidity ^0.4.0;

import "./strings.sol";
contract PublishService
{

using strings for *;
struct ServiceListStruct {
string _cloudID;
address _gatewayID;
string _serviceProducerID;
string _serviceProducerMetadata;
string _serviceConsumerMetaAttr;
}
ServiceListStruct[] BCServiceList;

function countEntries() public returns (uint)
{
    return BCServiceList.length;
}

function addEntry(string cloudID, address gatewayID, string spID, string spMetadata, string scMetaAttr) public returns (uint)
{
require(msg.sender==gatewayID);
var sls = ServiceListStruct(cloudID, gatewayID, spID, spMetadata, scMetaAttr);
BCServiceList.push(sls);
return 1;
}

function deleteEntry(string cloudID, address gatewayID, string spID, string spMetadata, string scMetaAttr) public returns (uint)
{
require(msg.sender==gatewayID);
int pos = -1;
for(uint index = 0; index< BCServiceList.length; index++)
{
  if(compareStrings(cloudID, BCServiceList[index]._cloudID))
  {
    if(gatewayID == BCServiceList[index]._gatewayID)
    {
      if(compareStrings(spID, BCServiceList[index]._serviceProducerID))
      {
        if(compareStrings(spMetadata, BCServiceList[index]._serviceProducerMetadata))
        {
          if(compareStrings(scMetaAttr, BCServiceList[index]._serviceConsumerMetaAttr))
          {
            pos = int(index);
          }
        }
      }
    }
  }
}
if(pos > -1)
{
  for(uint i = uint(pos); i < BCServiceList.length -1 ; i++)
  {
    BCServiceList[i] = BCServiceList[i+1];
    delete BCServiceList[BCServiceList.length-1];
    BCServiceList.length--;
  }
return 1;
}
else
return 0;
}

function compareStrings(string s1, string s2) internal returns(bool)
{
var slice1 = s1.toSlice();
var slice2 = s2.toSlice();
var result = slice1.compare(slice2);
if(result == 0)
return true;
else
return false;
}
}    

And I tried to test it using Remix IDE and it works as expected.

  1. tried to add a service from an account other than the one deployed the contract and deleted the service entry using the same account and it worked.
  2. tried to add service from one account and deleted from other account and the require statement failed as expected

But, when I try to test it using truffle, it fails. The test file is as follows:

const PublishService = artifacts.require('PublishService');
contract('PublishService', function(accounts) {
  it("addEntry function call from a valid account", function() {
    return PublishService.new({from: accounts[0]}).then(instance => { return instance.addEntry.call("LC1","0xca6e598bac8319860c663926f98096fdb279e55e", "LC1SP1", "SP1MD", "SC_ATTR",{"from": accounts[1]}); }).then(result => {
      assert.equal(result, 1, "[E] addEntry function test failed");
    });
  });
  it("deleteEntry function call from a valid account", function() {
    return PublishService.new({from: accounts[0]}).then(instance => { return instance.deleteEntry.call("LC1","0xca6e598bac8319860c663926f98096fdb279e55e", "LC1SP1", "SP1MD", "SC_ATTR",{"from": accounts[1]}); }).then(result => {
      assert.equal(result, 1, "[E] deleteEntry function test failed");
    });
  });

});

The second parameter of the call function is the account1 id from the testrpc network Here is the result of it.

enter image description here

The addEntry function succeeds but the deleteEntry failed. I can't figure out the reason. Any help is appreciated.

1 Answer 1

0

I suspect you are trying to compare a Bignumber object coming from a transaction call in web3 with a number literal

assert.equal(result, 1, "[E] deleteEntry function test failed");

You probably should use

result.toNumber()

instead of

result

when comparing with JS integers.

See this question and answer to elaborate on BigNumber methods.

What are *C*, *E* and *S* properties in message call return object?

1
  • I am returning either 0 or 1 in the deleteEntry function as shown in above piece of code
    – K Kolluru
    Mar 26, 2018 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.