<commit_before>﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.OData.Client;
using Microsoft.OData.Core;
using Microsoft.OData.Edm.Csdl;
using Microsoft.Restier.WebApi.Test.Services.Trippin.Models;
using Xunit;

namespace Microsoft.Restier.WebApi.Test.Scenario
{
    public class TrippinE2ETestCases : E2ETestBase<TrippinModel>
    {
        public TrippinE2ETestCases()
            : base(new Uri("http://localhost:18384/api/Trippin/"))
        {
        }

        [Fact]
        public void AnnotationComputedOptimisticConcurrency()
        {
            var requestMessage = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "GET",
                    new Uri(this.ServiceBaseUri.OriginalString + "$metadata", UriKind.Absolute),
                    true,
                    false,
                    new Dictionary<string, string>()));
            using (var r = new StreamReader(requestMessage.GetResponse().GetStream()))
            {
                var modelStr = r.ReadToEnd();
                Assert.Contains("<Annotation Term=\"Org.OData.Core.V1.Computed\" Bool=\"true\" />", modelStr, StringComparison.Ordinal);
                Assert.Contains("<Annotation Term=\"Org.OData.Core.V1.OptimisticConcurrency\">", modelStr, StringComparison.Ordinal);
            }
        }

        [Fact]
        public void AnnotationRequiredMaxMinLengthTimestamp()
        {
            var requestMessage = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "GET",
                    new Uri(this.ServiceBaseUri.OriginalString + "$metadata", UriKind.Absolute),
                    useDefaultCredentials: true,
                    usePostTunneling: false,
                    headers: new Dictionary<string, string>()));
            using (var r = new StreamReader(requestMessage.GetResponse().GetStream()))
            {
                var modelStr = r.ReadToEnd();

                // [Required] ==> Nullable="false"
                Assert.Contains(
                    "<Property Name=\"FirstName\" Type=\"Edm.String\" Nullable=\"false\" MaxLength=\"max\" />",
                    modelStr,
                    StringComparison.Ordinal);

                // [MaxLength] [MinLength] --> only MaxLength=".."
                Assert.Contains(
                    "<Property Name=\"LastName\" Type=\"Edm.String\" MaxLength=\"26\" />",
                    modelStr,
                    StringComparison.Ordinal);

                // [Timestamp] ==> Computed
                Assert.Contains(
                    "<Property Name=\"TimeStampValue\" Type=\"Edm.Binary\" Nullable=\"false\" MaxLength=\"8\">\r\n"
                        + "          <Annotation Term=\"Org.OData.Core.V1.Computed\" Bool=\"true\" />",
                    modelStr,
                    StringComparison.Ordinal);
            }
        }

        [Fact]
        public void CURDEntity()
        {
            this.TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges;
            // Post an entity
            Person person = new Person()
            {
                FirstName = "Sheldon",
                LastName = "Cooper",
                UserName = "SheldonCooper"
            };

            this.TestClientContext.AddToPeople(person);
            this.TestClientContext.SaveChanges();
            int personId = person.PersonId;

            // Count this entity
            var count = this.TestClientContext.People.Count();
            Assert.Equal(personId, count);

            // Update an entity
            person.LastName = "Lee";
            this.TestClientContext.UpdateObject(person);
            this.TestClientContext.SaveChanges();

            // Query an entity
            this.TestClientContext.Detach(person);
            person = this.TestClientContext.People
                .ByKey(new Dictionary<string, object>() { { "PersonId", personId } }).GetValue();
            Assert.Equal("Lee", person.LastName);

            // Delete an entity
            this.TestClientContext.DeleteObject(person);
            this.TestClientContext.SaveChanges();

            // Filter this entity
            var persons = this.TestClientContext.People
                .AddQueryOption("$filter", string.Format("PersonId eq {0}", personId)).ToList();
            Assert.Equal(0, persons.Count);
        }

        [Fact]
        public void UQProperty()
        {
            this.TestClientContext.MergeOption = MergeOption.OverwriteChanges;<commit_msg>Add e2e case for complex type.<commit_after>﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.OData.Client;
using Microsoft.OData.Core;
using Microsoft.OData.Edm.Csdl;
using Microsoft.Restier.WebApi.Test.Services.Trippin.Models;
using Xunit;

namespace Microsoft.Restier.WebApi.Test.Scenario
{
    public class TrippinE2ETestCases : E2ETestBase<TrippinModel>
    {
        public TrippinE2ETestCases()
            : base(new Uri("http://localhost:18384/api/Trippin/"))
        {
        }

        [Fact]
        public void AnnotationComputedOptimisticConcurrency()
        {
            var requestMessage = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "GET",
                    new Uri(this.ServiceBaseUri.OriginalString + "$metadata", UriKind.Absolute),
                    true,
                    false,
                    new Dictionary<string, string>()));
            using (var r = new StreamReader(requestMessage.GetResponse().GetStream()))
            {
                var modelStr = r.ReadToEnd();
                Assert.Contains("<Annotation Term=\"Org.OData.Core.V1.Computed\" Bool=\"true\" />", modelStr, StringComparison.Ordinal);
                Assert.Contains("<Annotation Term=\"Org.OData.Core.V1.OptimisticConcurrency\">", modelStr, StringComparison.Ordinal);
            }
        }

        [Fact]
        public void AnnotationRequiredMaxMinLengthTimestamp()
        {
            var requestMessage = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "GET",
                    new Uri(this.ServiceBaseUri.OriginalString + "$metadata", UriKind.Absolute),
                    useDefaultCredentials: true,
                    usePostTunneling: false,
                    headers: new Dictionary<string, string>()));
            using (var r = new StreamReader(requestMessage.GetResponse().GetStream()))
            {
                var modelStr = r.ReadToEnd();

                // [Required] ==> Nullable="false"
                Assert.Contains(
                    "<Property Name=\"FirstName\" Type=\"Edm.String\" Nullable=\"false\" MaxLength=\"max\" />",
                    modelStr,
                    StringComparison.Ordinal);

                // [MaxLength] [MinLength] --> only MaxLength=".."
                Assert.Contains(
                    "<Property Name=\"LastName\" Type=\"Edm.String\" MaxLength=\"26\" />",
                    modelStr,
                    StringComparison.Ordinal);

                // [Timestamp] ==> Computed
                Assert.Contains(
                    "<Property Name=\"TimeStampValue\" Type=\"Edm.Binary\" Nullable=\"false\" MaxLength=\"8\">\r\n"
                        + "          <Annotation Term=\"Org.OData.Core.V1.Computed\" Bool=\"true\" />",
                    modelStr,
                    StringComparison.Ordinal);
            }
        }

        [Fact]
        public void CURDEntity()
        {
            this.TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges;
            // Post an entity
            Person person = new Person()
            {
                FirstName = "Sheldon",
                LastName = "Cooper",
                UserName = "SheldonCooper"
            };

            this.TestClientContext.AddToPeople(person);
            this.TestClientContext.SaveChanges();
            int personId = person.PersonId;

            // Count this entity
            var count = this.TestClientContext.People.Count();
            Assert.Equal(personId, count);

            // Update an entity
            person.LastName = "Lee";
            this.TestClientContext.UpdateObject(person);
            this.TestClientContext.SaveChanges();

            // Query an entity
            this.TestClientContext.Detach(person);
            person = this.TestClientContext.People
                .ByKey(new Dictionary<string, object>() { { "PersonId", personId } }).GetValue();
            Assert.Equal("Lee", person.LastName);

            // Delete an entity
            this.TestClientContext.DeleteObject(person);
            this.TestClientContext.SaveChanges();

            // Filter this entity
            var persons = this.TestClientContext.People
                .AddQueryOption("$filter", string.Format("PersonId eq {0}", personId)).ToList();
            Assert.Equal(0, persons.Count);
        }

        [Fact]
        public void CURDEntityWithComplexType()
        {
            this.TestClientContext.MergeOption = MergeOption.OverwriteChanges;

            // Post an entity
            var entry = new Event
            {
                Description = "event1",
                OccursAt = new Location { Address = "address1" }
            };

            this.TestClientContext.AddToEvents(entry);
            this.TestClientContext.SaveChanges();
            int eventId = entry.Id;
            this.TestClientContext.Detach(entry);

            // Query this entity
            entry =
                this.TestClientContext.Events.ByKey(new Dictionary<string, object> { { "Id", eventId } }).GetValue();
            Assert.Equal("event1", entry.Description);
            Assert.Equal("address1", entry.OccursAt.Address);
            this.TestClientContext.Detach(entry);

            // PUT this entity
            entry = new Event
            {
                Id = eventId,
                Description = "event2",
                OccursAt = new Location { Address = "address2" }
            };
            this.TestClientContext.AttachTo("Events", entry);
            this.TestClientContext.UpdateObject(entry);
            this.TestClientContext.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
            this.TestClientContext.Detach(entry);

            // Query this entity
            entry =
                this.TestClientContext.Events.ByKey(new Dictionary<string, object> { { "Id", eventId } }).GetValue();
            Assert.Equal("event2", entry.Description);
            Assert.Equal("address2", entry.OccursAt.Address);

            // Update an entity
            entry.OccursAt.Address = "address3";
            this.TestClientContext.UpdateObject(entry);
            this.TestClientContext.SaveChanges();
            this.TestClientContext.Detach(entry);

            // Query this entity
            entry =
                this.TestClientContext.Events.ByKey(new Dictionary<string, object> { { "Id", eventId } }).GetValue();
            Assert.Equal("event2", entry.Description);
            Assert.Equal("address3", entry.OccursAt.Address);

            // Delete an entity
            var count = this.TestClientContext.Events
                .AddQueryOption("$filter", string.Format("Id eq {0}", eventId)).ToList().Count;
            Assert.Equal(1, count);
            this.TestClientContext.DeleteObject(entry);
            this.TestClientContext.SaveChanges();
            count = this.TestClientContext.Events
                .AddQueryOption("$filter", string.Format("Id eq {0}", eventId)).ToList().Count;
            Assert.Equal(0, count);
        }

        [Fact]
        public void UQProperty()
        {
            this.TestClientContext.MergeOption = MergeOption.OverwriteChanges;
{'fraction_non_alphanumeric': 0.11091219790056148, 'fraction_numerical': 0.0039059321344291644, 'mean_word_length': 1.7705139765554554, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7593997', 'n_tokens_mistral': 3129, 'n_tokens_neox': 2987, 'n_words': 583}