ryanheath / RavenDB-NodaTime

Noda Time support for RavenDB
Other
20 stars 14 forks source link

LocalDate as key in dictionary #5

Closed di97mni closed 9 years ago

di97mni commented 9 years ago

Any suggestions how to handle this case.

This query returns 0 hits.

var result = await session.Query<SampleData, DailyPriceListIndex>()
                    .ProjectFromIndexFieldsInto<DailyPriceListIndex.Result>()
                    .Where(p => p.Date >= startDate && p.Date < endDate)
                    .ToListAsync();

Failing test. You need the RDB server and Nodatime bundle installed.

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NodaTime;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Tests.Helpers;
using Xunit;

namespace FilterByNodatTime
{
    public class IndexTest : RavenTestBase
    {
        [Fact]
        public async Task CanIndexAndQuery()
        {
            var store = new DocumentStore
            {
                ConnectionStringName = "RavenDB",
                DefaultDatabase = "FilterByNodatTime"
            };

            store.Initialize(true);

            new DailyPriceListIndex().Execute(store);

            using (var session = store.OpenAsyncSession())
            {
                for (int i = 0; i < 2; i++)
                {
                    await session.StoreAsync(new SampleData
                    {
                        ADay = new LocalDate(2015, 3, 20),
                        Prices = GeneratePrices()
                    });
                }
                await session.SaveChangesAsync();
            }

            var startDate = new LocalDate(2015, 3, 24);
            var endDate = startDate.PlusDays(2);

            using (var session = store.OpenAsyncSession())
            {
                var result = await session.Query<SampleData, DailyPriceListIndex>()
                    .ProjectFromIndexFieldsInto<DailyPriceListIndex.Result>()
                    .Where(p => p.Date >= startDate && p.Date < endDate)
                    .ToListAsync();

                Assert.NotEmpty(result);
            }
        }

        private static Dictionary<LocalDate, decimal?> GeneratePrices()
        {
            var prices = new Dictionary<LocalDate, decimal?>();
            var startDate = new LocalDate(2015, 3, 20);
            for (int i = 0; i < 5; i++)
            {
                prices.Add(startDate.PlusDays(i), 100);
            }
            return prices;
        }
    }

    public class SampleData
    {
        public Dictionary<LocalDate, decimal?> Prices { get; set; }
        public LocalDate ADay { get; set; }
    }

    public class DailyPriceListIndex : AbstractIndexCreationTask<SampleData>
    {
        public DailyPriceListIndex()
        {
            Map = pricelists => from pricelist in pricelists
                from price in pricelist.Prices
                select new
                {
                    Date = price.Key,
                    Price = price.Value
                };

            StoreAllFields(FieldStorage.Yes);
        }

        public class Result
        {
            public LocalDate Date { get; set; }
            public decimal? Price { get; set; }
        }
    }
}

Example of document in RDB. Notice the format of the key.

{
    "Prices": {
        "den 20 mars 2015": 100,
        "den 21 mars 2015": 100,
        "den 22 mars 2015": 100,
        "den 23 mars 2015": 100,
        "den 24 mars 2015": 100,
        "den 25 mars 2015": 100,
        "den 26 mars 2015": 100,
        "den 27 mars 2015": 100,
        "den 28 mars 2015": 100,
        "den 29 mars 2015": 100
    },
    "ADay": {
        "ticks": 14268096000000000,
        "calendar": "ISO"
    }
}

Is there a way to get the key to be in the format of ADay instead?

"ADay": {
        "ticks": 14268096000000000,
        "calendar": "ISO"
    }

RavenDB Nodatime Bundle: 3.0.0.891 Packages

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="NodaTime" version="1.3.0" targetFramework="net451" />
  <package id="RavenDB.Client" version="3.0.3528" targetFramework="net451" />
  <package id="RavenDB.Client.NodaTime" version="3.0.0" targetFramework="net451" />
  <package id="RavenDB.Database" version="3.0.3528" targetFramework="net451" />
  <package id="RavenDB.Tests.Helpers" version="3.0.3528" targetFramework="net451" />
  <package id="xunit" version="1.9.2" targetFramework="net451" />
</packages>
di97mni commented 9 years ago

Forgot store.ConfigureForNodaTime(); Have an issue with the query above which is working for RDB 2.5 but not 3.0 Will try to recreate it..