<commit_before>/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2012-2013 Canonical Ltd.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
General Public License for more details.

You should have received a copy of the GNU Affero General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

describe('test_model.js', function() {
  describe('Charm initialization', function() {
    var models;

    before(function(done) {
      YUI(GlobalConfig).use('juju-models', 'juju-charm-models', function(Y) {
        models = Y.namespace('juju.models');
        done();
      });
    });

    it('must be able to create Charm', function() {
      var charm = new models.Charm(
          {id: 'cs:~alt-bac/precise/openstack-dashboard-0'});
      charm.get('scheme').should.equal('cs');
      charm.get('owner').should.equal('alt-bac');
      charm.get('series').should.equal('precise');
      charm.get('package_name').should.equal('openstack-dashboard');
      charm.get('revision').should.equal(0);
      charm.get('full_name').should.equal(
          '~alt-bac/precise/openstack-dashboard');
      charm.get('charm_path').should.equal(
          '~alt-bac/precise/openstack-dashboard-0/json');
    });

    it('must not set "owner" for promulgated charms', function() {
      var charm = new models.Charm({
        id: 'cs:precise/openstack-dashboard-0'
      });
      assert.isUndefined(charm.get('owner'));
    });

    it('must accept charm ids without versions.', function() {
      var charm = new models.Charm(
          {id: 'cs:~alt-bac/precise/openstack-dashboard'});
      assert.isUndefined(charm.get('revision'));
      assert.equal(charm.get('charm_path'),
          '~alt-bac/precise/openstack-dashboard/json');
    });

    it('must be able to parse hyphenated owner names', function() {
      // Note that an earlier version of the parsing code did not handle
      // hyphens in user names, so this test intentionally includes one.
      var charm = new models.Charm(
          {id: 'cs:~marco-ceppi/precise/wordpress-17'});
      charm.get('full_name').should.equal('~marco-ceppi/precise/wordpress');
    });

    it('must reject bad charm ids.', function() {
      try {
        /* jshint -W031 */
        new models.Charm({id: 'foobar'});
        /* jshint +W031 */
        assert.fail('Should have thrown an error');
      } catch (e) {
        e.should.equal(
            'Developers must initialize charms with a well-formed id.');
      }
    });


    it('must reject missing charm ids at initialization.', function() {
      try {
        /* jshint -W031 */
        new models.Charm();
        /* jshint +W031 */
        assert.fail('Should have thrown an error');
      } catch (e) {
        e.should.equal(
            'Developers must initialize charms with a well-formed id.');
      }
    });

    it('must convert timestamps into time objects on Charm', function() {
      var time = 1349797266.032,
          date = new Date(time),
          charm = new models.Charm(
          { id: 'cs:precise/foo-9', last_change: {created: time / 1000} });
      charm.get('last_change').created.should.eql(date);
    });
  });

  describe('juju models', function() {
    var models, yui;

    before(function(done) {
      YUI(GlobalConfig).use('juju-models', 'juju-charm-models', function(Y) {
        models = Y.namespace('juju.models');
        yui = Y;
        done();
      });
    });

    beforeEach(function() {
      window._gaq = [];
    });

    it('should be able to aggregate unit by status', function() {
      var sl = new models.ServiceList();
      var mysql = new models.Service({id: 'mysql'});
      var wordpress = new models.Service({id: 'wordpress'});
      sl.add([mysql, wordpress]);
      var my0 = new models.ServiceUnit({
        id: 'mysql/0',
        agent_state: 'pending'});
      var my1 = new models.ServiceUnit({
        id: 'mysql/1',
        agent_state: 'pending'});

      mysql.get('units').add([my0, my1]);

      var wp0 = new models.ServiceUnit({
        id: 'wordpress/0',
        agent_state: 'pending'});
      var wp1 = new models.ServiceUnit({
        id: 'wordpress/2',
        agent_state: 'error',
        agent_state_info: 'hook failed: "db-relation-changed"',
        agent_state_data: {
          hook: 'db-relation-changed',
          'relation-id': 1,
          'remote-unit': 'mysql/0'
        }});
      var wp2 = new models.ServiceUnit({
        id: 'wordpress/1',
        agent_state: 'error',
        agent_state_info: 'hook failed: "install"'});
      wordpress.get('units').add([wp0, wp1, wp2]);

      assert.deepEqual(mysql.get('units')
                       .get_informative_states_for_service(mysql),
          [{'pending': 2}, {}]);
      assert.deepEqual(wordpress.get('units')<commit_msg>added comment to clarify details in test<commit_after>/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2012-2013 Canonical Ltd.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
General Public License for more details.

You should have received a copy of the GNU Affero General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

describe('test_model.js', function() {
  describe('Charm initialization', function() {
    var models;

    before(function(done) {
      YUI(GlobalConfig).use('juju-models', 'juju-charm-models', function(Y) {
        models = Y.namespace('juju.models');
        done();
      });
    });

    it('must be able to create Charm', function() {
      var charm = new models.Charm(
          {id: 'cs:~alt-bac/precise/openstack-dashboard-0'});
      charm.get('scheme').should.equal('cs');
      charm.get('owner').should.equal('alt-bac');
      charm.get('series').should.equal('precise');
      charm.get('package_name').should.equal('openstack-dashboard');
      charm.get('revision').should.equal(0);
      charm.get('full_name').should.equal(
          '~alt-bac/precise/openstack-dashboard');
      charm.get('charm_path').should.equal(
          '~alt-bac/precise/openstack-dashboard-0/json');
    });

    it('must not set "owner" for promulgated charms', function() {
      var charm = new models.Charm({
        id: 'cs:precise/openstack-dashboard-0'
      });
      assert.isUndefined(charm.get('owner'));
    });

    it('must accept charm ids without versions.', function() {
      var charm = new models.Charm(
          {id: 'cs:~alt-bac/precise/openstack-dashboard'});
      assert.isUndefined(charm.get('revision'));
      assert.equal(charm.get('charm_path'),
          '~alt-bac/precise/openstack-dashboard/json');
    });

    it('must be able to parse hyphenated owner names', function() {
      // Note that an earlier version of the parsing code did not handle
      // hyphens in user names, so this test intentionally includes one.
      var charm = new models.Charm(
          {id: 'cs:~marco-ceppi/precise/wordpress-17'});
      charm.get('full_name').should.equal('~marco-ceppi/precise/wordpress');
    });

    it('must reject bad charm ids.', function() {
      try {
        /* jshint -W031 */
        new models.Charm({id: 'foobar'});
        /* jshint +W031 */
        assert.fail('Should have thrown an error');
      } catch (e) {
        e.should.equal(
            'Developers must initialize charms with a well-formed id.');
      }
    });


    it('must reject missing charm ids at initialization.', function() {
      try {
        /* jshint -W031 */
        new models.Charm();
        /* jshint +W031 */
        assert.fail('Should have thrown an error');
      } catch (e) {
        e.should.equal(
            'Developers must initialize charms with a well-formed id.');
      }
    });

    it('must convert timestamps into time objects on Charm', function() {
      var time = 1349797266.032,
          date = new Date(time),
          charm = new models.Charm(
          { id: 'cs:precise/foo-9', last_change: {created: time / 1000} });
      charm.get('last_change').created.should.eql(date);
    });
  });

  describe('juju models', function() {
    var models, yui;

    before(function(done) {
      YUI(GlobalConfig).use('juju-models', 'juju-charm-models', function(Y) {
        models = Y.namespace('juju.models');
        yui = Y;
        done();
      });
    });

    beforeEach(function() {
      window._gaq = [];
    });

    it('should be able to aggregate unit by status', function() {
      var sl = new models.ServiceList();
      var mysql = new models.Service({id: 'mysql'});
      var wordpress = new models.Service({id: 'wordpress'});
      sl.add([mysql, wordpress]);
      var my0 = new models.ServiceUnit({
        id: 'mysql/0',
        agent_state: 'pending'});
      var my1 = new models.ServiceUnit({
        id: 'mysql/1',
        agent_state: 'pending'});

      mysql.get('units').add([my0, my1]);

      var wp0 = new models.ServiceUnit({
        id: 'wordpress/0',
        agent_state: 'pending'});
      // The order of these errored units is important because there was an old
      // long standing bug where if the first unit was in relation error it
      // would drop the error status silently.
      var wp1 = new models.ServiceUnit({
        id: 'wordpress/2',
        agent_state: 'error',
        agent_state_info: 'hook failed: "db-relation-changed"',
        agent_state_data: {
          hook: 'db-relation-changed',
          'relation-id': 1,
          'remote-unit': 'mysql/0'
        }});
      var wp2 = new models.ServiceUnit({
        id: 'wordpress/1',
        agent_state: 'error',
        agent_state_info: 'hook failed: "install"'});
      wordpress.get('units').add([wp0, wp1, wp2]);

      assert.deepEqual(mysql.get('units')
                       .get_informative_states_for_service(mysql),
          [{'pending': 2}, {}]);
      assert.deepEqual(wordpress.get('units')
{'fraction_non_alphanumeric': 0.14323925475004612, 'fraction_numerical': 0.01162147205312673, 'mean_word_length': 2.603522765038219, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4075640', 'n_tokens_mistral': 3384, 'n_tokens_neox': 3253, 'n_words': 877}