[HELP] Unit tests

Sorry in advance if my questions are basic (for professional devs)

@cicoub13 gave me a hand :pray: on the PR concerned. but being a noob, I can’t figure it out

I need to test this file:
https://github.com/GladysAssistant/Gladys/pull/1045/files#diff-1e5ef582cb3ebc70e2b396ec31c6116178f1a41b6434f178ae83f0fdd0350ca0

It’s partly done.

I still need to test the case where no values (humidity) are available for a given room.

How should I do this?

I tried adding a room + a device + device feature (without values) in the seed, but it messes up the other tests (room/device, etc.). So I think this isn’t the right method.

If I use the only room available in the tests (kitchen), the test fails because a value is returned (which is normal).

I prefer to ask even if it’s really basic because I’m stuck.

You can create a room just in your test simply :slight_smile: When it’s just for a single test, it’s better to avoid creating global seeds

Ok, I’ve done this: probably messy but well

it('should ask the humidity in a room with no values', async () => {
    const brain = {
      addRoom: fake.returns(null),
      removeRoom: fake.returns(null),
    };
    const stateManager = new StateManager(event);
    const deviceManager = new Device(event, messageManager, stateManager, {});
    const serviceManager = new ServiceManager({}, stateManager);
    const room = new Room(brain);
    await room.create('test-house', {
      name: 'No value Room',
    });
    const roomFound = await room.getBySelector('no-value-room');
    const device = new Device(event, {}, stateManager, serviceManager);
    await device.create({
      id: '7bbe2ed8-e7c7-4b89-8421-b52fea5491ef',
      name: 'HUMIDITY DEVICE',
      selector: 'humidity-device',
      external_id: 'humidity-device-external',
      service_id: 'a810b8db-6d04-4697-bed3-c4b72c996279',
      room_id: roomFound.id,
      created_at: '2021-02-12 07:49:07.556 +00:00',
      updated_at: '2021-02-12 07:49:07.556 +00:00',
      features: [
        {
          id: '984d23b0-3508-4d9e-ba91-63cfc3f9e536',
          name: 'Test humidity sensor null',
          selector: 'test-humidity-sensor-null',
          external_id: 'humidity-sensor:2',
          category: 'humidity-sensor',
          type: 'humidity',
          unit: 'percent',
          read_only: false,
          has_feedback: false,
          min: 0,
          max: 100,
          last_value: null,
          last_value_changed: new Date().toISOString(),
          device_id: '7bbe2ed8-e7c7-4b89-8421-b52fea5491ef',
        },
      ],
    });
    const message = {};
    await deviceManager.humiditySensorManager.command(
      message,
      {
        intent: 'humidity-sensor.get-in-room',
        entities: [
          {
            sourceText: 'No value Room',
            entity: 'room',
          },
        ],
      },
      {
        room: roomFound.id,
      },
    );
    assert.calledWith(messageManager.replyByIntent, message, 'humidity-sensor.get-in-room.fail.no-results', {
      room: roomFound.id,
      roomName: 'No value Room',
      humidity: null,
      unit: 'percent',
    });
  });

I think I’m close

The log:

2021-03-12T14:00:11+0100 <debug> device.notify.js:20 (DeviceManager.notify) Notify device humidity-device creation
2021-03-12T14:00:11+0100 <warn> device.notify.js:33 (DeviceManager.notify) Service a810b8db-6d04-4697-bed3-c4b72c996279 was not found.
2021-03-12T14:00:11+0100 <debug> humidity-sensor.getHumidityInRoom.js:21 (HumiditySensorManager.getHumidityInRoom) Getting average humidity in room de14c6bf-d739-479f-a3f2-85ab783b68ed
2021-03-12T14:00:11+0100 <debug> humidity-sensor.command.js:37 (HumiditySensorManager.command) NoValuesFoundError: No humidity values found in this room.
    at HumiditySensorManager.command (/home/vonox/repos/GladysFork/server/lib/device/humidity-sensor/humidity-sensor.command.js:26:17)
    at async Context.<anonymous> (/home/vonox/repos/GladysFork/server/test/lib/device/humidity-sensor/humidity-sensor.test.js:113:5)
    1) should ask the humidity in a room with no values

The result is there => NoValuesFoundError: No humidity values found in this room.

But

Again this context issue I thought I understood, how to read this output?

EDIT: in green what is expected and in red what was produced/received?

It’s not dirty! On the contrary, I think it’s better to create your own little scenario in your test, I like that :slight_smile:

Exactly, that’s it.

Okay, I was afraid I had understood :D, so I don’t understand what the kitchen has to do with it ^^

lol who would have thought, dev looks like a real job?? :stuck_out_tongue:

Do you want to pair up?

We’re not going that far :rofl:

Not necessarily (you’ve got other fish to fry).

What I really don’t want is to have a turnkey solution without understanding.

After several attempts, I still don’t understand why the code is returning information from another Room.

I’ve added logging everywhere to test the test ^^!

By the way, it’s really strange because the red output is from the first test.

I’m taking your branch locally to test, I tell you.

@VonOx

I recommend adding the ability to run the test file in debug mode in VSCode and to be able to add breakpoints to see what happens throughout the process.

You add in the file

    {
      "type": "node",
      "request": "launch",
      "name": "NodeJS test current file",
      "runtimeArgs": [
        "${workspaceFolder}/server/node_modules/mocha/bin/mocha",
        "--timeout",
        "999999",
        "--colors",
        "${workspaceFolder}/server/test/bootstrap.test.js",
        "${file}",
        "--exit"
      ],
      "console": "integratedTerminal",
      "env": {
        "NODE_ENV": "test",
        "SQLITE_FILE_PATH": "/tmp/gladys-test.db"
      }
    },

Then, the little green arrow to run the open file in debug mode, having the breakpoints… cough cough

I didn’t push the test snippet I put above (thanks :slight_smile: )

Yes, I’ve retrieved it, I have the error.

Just before continuing, when you use the « fake Â» of sinon,
you need to remember to reset sinon with

  afterEach(() => {
    sinon.reset();
  });

In the command function you created, you raise a NoValuesFoundError if your device has no value (test case above).

But the context is « filled Â» after the error.

The context is therefore

    {
      room: roomFound.id,
    }

without anything else around it.

Your test is not bad, but there is a gap between what you do in command and the result you expect.

On line 37, add the « context Â» to your debug, you’ll see :wink:

Got it, thanks Alex, I removed the device creation, it was useless :slight_smile: