public When_Using_Sharded_Servers() { server = "localhost"; port1 = 8080; port2 = 8081; path1 = GetPath("TestShardedDb1"); path2 = GetPath("TestShardedDb2"); NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port1); NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port2); company1 = new Company { Name = "Company1" }; company2 = new Company { Name = "Company2" }; server1 = GetNewServer(port1, path1); server2 = GetNewServer(port2, path2); shards = new Shards { new DocumentStore { Identifier="Shard1", Url = "http://" + server +":"+port1}, new DocumentStore { Identifier="Shard2", Url = "http://" + server +":"+port2} }; shardSelection = MockRepository.GenerateStub<IShardSelectionStrategy>(); shardSelection.Stub(x => x.ShardIdForNewObject(company1)).Return("Shard1"); shardSelection.Stub(x => x.ShardIdForNewObject(company2)).Return("Shard2"); shardResolution = MockRepository.GenerateStub<IShardResolutionStrategy>(); shardStrategy = MockRepository.GenerateStub<IShardStrategy>(); shardStrategy.Stub(x => x.ShardSelectionStrategy).Return(shardSelection); shardStrategy.Stub(x => x.ShardResolutionStrategy).Return(shardResolution); }
public void WhenDocumentAlreadyExists_Can_Still_Generate_Values() { using (var store = NewDocumentStore()) { var mk = new MultiTypeHiLoKeyGenerator(store.DatabaseCommands, 5); store.Conventions.DocumentKeyGenerator = o => mk.GenerateDocumentKey(store.Conventions, o); using (var session = store.OpenSession()) { var company = new Company(); session.Store(company); var contact = new Contact(); session.Store(contact); Assert.Equal("companies/1", company.Id); Assert.Equal("contacts/1", contact.Id); } mk = new MultiTypeHiLoKeyGenerator(store.DatabaseCommands, 5); store.Conventions.DocumentKeyGenerator = o => mk.GenerateDocumentKey(store.Conventions, o); using (var session = store.OpenSession()) { var company = new Company(); session.Store(company); var contact = new Contact(); session.Store(contact); Assert.Equal("companies/6", company.Id); Assert.Equal("contacts/6", contact.Id); } } }
public void Can_delete_by_index() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialize(); var entity = new Company { Name = "Company" }; using (var session = documentStore.OpenSession()) { session.Store(entity); session.SaveChanges(); session.LuceneQuery<Company>().WaitForNonStaleResults().ToArray();// wait for the index to settle down } documentStore.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery { Query = "Tag:[[Companies]]" }, allowStale: false); using (var session = documentStore.OpenSession()) { Assert.Empty(session.LuceneQuery<Company>().WaitForNonStaleResults().ToArray()); } } }
public void Can_insert_sync_and_get_async() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialize(); var entity = new Company { Name = "Async Company" }; using (var session = documentStore.OpenSession()) { session.Store(entity); session.SaveChanges(); } using (var session = documentStore.OpenAsyncSession()) { var asyncResult = session.BeginLoad(entity.Id, null, null); if (asyncResult.CompletedSynchronously == false) asyncResult.AsyncWaitHandle.WaitOne(); var company = session.EndLoad<Company>(asyncResult); Assert.Equal("Async Company", company.Name); } } }
public void Will_get_notification_when_reading_non_authoritive_information() { using (var documentStore = NewDocumentStore()) { var company = new Company { Name = "Company Name" }; var session = documentStore.OpenSession(); using (var original = documentStore.OpenSession()) { original.Store(company); original.SaveChanges(); } using ( new TransactionScope()) { company.Name = "Another Name"; session.Store(company); session.SaveChanges(); using (new TransactionScope(TransactionScopeOption.Suppress)) { using (var session2 = documentStore.OpenSession()) { session2.AllowNonAuthoritiveInformation = false; Assert.Throws<NonAuthoritiveInformationException>(()=>session2.Load<Company>(company.Id)); } } } } }
public void Can_project_from_index() { using (var documentStore = NewDocumentStore()) { var session = documentStore.OpenSession(); var company = new Company { Name = "Company 1", Phone = 5}; session.Store(company); session.SaveChanges(); documentStore.DatabaseCommands.PutIndex("company_by_name", new IndexDefinition { Map = "from doc in docs where doc.Name != null select new { doc.Name, doc.Phone}", Stores = {{"Name", FieldStorage.Yes}, {"Phone",FieldStorage.Yes}} }); var q = session .Query<Company>("company_by_name") .SelectFields<Company>("Name", "Phone") .WaitForNonStaleResults(); var single = q.Single(); Assert.Equal("Company 1", single.Name); Assert.Equal(5, single.Phone); } }
public void Can_insert_into_two_servers_running_simultaneously_without_sharding() { var serversStoredUpon = new List<string>(); using (var server1 = GetNewServer(port1, path1)) using (var server2 = GetNewServer(port2, path2)) { foreach (var port in new[] { port1, port2 }) { using (var documentStore = new DocumentStore { Url = "http://localhost:"+ port }.Initialise()) using (var session = documentStore.OpenSession()) { documentStore.Stored += (storeServer, storeEntity) => serversStoredUpon.Add(storeServer); var entity = new Company { Name = "Company" }; session.Store(entity); session.SaveChanges(); Assert.NotEqual(Guid.Empty.ToString(), entity.Id); } } } Assert.Contains(port1.ToString(), serversStoredUpon[0]); Assert.Contains(port2.ToString(), serversStoredUpon[1]); }
public void IdIsSetFromGeneratorOnStore() { using (var store = NewDocumentStore()) { using (var session = store.OpenSession()) { Company company = new Company(); session.Store(company); Assert.Equal("companies/1", company.Id); } } }
public void Should_insert_into_db_and_set_id() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:"+ port }; documentStore.Initialise(); var session = documentStore.OpenSession(); var entity = new Company {Name = "Company"}; session.Store(entity); session.SaveChanges(); Assert.NotEqual(Guid.Empty.ToString(), entity.Id); } }
public void DifferentTypesWillHaveDifferentIdGenerators() { using (var store = NewDocumentStore()) { using (var session = store.OpenSession()) { var company = new Company(); session.Store(company); var contact = new Contact(); session.Store(contact); Assert.Equal("companies/1", company.Id); Assert.Equal("contacts/1", contact.Id); } } }
public void After_tx_rollback_value_will_not_be_in_the_database() { using (var documentStore = NewDocumentStore()) { var company = new Company { Name = "Company Name" }; var session = documentStore.OpenSession(); using (new TransactionScope()) { session.Store(company); session.SaveChanges(); } using (var session2 = documentStore.OpenSession()) Assert.Null(session2.Load<Company>(company.Id)); } }
public void TotalResultIsIncludedInQueryResult() { using (var server = GetNewServer(port, path)) { using (var store = new DocumentStore { Url = "http://localhost:" + port }) { store.Initialize(); using (var session = store.OpenSession()) { Company company1 = new Company() { Name = "Company1", Address1 = "", Address2 = "", Address3 = "", Contacts = new List<Contact>(), Phone = 2 }; Company company2 = new Company() { Name = "Company2", Address1 = "", Address2 = "", Address3 = "", Contacts = new List<Contact>(), Phone = 2 }; session.Store(company1); session.Store(company2); session.SaveChanges(); } using (var session = store.OpenSession()) { int resultCount = session.Advanced.LuceneQuery<Company>().WaitForNonStaleResults().QueryResult.TotalResults; Assert.Equal(2, resultCount); } } } }
public void Can_refresh_entity_from_database() { using (var documentStore = NewDocumentStore()) { var company = new Company {Name = "Company Name"}; var session = documentStore.OpenSession(); session.Store(company); session.SaveChanges(); var session2 = documentStore.OpenSession(); var company2 = session2.Load<Company>(company.Id); company2.Name = "Hibernating Rhinos"; session2.Store(company2); session2.SaveChanges(); session.Refresh(company); Assert.Equal(company2.Name, company.Name); } }
public void Can_use_transactions_to_isolate_saves() { using (var documentStore = NewDocumentStore()) { var company = new Company { Name = "Company Name" }; var session = documentStore.OpenSession(); using (RavenTransactionAccessor.StartTransaction()) { session.Store(company); session.SaveChanges(); using (RavenTransactionAccessor.StartTransaction()) { using (var session2 = documentStore.OpenSession()) Assert.Null(session2.Load<Company>(company.Id)); } Assert.NotNull(session.Load<Company>(company.Id)); documentStore.DatabaseCommands.Commit(RavenTransactionAccessor.GetTransactionInformation().Id); } Assert.NotNull(session.Load<Company>(company.Id)); } }
public void Can_use_transactions_to_isolate_saves() { using (var documentStore = NewDocumentStore()) { var company = new Company { Name = "Company Name" }; var session = documentStore.OpenSession(); using (var tx = new TransactionScope()) { session.Store(company); session.SaveChanges(); using (new TransactionScope(TransactionScopeOption.Suppress)) { using (var session2 = documentStore.OpenSession()) Assert.Null(session2.Load<Company>(company.Id)); tx.Complete(); } } Assert.NotNull(session.Load<Company>(company.Id)); } }
public void Can_delete_document() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore("localhost", port); documentStore.Initialise(); var session = documentStore.OpenSession(); var entity = new Company { Name = "Company" }; session.Store(entity); session.SaveChanges(); using(var session2 = documentStore.OpenSession()) Assert.NotNull(session2.Load<Company>(entity.Id)); session.Delete(entity); session.SaveChanges(); using (var session3 = documentStore.OpenSession()) Assert.Null(session3.Load<Company>(entity.Id)); } }
public void Can_insert_async_and_get_sync() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialize(); var entity = new Company { Name = "Async Company" }; using (var session = documentStore.OpenAsyncSession()) { session.Store(entity); var ar = session.BeginSaveChanges(null,null); ar.AsyncWaitHandle.WaitOne(); session.EndSaveChanges(ar); } using (var session = documentStore.OpenSession()) { var company = session.Load<Company>(entity.Id); Assert.Equal("Async Company", company.Name); } } }
public void Can_promote_transactions() { var documentStore = new DocumentStore {Url = "http://localhost:8080"}; documentStore.Initialize(); var company = new Company {Name = "Company Name"}; using (var tx = new TransactionScope()) { var session = documentStore.OpenSession(); session.Store(company); session.SaveChanges(); Assert.Equal(Guid.Empty, Transaction.Current.TransactionInformation.DistributedIdentifier); using (var session3 = documentStore.OpenSession()) { session3.Store(new Company {Name = "Another company"}); session3.SaveChanges(); // force a dtc promotion Assert.NotEqual(Guid.Empty, Transaction.Current.TransactionInformation.DistributedIdentifier); } tx.Complete(); } for (int i = 0; i < 15; i++)// wait for commit { using (var session2 = documentStore.OpenSession()) if (session2.Load<Company>(company.Id) != null) break; Thread.Sleep(100); } using (var session2 = documentStore.OpenSession()) Assert.NotNull((session2.Load<Company>(company.Id))); }
public void KeysGeneratedFromDifferentSessionsAreConsecutive() { using(var store = NewDocumentStore()) { var c1 = new Company(); var c2 = new Company(); using(var s = store.OpenSession()) { s.Store(c1); s.SaveChanges(); } using (var s = store.OpenSession()) { s.Store(c2); s.SaveChanges(); } Assert.Equal("companies/1", c1.Id); Assert.Equal("companies/2", c2.Id); } }
public void Should_update_retrieved_entity() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); var session1 = documentStore.OpenSession(); var company = new Company {Name = "Company 1"}; session1.Store(company); session1.SaveChanges(); var companyId = company.Id; var session2 = documentStore.OpenSession(); var companyFound = session2.Load<Company>(companyId); companyFound.Name = "New Name"; session2.SaveChanges(); Assert.Equal("New Name", session2.Load<Company>(companyId).Name); } }
public void Should_update_stored_entity() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); var session = documentStore.OpenSession(); var company = new Company {Name = "Company 1"}; session.Store(company); session.SaveChanges(); var id = company.Id; company.Name = "Company 2"; session.SaveChanges(); var companyFound = session.Load<Company>(company.Id); Assert.Equal("Company 2", companyFound.Name); Assert.Equal(id, company.Id); } }
public void Can_rollback_transaction_on_insert() { using (var server = GetNewServer(port, path)) { string id; var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); using (var session = documentStore.OpenSession()) { using (var tx = new TransactionScope()) { var company = new Company { Name = "Company 1" }; session.Store(company); session.SaveChanges(); id = company.Id; } } using (var session2 = documentStore.OpenSession()) { Assert.Null(session2.Load<Company>(id)); } } }
public void Optimistic_concurrency() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); var session = documentStore.OpenSession(); session.UseOptimisticConcurrency = true; var company = new Company { Name = "Company 1" }; session.Store(company); session.SaveChanges(); using (var session2 = documentStore.OpenSession()) { var company2 = session2.Load<Company>(company.Id); company2.Name = "foo"; session2.SaveChanges(); } company.Name = "Company 2"; Assert.Throws<ConcurrencyException>(() => session.SaveChanges()); } }
public void Can_select_from_index_using_linq_method_chain_using_integer_and_greater_than_or_equal() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); var session = documentStore.OpenSession(); var company = new Company { Name = "Company 1", Phone = 5 }; session.Store(company); session.SaveChanges(); documentStore.DatabaseCommands.PutIndex("company_by_name", new IndexDefinition { Map = "from doc in docs where doc.Name != null select new { doc.Name, doc.Phone}", Stores = { { "Name", FieldStorage.Yes }, { "Phone", FieldStorage.Yes } } }); var q = session.Query<Company>("company_by_name") .Customize(query => query.WaitForNonStaleResults(TimeSpan.FromHours(1))) .Where(x => x.Phone > 1); var single = q.ToArray()[0]; Assert.Equal("Company 1", single.Name); Assert.Equal(5, single.Phone); } }
public void Can_store_and_get_array_metadata() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); var session = documentStore.OpenSession(); session.OnEntityConverted += (entity, document, metadata) => { metadata["Raven-Allowed-Users"] = new JArray("ayende", "oren", "rob"); }; var company = new Company { Name = "Company" }; session.Store(company); session.SaveChanges(); var metadataFromServer = session.GetMetadataFor(session.Load<Company>(company.Id)); var users = metadataFromServer["Raven-Allowed-Users"].OfType<JValue>().Select(x => (string)x.Value).ToArray(); Assert.Equal(new[]{"ayende","oren","rob"}, users); } }
public void Can_get_entity_back_with_enum() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); var company = new Company { Name = "Company Name", Type = Company.CompanyType.Private }; var session = documentStore.OpenSession(); session.Store(company); session.SaveChanges(); session = documentStore.OpenSession(); var companyFound = session.Load<Company>(company.Id); Assert.Equal(companyFound.Type, company.Type); } }
public void Can_update_by_index() { using (var store = NewDocumentStore()) { var entity = new Company { Name = "Company" }; using (var session = store.OpenSession()) { session.Store(entity); session.SaveChanges(); session.Advanced.LuceneQuery<Company>().WaitForNonStaleResults().ToArray();// wait for the index to settle down } store.DatabaseCommands.UpdateByIndex("Raven/DocumentsByEntityName", new IndexQuery { Query = "Tag:[[Companies]]" }, new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "Name", Value = JToken.FromObject("Another Company") }, }, allowStale: false); using (var session = store.OpenSession()) { Assert.Equal("Another Company", session.Load<Company>(entity.Id).Name); } } }
public void Can_insert_async_and_multi_get_async() { using (var server = GetNewServer(port, path)) { var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialize(); var entity1 = new Company { Name = "Async Company #1" }; var entity2 = new Company { Name = "Async Company #2" }; using (var session = documentStore.OpenAsyncSession()) { session.Store(entity1); session.Store(entity2); var ar = session.BeginSaveChanges(null, null); ar.AsyncWaitHandle.WaitOne(); session.EndSaveChanges(ar); } using (var session = documentStore.OpenAsyncSession()) { var asyncResult = session.BeginMultiLoad(new[]{entity1.Id, entity2.Id}, null,null); asyncResult.AsyncWaitHandle.WaitOne(); var companies = session.EndMultiLoad<Company>(asyncResult); Assert.Equal(entity1.Name, companies[0].Name); Assert.Equal(entity2.Name, companies[1].Name); } } }
public void Can_insert_with_transaction() { using (var server = GetNewServer(port, path)) { const string id = "Company/id"; var documentStore = new DocumentStore { Url = "http://localhost:" + port }; documentStore.Initialise(); using (var session = documentStore.OpenSession()) { Assert.Null(session.Load<Company>(id)); using (var tx = new TransactionScope()) { var company = new Company { Id = id, Name = "Company 1" }; session.Store(company); session.SaveChanges(); tx.Complete(); } Assert.NotNull(session.Load<Company>(id)); } } }
public void NoIdIsSetAndSoIdIsSetAfterSaveChanges() { using (var store = NewDocumentStore()) { store.Conventions.DocumentKeyGenerator = fun => null; using (var session = store.OpenSession()) { Company company = new Company(); session.Store(company); session.SaveChanges(); Assert.NotNull(company.Id); } } }