<commit_before>jest.dontMock('../ServiceUtil');
jest.dontMock('../../structs/Service');
jest.dontMock('../../constants/VolumeConstants');

var Service = require('../../structs/Service');
var ServiceUtil = require('../ServiceUtil');

describe('ServiceUtil', function () {
  describe('#createServiceFromFormModel', function () {
    it('should convert to the correct Service', function () {
      let model = {
        general: {
          id: '/test',
          cmd: 'sleep 1000;'
        }
      };

      let expectedService = new Service({
        id: '/test',
        cmd: 'sleep 1000;'
      });

      expect(ServiceUtil.createServiceFromFormModel(model))
        .toEqual(expectedService);
    });

    it('should return empty service if null is provided', function () {
      let model = null;

      let expectedService = new Service({});

      expect(ServiceUtil.createServiceFromFormModel(model))
        .toEqual(expectedService);
    });

    it('should return empty service if empty object is provided', function () {
      let model = {};

      let expectedService = new Service({});

      expect(ServiceUtil.createServiceFromFormModel(model))
        .toEqual(expectedService);
    });

    describe('networking', function () {

      describe('host mode', function () {

        it('should not add a portDefinitions field if no ports were passed in', function () {
          let service = ServiceUtil.createServiceFromFormModel({
            networking: {
              networkType: 'host',
              ports: []
            }
          });
          expect(service.portDefinitions).not.toBeDefined();
        });

        it('should convert the supplied network fields', function () {
          let service = ServiceUtil.createServiceFromFormModel({
            networking: {
              networkType: 'host',
              ports: [{protocol: 'udp', name: 'foo'}]
            }
          });
          expect(service.portDefinitions).toEqual([
            {port: 0, protocol: 'udp', name: 'foo'}
          ]);
        });
<commit_msg>Adding test cases for undefined and null<commit_after>jest.dontMock('../ServiceUtil');
jest.dontMock('../../structs/Service');
jest.dontMock('../../constants/VolumeConstants');

var Service = require('../../structs/Service');
var ServiceUtil = require('../ServiceUtil');

describe('ServiceUtil', function () {
  describe('#createServiceFromFormModel', function () {
    it('should convert to the correct Service', function () {
      let model = {
        general: {
          id: '/test',
          cmd: 'sleep 1000;'
        }
      };

      let expectedService = new Service({
        id: '/test',
        cmd: 'sleep 1000;'
      });

      expect(ServiceUtil.createServiceFromFormModel(model))
        .toEqual(expectedService);
    });

    it('should return empty service if null is provided', function () {
      let model = null;

      let expectedService = new Service({});

      expect(ServiceUtil.createServiceFromFormModel(model))
        .toEqual(expectedService);
    });

    it('should return empty service if empty object is provided', function () {
      let model = {};

      let expectedService = new Service({});

      expect(ServiceUtil.createServiceFromFormModel(model))
        .toEqual(expectedService);
    });

    describe('environmentVariables', function () {

      it('should keep undefined values as ""', function() {
        let service = ServiceUtil.createServiceFromFormModel({
          environmentVariables: {
            environmentVariables: [
              { key: 'a', value: 'correct' },
              { key: 'b', value: undefined }
            ]
          }
        });
        expect(service.env).toEqual({
          a: 'correct',
          b: ''
        });
      });

      it('should keep null values as ""', function() {
        let service = ServiceUtil.createServiceFromFormModel({
          environmentVariables: {
            environmentVariables: [
              { key: 'a', value: 'correct' },
              { key: 'b', value: null }
            ]
          }
        });
        expect(service.env).toEqual({
          a: 'correct',
          b: ''
        });
      });

    });

    describe('networking', function () {

      describe('host mode', function () {

        it('should not add a portDefinitions field if no ports were passed in', function () {
          let service = ServiceUtil.createServiceFromFormModel({
            networking: {
              networkType: 'host',
              ports: []
            }
          });
          expect(service.portDefinitions).not.toBeDefined();
        });

        it('should convert the supplied network fields', function () {
          let service = ServiceUtil.createServiceFromFormModel({
            networking: {
              networkType: 'host',
              ports: [{protocol: 'udp', name: 'foo'}]
            }
          });
          expect(service.portDefinitions).toEqual([
            {port: 0, protocol: 'udp', name: 'foo'}
          ]);
        });

{'fraction_non_alphanumeric': 0.13854704247717348, 'fraction_numerical': 0.003572846367606193, 'mean_word_length': 2.083843329253366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '922033', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1255, 'n_words': 306}