<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package migration

import (
	"time"

	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/juju/testing"
	"github.com/juju/juju/version"
)

type ModelSerializationSuite struct {
	testing.BaseSuite
}

var _ = gc.Suite(&ModelSerializationSuite{})

func (*ModelSerializationSuite) TestNil(c *gc.C) {
	_, err := importModel(nil)
	c.Check(err, gc.ErrorMatches, "version: expected int, got nothing")
}

func (*ModelSerializationSuite) TestMissingVersion(c *gc.C) {
	_, err := importModel(map[string]interface{}{})
	c.Check(err, gc.ErrorMatches, "version: expected int, got nothing")
}

func (*ModelSerializationSuite) TestNonIntVersion(c *gc.C) {
	_, err := importModel(map[string]interface{}{
		"version": "hello",
	})
	c.Check(err.Error(), gc.Equals, `version: expected int, got string("hello")`)
}

func (*ModelSerializationSuite) TestUnknownVersion(c *gc.C) {
	_, err := importModel(map[string]interface{}{
		"version": 42,
	})
	c.Check(err.Error(), gc.Equals, `version 42 not valid`)
}

func (*ModelSerializationSuite) TestParsing(c *gc.C) {
	latestTools := version.MustParse("2.0.1")
	configMap := map[string]interface{}{
		"name": "awesome",
		"uuid": "some-uuid",
	}
	model, err := importModel(map[string]interface{}{
		"version":      1,
		"owner":        "magic",
		"config":       configMap,
		"latest-tools": latestTools.String(),
		"users": map[string]interface{}{
			"version": 1,
			"users": []interface{}{
				map[string]interface{}{
					"name":         "admin@local",
					"created-by":   "admin@local",
					"date-created": time.Date(2015, 10, 9, 12, 34, 56, 0, time.UTC),
				},
			},
		},
		"machines": map[string]interface{}{
			"version": 1,
			"machines": []interface{}{
				map[string]interface{}{
					"id":         "0",
					"instance":   minimalCloudInstanceMap(),
					"containers": []interface{}{},
				},
			},
		},
	})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(model.Owner_, gc.Equals, "magic")
	c.Assert(model.Tag().Id(), gc.Equals, "some-uuid")
	c.Assert(model.Config_, jc.DeepEquals, configMap)
	c.Assert(model.LatestToolsVersion(), gc.Equals, latestTools)
	c.Assert(model.Users_.Users_, gc.HasLen, 1)
	c.Assert(model.Users_.Users_[0].Name_, gc.Equals, "admin@local")
	c.Assert(model.Machines_.Machines_, gc.HasLen, 1)
	c.Assert(model.Machines_.Machines_[0].Id_, gc.Equals, "0")
}

func (*ModelSerializationSuite) TestParsingOptionals(c *gc.C) {
	configMap := map[string]interface{}{
		"name": "awesome",
		"uuid": "some-uuid",
	}
	model, err := importModel(map[string]interface{}{
		"version": 1,
		"owner":   "magic",
		"config":  configMap,
		"users": map[string]interface{}{
			"version": 1,
			"users":   []interface{}{},
		},
		"machines": map[string]interface{}{
			"version":  1,
			"machines": []interface{}{},<commit_msg>tests pass - w00t<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package migration

import (
	"time"

	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/juju/testing"
	"github.com/juju/juju/version"
)

type ModelSerializationSuite struct {
	testing.BaseSuite
}

var _ = gc.Suite(&ModelSerializationSuite{})

func (*ModelSerializationSuite) TestNil(c *gc.C) {
	_, err := importModel(nil)
	c.Check(err, gc.ErrorMatches, "version: expected int, got nothing")
}

func (*ModelSerializationSuite) TestMissingVersion(c *gc.C) {
	_, err := importModel(map[string]interface{}{})
	c.Check(err, gc.ErrorMatches, "version: expected int, got nothing")
}

func (*ModelSerializationSuite) TestNonIntVersion(c *gc.C) {
	_, err := importModel(map[string]interface{}{
		"version": "hello",
	})
	c.Check(err.Error(), gc.Equals, `version: expected int, got string("hello")`)
}

func (*ModelSerializationSuite) TestUnknownVersion(c *gc.C) {
	_, err := importModel(map[string]interface{}{
		"version": 42,
	})
	c.Check(err.Error(), gc.Equals, `version 42 not valid`)
}

func (*ModelSerializationSuite) TestParsing(c *gc.C) {
	latestTools := version.MustParse("2.0.1")
	configMap := map[string]interface{}{
		"name": "awesome",
		"uuid": "some-uuid",
	}
	model, err := importModel(map[string]interface{}{
		"version":      1,
		"owner":        "magic",
		"config":       configMap,
		"latest-tools": latestTools.String(),
		"users": map[string]interface{}{
			"version": 1,
			"users": []interface{}{
				map[string]interface{}{
					"name":         "admin@local",
					"created-by":   "admin@local",
					"date-created": time.Date(2015, 10, 9, 12, 34, 56, 0, time.UTC),
				},
			},
		},
		"machines": map[string]interface{}{
			"version": 1,
			"machines": []interface{}{
				minimalMachineMap("0"),
			},
		},
	})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(model.Owner_, gc.Equals, "magic")
	c.Assert(model.Tag().Id(), gc.Equals, "some-uuid")
	c.Assert(model.Config_, jc.DeepEquals, configMap)
	c.Assert(model.LatestToolsVersion(), gc.Equals, latestTools)
	c.Assert(model.Users_.Users_, gc.HasLen, 1)
	c.Assert(model.Users_.Users_[0].Name_, gc.Equals, "admin@local")
	c.Assert(model.Machines_.Machines_, gc.HasLen, 1)
	c.Assert(model.Machines_.Machines_[0].Id_, gc.Equals, "0")
}

func (*ModelSerializationSuite) TestParsingOptionals(c *gc.C) {
	configMap := map[string]interface{}{
		"name": "awesome",
		"uuid": "some-uuid",
	}
	model, err := importModel(map[string]interface{}{
		"version": 1,
		"owner":   "magic",
		"config":  configMap,
		"users": map[string]interface{}{
			"version": 1,
			"users":   []interface{}{},
		},
		"machines": map[string]interface{}{
			"version":  1,
			"machines": []interface{}{},
{'fraction_non_alphanumeric': 0.1902834008097166, 'fraction_numerical': 0.014082027812004929, 'mean_word_length': 4.532619279454723, 'pattern_counts': {'":': 59, '<': 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': '5299113', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2056, 'n_words': 391}