Writing a unit test for a trigger - unit-testing

I need to write a unit test for this piece of code. I am just learning how to write unit tests. My understanding is that I need to start from the outside and work my way in for the if statements. What exactly am I trying to do in the unit test? How would you approach this task? Sorry I am a complete beginner. Thank you.
Trigger BuildComponentBI on Build_Component__c(before insert , before update) {
if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {
//Double CompVer = 0;
double q = 0;
for (Build_Component__c C: Trigger.new) {
if (C.Manual_Override__c == False){
List<Effort_Matrix__c> em = Effort_Matrix__c.getall().values();
q = c.Scale__c;
For(Effort_Matrix__c e:em){
if(e.Component_Name__c == c.Type__c){
if(e.Phase__c == 'Build'){
c.Estimated_Build_Hours__c = e.OOM__c * q;
}
if(e.phase__c == 'Analysis'){
c.Estimated_Analysis_Hours__c = e.OOM__c * q;
}
if(e.phase__c == 'SIT'){
c.Estimated_SIT_Hours__c = e.OOM__c * q;
}
if(e.phase__c == 'Deployment'){
c.Estimated_Deployment_Hours__c = e.OOM__c * q;
}
if(e.phase__c == 'UAT'){
c.Estimated_UAT_Hours__c = e.OOM__c * q;
}
if(e.phase__c == 'Unit Test'){
c.Estimated_Unit_Test_Hours__c = e.OOM__c * q;
}
if(e.phase__c == 'Design'){
c.Estimated_Design_Hours__c = e.OOM__c * q;
}
else{}
}
}
}
}
}
}

You don't test triggers directly.
What you need to do is to create a unit test that inserts and updates the trigger object (Build_Component__c).
Example:
#isTest
private class BuildComponentTest {
#isTest static void testManualOverrideFalseInsert() {
Build_Component__c bc = new Build_Component__c();
bc.Manual_Override__c = false;
insert bc;
}
}
Here you'll be testing the insert scenario when the Manual_Override__c boolean is false. Keep adding tests to cover the other conditions inside the if statement until you get the 100% of coverage.
Try to ask questions on the dedicated Salesforce SE you'll get answers quicker.

Related

Smart Contract not producing results, how do I see both results or get the total count of both results

When I deploy the contract I get the count result of only one entity not both entities. Can you help me? How do I get the result of both entities.
Here is part of the code.
function doVote(bool _choice)
public
inState(State.Voting)
returns (bool voted)
{
bool found = false;
if (bytes(voterRegister[msg.sender].voterName).length != 0
&& !voterRegister[msg.sender].voted){
voterRegister[msg.sender].voted = true;
vote memory v;
v.voterAddress = msg.sender;
v.choice = _choice;
if (_choice){
countResult++; //counting on the go
}
else {
countRe++;
}
votes[totalVote] = v;
totalVote++;
found = true;
}
emit voteDone(msg.sender);
return found;
}
//end votes
function endVote()
public
inState(State.Voting)
onlyOfficial
{
state = State.Ended;
finalResult = countResult; //move result from private countResult to public finalResult
totalFalse = countRe;
emit voteEnded(finalResult, totalFalse);
}
}

Do I need to add some kind of check before continuing when hopping to other void

I'm new(-ish) to C++, and I'm trying to learn something new every day. Today I'm trying to figure this out.
Do I have to check if Valmis == 1 so it won't continue/return before Realtest() is complete?
This is just an example.
int valmis = 0;
void test::Main()
{
//just some basic stuff
do
{
if (!strcmp())
{
//here too
float testing = 0;
printf("Test? 1=do it Else=Nothing\n");
scanf(" %f", &testing);
if (testing == 1)
{
Realtest();
//Realtest needs to be completed before continuing
if (valmis == 1) //Do I need this or does it continue after RealTest() is complete without this?
return (somethingmate = true);
}
else
{
return (somethingmate = true);
}
}
} while();
return (somethingmate = false);
}
void test::Realtest()
{
//doing something here that I need to do before continuing in main/letting somethingmate to become true
valmis = 1; //do i need this?
}

MariaDB Connector C, mysql_stmt_fetch_column() and memory corruption

