<commit_before>package db_test

import (
	"encoding/json"
	"time"

	"github.com/concourse/concourse/atc"
	"github.com/concourse/concourse/atc/db"
	"github.com/concourse/concourse/atc/db/dbtest"
	"github.com/concourse/concourse/atc/db/lock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Resource Config Scope", func() {
	var scenario *dbtest.Scenario
	var resourceScope db.ResourceConfigScope
	var pipelineConfig atc.Config = atc.Config{
		Resources: atc.ResourceConfigs{
			{
				Name: "some-resource",
				Type: "some-base-resource-type",
				Source: atc.Source{
					"some": "source",
				},
			},
		},
		Jobs: atc.JobConfigs{
			{
				Name: "some-job",
				PlanSequence: []atc.Step{
					{
						Config: &atc.GetStep{
							Name: "some-resource",
						},
					},
				},
			},
			{
				Name: "downstream-job",
				PlanSequence: []atc.Step{
					{
						Config: &atc.GetStep{
							Name:   "some-resource",
							Passed: []string{"some-job"},
						},
					},
				},
			},
			{
				Name: "some-other-job",
			},
		},
	}

	BeforeEach(func() {
		scenario = dbtest.Setup(
			builder.WithPipeline(pipelineConfig),
			builder.WithResourceVersions("some-resource"),
		)

		rc, found, err := resourceConfigFactory.FindResourceConfigByID(scenario.Resource("some-resource").ResourceConfigID())
		Expect(err).ToNot(HaveOccurred())
		Expect(found).To(BeTrue())

		resourceScope, err = rc.FindOrCreateScope(intptr(scenario.Resource("some-resource").ID()))
		Expect(err).ToNot(HaveOccurred())
	})

	Describe("SaveVersions", func() {
		var (
			originalVersionSlice []atc.Version
		)

		BeforeEach(func() {
			originalVersionSlice = []atc.Version{
				{"ref": "v1"},
				{"ref": "v3"},
			}
		})

		// XXX: Can make test more resilient if there is a method that gives all versions by descending check order
		It("ensures versioned resources have the correct check_order", func() {
			err := resourceScope.SaveVersions(nil, originalVersionSlice)
			Expect(err).ToNot(HaveOccurred())

			latestVR, found, err := resourceScope.LatestVersion()
			Expect(err).ToNot(HaveOccurred())
			Expect(found).To(BeTrue())

			Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
			Expect(latestVR.CheckOrder()).To(Equal(2))

			pretendCheckResults := []atc.Version{
				{"ref": "v2"},
				{"ref": "v3"},
			}

			err = resourceScope.SaveVersions(nil, pretendCheckResults)
			Expect(err).ToNot(HaveOccurred())

			latestVR, found, err = resourceScope.LatestVersion()
			Expect(err).ToNot(HaveOccurred())
			Expect(found).To(BeTrue())

			Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
			Expect(latestVR.CheckOrder()).To(Equal(4))
		})

		Context("when the versions already exists", func() {
			var newVersionSlice []atc.Version

			BeforeEach(func() {
				newVersionSlice = []atc.Version{
					{"ref": "v1"},
					{"ref": "v3"},
				}

				err := resourceScope.SaveVersions(nil, originalVersionSlice)
				Expect(err).ToNot(HaveOccurred())

				latestVR, found, err := resourceScope.LatestVersion()
				Expect(err).ToNot(HaveOccurred())
				Expect(found).To(BeTrue())

				Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
				Expect(latestVR.CheckOrder()).To(Equal(2))
			})

			It("does not change the check order", func() {
				err := resourceScope.SaveVersions(nil, newVersionSlice)
				Expect(err).ToNot(HaveOccurred())

				latestVR, found, err := resourceScope.LatestVersion()
				Expect(err).ToNot(HaveOccurred())
				Expect(found).To(BeTrue())

				Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
				Expect(latestVR.CheckOrder()).To(Equal(2))
			})

			Context("when a new version is added for non-trigger resource", func() {
				It("requests schedule on the jobs that use the resource", func() {
					err := resourceScope.SaveVersions(nil, originalVersionSlice)
					Expect(err).ToNot(HaveOccurred())

					requestedSchedule := scenario.Job("some-job").ScheduleRequestedTime()

					newVersions := []atc.Version{
						{"ref": "v0"},
						{"ref": "v3"},<commit_msg>Update test case description in atc/db/resource_config_scope_test.go.<commit_after>package db_test

import (
	"encoding/json"
	"time"

	"github.com/concourse/concourse/atc"
	"github.com/concourse/concourse/atc/db"
	"github.com/concourse/concourse/atc/db/dbtest"
	"github.com/concourse/concourse/atc/db/lock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Resource Config Scope", func() {
	var scenario *dbtest.Scenario
	var resourceScope db.ResourceConfigScope
	var pipelineConfig atc.Config = atc.Config{
		Resources: atc.ResourceConfigs{
			{
				Name: "some-resource",
				Type: "some-base-resource-type",
				Source: atc.Source{
					"some": "source",
				},
			},
		},
		Jobs: atc.JobConfigs{
			{
				Name: "some-job",
				PlanSequence: []atc.Step{
					{
						Config: &atc.GetStep{
							Name: "some-resource",
						},
					},
				},
			},
			{
				Name: "downstream-job",
				PlanSequence: []atc.Step{
					{
						Config: &atc.GetStep{
							Name:   "some-resource",
							Passed: []string{"some-job"},
						},
					},
				},
			},
			{
				Name: "some-other-job",
			},
		},
	}

	BeforeEach(func() {
		scenario = dbtest.Setup(
			builder.WithPipeline(pipelineConfig),
			builder.WithResourceVersions("some-resource"),
		)

		rc, found, err := resourceConfigFactory.FindResourceConfigByID(scenario.Resource("some-resource").ResourceConfigID())
		Expect(err).ToNot(HaveOccurred())
		Expect(found).To(BeTrue())

		resourceScope, err = rc.FindOrCreateScope(intptr(scenario.Resource("some-resource").ID()))
		Expect(err).ToNot(HaveOccurred())
	})

	Describe("SaveVersions", func() {
		var (
			originalVersionSlice []atc.Version
		)

		BeforeEach(func() {
			originalVersionSlice = []atc.Version{
				{"ref": "v1"},
				{"ref": "v3"},
			}
		})

		// XXX: Can make test more resilient if there is a method that gives all versions by descending check order
		It("ensures versioned resources have the correct check_order", func() {
			err := resourceScope.SaveVersions(nil, originalVersionSlice)
			Expect(err).ToNot(HaveOccurred())

			latestVR, found, err := resourceScope.LatestVersion()
			Expect(err).ToNot(HaveOccurred())
			Expect(found).To(BeTrue())

			Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
			Expect(latestVR.CheckOrder()).To(Equal(2))

			pretendCheckResults := []atc.Version{
				{"ref": "v2"},
				{"ref": "v3"},
			}

			err = resourceScope.SaveVersions(nil, pretendCheckResults)
			Expect(err).ToNot(HaveOccurred())

			latestVR, found, err = resourceScope.LatestVersion()
			Expect(err).ToNot(HaveOccurred())
			Expect(found).To(BeTrue())

			Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
			Expect(latestVR.CheckOrder()).To(Equal(4))
		})

		Context("when the versions already exists", func() {
			var newVersionSlice []atc.Version

			BeforeEach(func() {
				newVersionSlice = []atc.Version{
					{"ref": "v1"},
					{"ref": "v3"},
				}

				err := resourceScope.SaveVersions(nil, originalVersionSlice)
				Expect(err).ToNot(HaveOccurred())

				latestVR, found, err := resourceScope.LatestVersion()
				Expect(err).ToNot(HaveOccurred())
				Expect(found).To(BeTrue())

				Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
				Expect(latestVR.CheckOrder()).To(Equal(2))
			})

			It("does not change the check order", func() {
				err := resourceScope.SaveVersions(nil, newVersionSlice)
				Expect(err).ToNot(HaveOccurred())

				latestVR, found, err := resourceScope.LatestVersion()
				Expect(err).ToNot(HaveOccurred())
				Expect(found).To(BeTrue())

				Expect(latestVR.Version()).To(Equal(db.Version{"ref": "v3"}))
				Expect(latestVR.CheckOrder()).To(Equal(2))
			})

			Context("when a new version is added for non-trigger resource", func() {
				It("should not request schedule on the jobs that use the resource", func() {
					err := resourceScope.SaveVersions(nil, originalVersionSlice)
					Expect(err).ToNot(HaveOccurred())

					requestedSchedule := scenario.Job("some-job").ScheduleRequestedTime()

					newVersions := []atc.Version{
						{"ref": "v0"},
						{"ref": "v3"},
{'fraction_non_alphanumeric': 0.16818293582493238, 'fraction_numerical': 0.003934103761986722, 'mean_word_length': 3.82502965599051, 'pattern_counts': {'":': 26, '<': 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': '5199189', 'n_tokens_mistral': 3341, 'n_tokens_neox': 2716, 'n_words': 456}