Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid line coverage when testing GraphQL services #6418

Closed
MaryamZi opened this issue Apr 25, 2024 · 1 comment · Fixed by ballerina-platform/module-ballerina-graphql#1841
Assignees
Labels
module/graphql Issues related to Ballerina GraphQL module Priority/High Reason/Complex Issue occurred due to complex scenario. Type/Bug
Milestone

Comments

@MaryamZi
Copy link
Member

Description:
$title.

Summarizing findings by/discussions with @gayaldassanayake, @azinneera, @SasinduDilshara, and @lochana-chathura:

  • This is due to the code modifier modifying the @graphql:ServiceConfig annotation, if present, in a way that changes line numbers
  • The new annotation is created with the old fields, but without the old separators (to which new line/whitespace is attached), causing a difference in line numbers.
    1. We need to use the separators from the original list
    2. Need to incorporate whitespace after curly braces similarly
    3. New data added should not add new lines (should be on an existing line, can add at the end)

Please add if I've missed anything.

Steps to reproduce:

Source

import ballerina/graphql;

type Person record {|
    readonly int id;
    string firstName;
    string lastName;
    int age;
|};

table<Person> key(id) data = table [
    {id: 101, firstName: "John", lastName: "Doe", age: 20},
    {id: 102, firstName: "Mary", lastName: "Anne", age: 30}
];

@graphql:ServiceConfig {
    graphiql: {
        enabled: true
    }
}
service /graphql on new graphql:Listener(9090) {
    resource function get profiles() returns Profile[] {
        return from int id in data.keys() select new (id);
    }
    resource function get profile(@graphql:ID int id) returns Profile {
        return new (id);
    }
}

service class Profile {
    private final int id;
    private final string firstName;
    private final string lastName;
    private final int age;

    function init(int id) {
        Person {firstName, lastName, age} = data.get(id);
        self.id = id;
        self.firstName = firstName;
        self.lastName = lastName;
        self.age = age;
    }

    resource function get id() returns @graphql:ID int {
        return self.id;
    }

    resource function get fullName() returns string {
        return self.firstName + " " + self.lastName;
    }

    resource function get firstName() returns string {
        return self.firstName;
    }

    resource function get lastName() returns string {
        return self.lastName;
    }

    resource function get age() returns int {
        return self.age;
    }

    resource function get isAdult() returns boolean {
        return self.age > 21;
    }
}

Tests

import ballerina/graphql;
import ballerina/test;

final graphql:Client cl = check new ("http://localhost:9090/graphql");

@test:Config
function testRetrievingProfile() returns error? {
    json payload = check cl->execute(string `query QueryProfile($id: ID!) {
        profile(id: $id) {
            fullName
            age
        }
    }`, {id: 101});
    test:assertEquals(payload, {
        "data": {
            "profile": {
                "fullName": "John Doe",
                "age": 20
            }
        }
    });
}

Screenshot from 2024-04-25 17-42-48

Affected Versions:
2201.9.0-rc1

Copy link

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/graphql Issues related to Ballerina GraphQL module Priority/High Reason/Complex Issue occurred due to complex scenario. Type/Bug
Projects
Archived in project
2 participants