I'm working on a wrapper for MariaDB Connector C. There is a typical situation when a developer doesn't know a length of a data stored in a field. As I figured out, one of the ways to obtain a real length of the field is to pass a buffer of lengths to mysql_stmt_bind_result and then to fetch each column by calling mysql_stmt_fetch_column. But I can't understand how the function mysql_stmt_fetch_column works because I'm getting a memory corruption and app abortion.
Here is how I'm trying to reach my goal
// preparations here
...
if (!mysql_stmt_execute(stmt))
{
int columnNum = mysql_stmt_field_count(stmt);
if (columnNum > 0)
{
MYSQL_RES* metadata = mysql_stmt_result_metadata(stmt);
MYSQL_FIELD* fields = mysql_fetch_fields(metadata);
MYSQL_BIND* result = new MYSQL_BIND[columnNum];
std::memset(result, 0, sizeof (MYSQL_BIND) * columnNum);
std::vector<unsigned long> lengths;
lengths.resize(columnNum);
for (int i = 0; i < columnNum; ++i)
result[i].length = &lengths[i];
if (!mysql_stmt_bind_result(stmt, result))
{
while (true)
{
int status = mysql_stmt_fetch(stmt);
if (status == 1)
{
m_lastError = mysql_stmt_error(stmt);
isOK = false;
break;
}
else if (status == MYSQL_NO_DATA)
{
isOK = true;
break;
}
for (int i = 0; i < columnNum; ++i)
{
my_bool isNull = true;
if (lengths.at(i) > 0)
{
result[i].buffer_type = fields[i].type;
result[i].is_null = &isNull;
result[i].buffer = malloc(lengths.at(i));
result[i].buffer_length = lengths.at(i);
mysql_stmt_fetch_column(stmt, result, i, 0);
if (!isNull)
{
// here I'm trying to read a result and I'm getting a valid result only from the first column
}
}
}
}
}
}
If I put an array to the mysql_stmt_fetch_column then I'm fetching the only first field valid, all other fields are garbage. If I put a single MYSQL_BIND structure to this function, then I'm getting an abortion of the app on approximately 74th field (funny thing that it's always this field). If I use another array of MYSQL_BIND then the situation is the same as the first case.
Please help me to understand how to use it correctly! Thanks
Minimal reproducible example

Why does this if/else statement appear to be optimized away?

This is the code in question:
void DeckTug::StickCallback(unsigned long long evtID, DWORD value)
{
long int val = value;
if (evtID == stickXInputID || evtID == stickAxisXInputID)
stickXpct = (((double)val)) / 325.94;
else if (evtID == stickYInputID || evtID == stickAxisYInputID) {
stickYpct = (((double)val)) / 325.94;
if(isAuto)
if ((stickYpct < 0.0)) {
acPullingTug = true;
tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
}
else {
acPullingTug = false;
tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
}
}
}
When I compile a debug build, this runs perfectly. When I compile a release build, it does not work. When I attach the visual studio debugger to the release version, I can break on the first if statement and on the closing brace of the function, but I cannot hit a break point anywhere else, and neither stickXpct or stickYpct are ever being assigned anything, although in the debugger I can see that "value" has a valid value, and "evtID" DOES equal one of inputIDs.
In conclusion, it looks to me like, in the release version of the code only, both the first "if" statement and the first "else if" statement only evaluate to false, even when one of them should evaluate to true. Does anyone know what is going on here? because I don't.
Thanks so much,
Farley
Edit: changed answer in response to comments
Try adding volatility
void DeckTug::StickCallback(unsigned long long evtID, DWORD value)
{
long int val = value;
volatile unsigned long long _evtID = evtID;
if (_evtID == stickXInputID || _evtID == stickAxisXInputID)
stickXpct = (((double)val)) / 325.94;
else if (_evtID == stickYInputID || _evtID == stickAxisYInputID) {
stickYpct = (((double)val)) / 325.94;
if(isAuto)
if ((stickYpct < 0.0)) {
acPullingTug = true;
tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
}
else {
acPullingTug = false;
tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
}
}
}
That should prevent the compiler from optimizing those branches until you can track down why it wants to optimize those branches away.

While loop - how to remove code duplication

It's not the first time I find myself in the following situation:
bool a = some_very_long_computation;
bool b = another_very_long_computation;
while (a && b) {
...
a = some_very_long_computation;
b = another_very_long_computation;
}
I don't want to compute everything in while condition, since computations are long and I want to give them appropriate names.
I don't want to create helper functions, because computation uses many local variables, and passing them all will make the code much less readable (and it will be some_huge_call).
It's unknown whether loop body will be executed at least once.
What is a good pattern in such situation? Currently I face it in C++, but I've encountered this in other languages as well. I can solve it by using additional variable isFirstPass, but it looks ugly (and, I guess, will cause some warnings):
bool a, b;
bool isFirstPass = true;
do {
if (!isFirstPass) {
...
} else {
isFirstPass = false;
}
a = some_very_long_computation;
b = another_very_long_computation;
} while (a && b);
The direct simplification of your code is:
while (
some_very_long_computation &&
another_very_long_computation
) {
...
}
If you want to keep the variables a and b:
bool a, b;
while (
(a = some_very_long_computation) &&
(b = another_very_long_computation)
) {
...
}
If you don't want to put the conditions into the while condition:
while (true) {
bool a = some_very_long_computation;
bool b = another_very_long_computation;
if (!(a && b)) {
break;
}
...
}
You could also create helper lambdas (which have access to local variables):
auto fa = [&]() { return some_very_long_computation; };
auto fb = [&]() { return another_very_long_computation; };
while (fa() && fb()) {
...
}