<commit_before>﻿using System;
using System.Data.SqlClient;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Exceptions;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.Component.Basic
{
	public class ComponentWithUniqueConstraintTests : TestCaseMappingByCode
	{
		protected override bool AppliesTo(Dialect.Dialect dialect)
		{
			return dialect is MsSql2008Dialect;
		}

		protected override HbmMapping GetMappings()
		{
			var mapper = new ModelMapper();

			mapper.Component<Person>(comp =>
			{
				comp.Property(p => p.Name);
				comp.Property(p => p.Dob);
				comp.Unique(true); // hbm2ddl: Generate a unique constraint in the database
			});

			mapper.Class<Employee>(cm =>
			{
				cm.Id(employee => employee.Id, map => map.Generator(Generators.HighLow));
				cm.Property(employee => employee.HireDate);
				cm.Component(person => person.Person);
			});

			return mapper.CompileMappingForAllExplicitlyAddedEntities();
		}

		protected override void OnTearDown()
		{
			using (var session = sessions.OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				session.Delete("from Employee");
				transaction.Commit();
			}
		}

		[Test]
		public void CanBePersistedWithUniqueValues()
		{
			using (var session = OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Hillary", Dob = new DateTime(2000, 1, 1) } };
				session.Save(e1);
				session.Save(e2);
				transaction.Commit();
			}

			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var employees = session.Query<Employee>().ToList();
				Assert.That(employees.Count, Is.EqualTo(2));
				Assert.That(employees.Select(employee => employee.Person.Name).ToArray(), Is.EquivalentTo(new[] { "Hillary", "Bill" }));
			}
		}

		[Test]
		public void CannotBePersistedWithNonUniqueValues()
		{
			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };

				Assert.That(() =>
				{
					session.Save(e1);
					session.Save(e2);
					session.Flush();
				},
				Throws.Exception.TypeOf<GenericADOException>()
					.With.InnerException.TypeOf<SqlException>()
					.And.InnerException.Message.StringContaining("UNIQUE KEY"));
			}
		}
	}
}<commit_msg>Test fix for ODBC (related to NH-3024).<commit_after>﻿using System;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Exceptions;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.Component.Basic
{
	public class ComponentWithUniqueConstraintTests : TestCaseMappingByCode
	{
		protected override bool AppliesTo(Dialect.Dialect dialect)
		{
			return dialect is MsSql2008Dialect;
		}

		protected override HbmMapping GetMappings()
		{
			var mapper = new ModelMapper();

			mapper.Component<Person>(comp =>
			{
				comp.Property(p => p.Name);
				comp.Property(p => p.Dob);
				comp.Unique(true); // hbm2ddl: Generate a unique constraint in the database
			});

			mapper.Class<Employee>(cm =>
			{
				cm.Id(employee => employee.Id, map => map.Generator(Generators.HighLow));
				cm.Property(employee => employee.HireDate);
				cm.Component(person => person.Person);
			});

			return mapper.CompileMappingForAllExplicitlyAddedEntities();
		}

		protected override void OnTearDown()
		{
			using (var session = sessions.OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				session.Delete("from Employee");
				transaction.Commit();
			}
		}

		[Test]
		public void CanBePersistedWithUniqueValues()
		{
			using (var session = OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Hillary", Dob = new DateTime(2000, 1, 1) } };
				session.Save(e1);
				session.Save(e2);
				transaction.Commit();
			}

			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var employees = session.Query<Employee>().ToList();
				Assert.That(employees.Count, Is.EqualTo(2));
				Assert.That(employees.Select(employee => employee.Person.Name).ToArray(), Is.EquivalentTo(new[] { "Hillary", "Bill" }));
			}
		}

		[Test]
		public void CannotBePersistedWithNonUniqueValues()
		{
			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };

				var exception = Assert.Throws<GenericADOException>(() =>
					{
						session.Save(e1);
						session.Save(e2);
						session.Flush();
					});
				Assert.That(exception.InnerException, Is.TypeOf<SqlException>().Or.TypeOf<OdbcException>());
				Assert.That(exception.InnerException.Message, Is.StringContaining("UNIQUE KEY"));
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.13487306064880114, 'fraction_numerical': 0.014104372355430184, 'mean_word_length': 3.7833052276559864, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 34, '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': '4895725', 'n_tokens_mistral': 2180, 'n_tokens_neox': 1754, 'n_words': 399}