Exemple #1
0
        public Uploadable CreateUplodable(UploadableArg uplodableArg, string personId, string userLoginId)
        {
            var uplodable = SetUplodable(uplodableArg, personId, userLoginId);

            _uplodableRepository.Create(uplodable);
            return(uplodable);
        }
        public IActionResult Create([FromBody] CreateEntityViewModel model)
        {
            try
            {
                var result = entityRepository.Create(new
                {
                    model.Name,
                    model.Description,
                    model.SourceConnectionId,
                    model.DestinationConnectionId,
                    model.SourceProcessorId,
                    model.DestinationProcessorId
                });

                if (model.Options != null && model.Options.Count() > 0)
                {
                    entityRepository.LinkOptions(result, model.Options);
                }

                transaction.Commit();
                return(Ok(result));
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public void TestExistentContact()
        {
            var currentTestContactRepository = new EntityRepository <Contact>("Contact");

            var contactGuid = currentTestContactRepository.Create(new Contact());

            Assert.IsNotNull(currentTestContactRepository.Get(contactGuid));
        }
        public void TestDeleteContact()
        {
            var currentTestContactRepository = new EntityRepository <Contact>("Contact");

            var contactGuid = currentTestContactRepository.Create(new Contact());

            currentTestContactRepository.Delete(contactGuid);

            Assert.AreEqual(0, currentTestContactRepository.All.Count());
        }
Exemple #5
0
        public void FindByBarcodeReturnsHoldingMatchingClassificationAndCopy()
        {
            var holding = new Holding {
                Classification = "AA123", CopyNumber = 2
            };

            repo.Create(holding);

            Assert.That(HoldingsControllerUtil.FindByBarcode(repo, "AA123:2"), Is.EqualTo(holding));
        }
        public void TestFetchAllContacts()
        {
            var currentTestContactRepository = new EntityRepository <Contact>("Contact");
            var contactsSaved = new Random().Next(0, 10);

            for (int i = 0; i < contactsSaved; i++)
            {
                Console.WriteLine(currentTestContactRepository.Create(new Contact()));
            }

            Assert.AreEqual(contactsSaved, currentTestContactRepository.All.Count());
        }
Exemple #7
0
        public virtual async Task <TGetItem> AddAsync(TAddItem itemDto, CancellationToken ct = default)
        {
            Logger.LogInformation($"Create {typeof(TEntity).Name}, data = {itemDto.ToJsonString()}");

            await ValidateItemAsync(itemDto, ct);

            TEntity entity = MapEntity(itemDto);

            EntityRepository.Create(entity);

            await UnitOfWork.SaveAsync(ct);

            return(Mapper.Map <TGetItem>(entity));
        }
        public void TestUpdateCompany()
        {
            var currentTestContactRepository = new EntityRepository <Contact>("Contact");

            var contact = DataGenerator.Build("John");

            contact.Identifier = currentTestContactRepository.Create(contact);

            var newCompanyName = "NewCompany";

            contact.Company = newCompanyName;
            currentTestContactRepository.Update(contact);

            Assert.AreEqual(newCompanyName, currentTestContactRepository.Get(contact.Identifier.Value).Company);
        }
        public void Test_Relationship_FromTypeDefaultValue(bool hasExistingValue)
        {
            // Setup
            EntityType targetType = new EntityType( );

            targetType.Save( );

            IEntity defInstance = EntityRepository.Create(targetType.Id);

            defInstance.Save( );

            IEntity anotherInstance = EntityRepository.Create(targetType.Id);

            defInstance.Save( );

            Relationship rel = new Relationship
            {
                FromType             = targetType,
                FromTypeDefaultValue = defInstance.As <Resource>( ),
                Cardinality_Enum     = CardinalityEnum_Enumeration.OneToOne
            };

            EntityType type = new EntityType
            {
                ReverseRelationships = { rel }
            };

            type.Save( );

            // Run
            IEntity entity = CreateInstance(type);

            if (hasExistingValue)
            {
                entity.GetRelationships(rel.Id, Direction.Reverse).Add(anotherInstance);
            }
            ApplyDefaults(type, entity);

            // Check
            var relValues = entity.GetRelationships(rel.Id, Direction.Reverse);
            var relValue  = relValues.SingleOrDefault( );

            long expected = hasExistingValue ? anotherInstance.Id : defInstance.Id;

            Assert.That(relValue, Is.Not.Null);
            Assert.That(relValue.Id, Is.EqualTo(expected));
        }
        public async Task Log(string userName)
        {
            using (var repo = new EntityRepository(new BaitkmDbContext(new DbContextOptions <BaitkmDbContext>())))
            {
                var caller = await repo.Filter <User>(x => (x.PhoneCode + x.Phone) == userName || x.Email == userName && x.RoleEnum != Role.Admin).FirstOrDefaultAsync();

                if (caller != null)
                {
                    repo.Create(new Statistic
                    {
                        ActivityDate = DateTime.UtcNow,
                        UserId       = caller.Id
                    });
                }
                await repo.SaveChangesAsync();
            }
        }
        public void Can_Create()
        {
            var user = new User
            {
                Username = "******",
                Email    = "*****@*****.**"
            };

            var repo = new EntityRepository <User>(GetContext());

            repo.Create(user);

            var loadedUser = repo.Get(user.Id);

            Assert.Equal(user.Id, loadedUser.Id);
            Assert.Equal(user.Username, loadedUser.Username);
            Assert.Equal(user.Email, loadedUser.Email);
        }
Exemple #12
0
        public Entity Create(long userId, Entity toAdd, Address address = null)
        {
            using (var tran = new TransactionScope())
            {
                var toRet = _repository.Create(toAdd);

                if (address != null)
                {
                    address.Entity = toRet;
                    //Create related address
                    var blAddress = new BlAddress();
                    blAddress.Create(userId, address);
                }

                BlLog.Log(userId, Module, "Create entity", "EntityCreated", new object[] { toAdd.Pin });
                tran.Complete();
                return(toRet);
            }
        }
        public async Task CalculatePrices()
        {
            using (var repository = new EntityRepository(new BaitkmDbContext(new DbContextOptions <BaitkmDbContext>())))
            {
                IEnumerable <Rate>     rates      = repository.Filter <Rate>(r => !r.IsDeleted);
                IEnumerable <Currency> currencies = repository.Filter <Currency>(c => c.Id != 1 && !c.IsDeleted);
                foreach (Currency c in currencies)
                {
                    using (var client = new HttpClient())
                    {
                        try
                        {
                            var response       = client.GetAsync($"https://itfllc.am/api/rate/exchange?from={1}&to={c.RequestId}&value={1}").Result;
                            var responseResult = JsonConvert.DeserializeObject <BaseExchangeResponseModel>(response.Content.ReadAsStringAsync().Result);

                            if (rates.Count() < 1)
                            {
                                repository.Create <Rate>(new Rate
                                {
                                    CurrencyId  = c.Id,
                                    CurrentRate = Math.Round(responseResult.Data.Result, 3, MidpointRounding.ToEven)
                                });
                            }
                            else
                            {
                                Rate rate = await repository.Filter <Rate>(r => r.CurrencyId == c.Id).FirstOrDefaultAsync();

                                rate.CurrentRate = Math.Round(responseResult.Data.Result, 3, MidpointRounding.ToEven);
                                repository.Update(rate);
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception(e.Message);
                        }
                    }
                }
                repository.SaveChanges();
            }
        }
        public async Task Expired()
        {
            using (var repository = new EntityRepository(new BaitkmDbContext(new DbContextOptions <BaitkmDbContext>())))
            {
                var announcements = await repository
                                    .Filter <Announcement>(x => !x.IsDraft && x.AnnouncementStatus == AnnouncementStatus.Accepted && x.AnnouncementStatusLastDay.Date == DateTime.UtcNow.Date)
                                    .ToListAsync();

                var announcemenExpired = await repository.Filter <Notification>(n =>
                                                                                n.NotificationType == AnnouncementNotificationType.Available).Include(n => n.NotificationTranslate).FirstOrDefaultAsync();

                foreach (var a in announcements)
                {
                    var questionerLanguage = repository.FilterAsNoTracking <PersonOtherSetting>(x =>
                                                                                                x.UserId == a.UserId).Select(s => s.Language).FirstOrDefault();
                    using (var firebaseRepository = new FirebaseRepository(new BaitkmDbContext(new DbContextOptions <BaitkmDbContext>())))
                    {
                        await firebaseRepository.SendIndividualNotification(new IndividualNotificationModel
                        {
                            Description      = $"{a.Title}",
                            GenericId        = a.Id,
                            NotificationType = NotificationType.AnnouncementAvailable,
                            ReceiverId       = a.UserId,
                            SenderId         = null,
                            Title            = questionerLanguage == Language.English ? announcemenExpired.Title
                     : announcemenExpired.NotificationTranslate.Select(n => n.Title).FirstOrDefault()
                        }, false);
                    }
                    repository.Create(new PersonNotification
                    {
                        AnnouncementId = a.Id,
                        UserId         = a.UserId,
                        NotificationId = announcemenExpired.Id
                    });
                }
            }
        }
Exemple #15
0
        public void EntityRepositoryCRUDTest()
        {
            var context = new NoodleDbContext("NoodleDb");

            context.Init();

            IEntityRepository repository = new EntityRepository(context);

            var id = Guid.NewGuid();

            var record = new Entity
            {
                Id   = id,
                Key  = "Key_" + id.ToString(),
                Name = "Name_" + id.ToString()
            };

            repository.Create(record);

            record.Key  = "U_" + record.Key;
            record.Name = "U_" + record.Name;

            repository.Update(record);

            var record2 = repository.GetById(id);

            Assert.AreEqual(record.Id, record2.Id);
            Assert.AreEqual(record.Key, record2.Key);
            Assert.AreEqual(record.Name, record2.Name);

            repository.Delete(record.Id);

            var record3 = repository.GetById(id);

            Assert.IsNull(record3);
        }
Exemple #16
0
        public void Test_EntityRepo_Create()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m,
                    Genre = Genre.Fantasy
                };
                #endregion

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                fb1.Genre = Genre.Mistery; //default value
                feist.IsAlive = true;//default value

                var q = new EntityQuery2("author", feist.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                q.Include("book", "author");
                var e = repository.Read(q);
                var created = new Author(e);
                Assert.AreEqual(feist.FirstName, created.FirstName);
                Assert.AreEqual(feist.LastName, created.LastName);
                Assert.AreEqual(feist.Born, created.Born);
                Assert.AreEqual(feist.IsAlive, created.IsAlive);
                Assert.AreEqual(feist.Rating, created.Rating);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());//repository.Detach(feist, new Relation("author", fb1));
                var eb1 = e.GetManyRelations("book", "author").First();
                Book b = new Book(eb1.Entity);
                Assert.AreEqual(fb1.Genre, b.Genre);
            }
        }
Exemple #17
0
        public async Task <IActionResult> CreateNode([FromBody] EntityCreateRequest createNodeRequest)
        {
            var nodes = await _repository.Create(_mapper.Map <Entity>(createNodeRequest));

            return(new JsonResult(nodes.FirstOrDefault()));
        }
 public ContactRepository_Test()
 {
     ContactRepository = new EntityRepository <Contact>("Contact");
     DataGenerator.Generate().ForEach(contact => ContactRepository.Create(contact));
 }
Exemple #19
0
        public void Test_EntityRepo_AttachDetach()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);

                #endregion

                repository.Attach(feist, new Relation("author", fb1));
                var rel2 = new Relation("author", fb2);
                var writtenOn = new DateTime(1996, 4, 25);
                rel2.SetData<DateTime>("WrittenOn", writtenOn);
                repository.Attach(feist, rel2);

                var q = new EntityQuery2("author", feist.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                q.Include("book", "author");
                var e = repository.Read(q);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());

                var bq = new EntityQuery2("book");
                bq.Include("author", "author");
                var bes = repository.Search(bq);
                foreach (var be in bes)
                {
                    Assert.AreEqual(1, be.RelationsData.Count);
                    Assert.AreEqual(feist.Id, be.GetSingleRelation("author", "author").Entity.Id);
                    if (be.Id == fb2.Id)
                        Assert.AreEqual(writtenOn, be.GetSingleRelation("author", "author").GetData<DateTime>("writtenon"));
                }

                repository.Detach(feist, new Relation("author", fb1));
                e = repository.Read(q);
                Assert.AreEqual(1, e.GetManyRelations("book", "author").Count());

                repository.Attach(fb1, new Relation("author", feist));
                e = repository.Read(q);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());
                repository.Detach(fb1, new Relation("author", feist));
                e = repository.Read(q);
                Assert.AreEqual(1, e.GetManyRelations("book", "author").Count());

                bool ex = false;
                try { repository.Attach(fb2, new Relation("author", jordan)); }
                catch (Exception) { ex = true; }
                Assert.IsTrue(ex, "Exception not thrown when attaching two authors to single book");
            }
        }
Exemple #20
0
        public void Test_EntityRepo_Count()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                var jb1 = new Book()
                {
                    Title = "The Shadow is Rising",
                    Price = 21.15m
                };
                var jb2 = new Book()
                {
                    Title = "The Eye of the World",
                    Price = 25.80m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Create(jb1);
                repository.Create(jb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                repository.Attach(jordan, new Relation("author", jb1));
                repository.Attach(jordan, new Relation("author", jb2));
                #endregion

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                var res = repository.Search(query);

                Assert.AreEqual(2, res.Count());
                Assert.AreEqual(2, repository.Count(query));

                //greater then
                EntityQuery2 q = new EntityQuery2("book");
                q.WhereGreaterThen("price", 19.0m);
                Assert.AreEqual(3, repository.Search(q).Count());
                Assert.AreEqual(3, repository.Count(q));

                //less then
                q = new EntityQuery2("book");
                q.WhereLessThen("price", 20.0m);
                Assert.AreEqual(2, repository.Search(q).Count());
                Assert.AreEqual(2, repository.Count(q));

                //is boolean
                q = new EntityQuery2("author");
                q.WhereIs("isalive", false);
                var r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //is string (ignore case)
                q = new EntityQuery2("author");
                q.WhereIs("lastname", "jordan");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //starts with
                q = new EntityQuery2("author");
                q.WhereStartsWith("firstname", "ra");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //ends with
                q = new EntityQuery2("book");
                q.WhereEndsWith("title", "world");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //less then
                q = new EntityQuery2("book");
                q.WhereAnyOf("id", new object[] { fb1.Id, jb1.Id, jb2.Id });
                Assert.AreEqual(3, repository.Search(q).Count());
                Assert.AreEqual(3, repository.Count(q));

                //between decimal
                q = new EntityQuery2("book");
                q.WhereBetween("price", 19.0m, 22.0m);
                Assert.AreEqual(2, repository.Search(q).Count());
                Assert.AreEqual(2, repository.Count(q));

                //between datetime
                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                q.WhereIs("isalive", true);
                Assert.AreEqual(0, repository.Search(q).Count());
                Assert.AreEqual(0, repository.Count(q));

                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1960, 1, 1), new DateTime(1970, 1, 1));
                q.WhereIs("isalive", true);
                q.WhereStartsWith("firstname", "ra");
                Assert.AreEqual(1, repository.Search(q).Count());
                Assert.AreEqual(1, repository.Count(q));
            }
        }
Exemple #21
0
        public void Test_EntityRepo_SearchWithRels()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                query.Include("book", "author");
                var res = repository.Search(query);

                Assert.AreEqual(2, res.Count());
                var rf = res.Single(e => e.Id == feist.Id);
                var rj = res.Single(e => e.Id == jordan.Id);

                Assert.AreEqual(1, rf.RelationsData.Count);
                var books = rf.GetManyRelations("book", "author");
                Assert.AreEqual(2, books.Count());
                foreach (var r in books)
                {
                    var orig = r.Entity.Id == fb1.Id ? fb1 : fb2;
                    foreach (var pm in dms.Domain.Entities["book"].Properties)
                    {
                        if (orig.Data.ContainsKey(pm.Name))
                            Assert.AreEqual(orig.Data[pm.Name], r.Entity.Data[pm.Name]);
                    }
                }

                Assert.AreEqual(0, rj.RelationsData.Count);
                Assert.AreEqual(0, rj.GetManyRelations("book", "author").Count());

                //TODO: rules!

                //repository.Complete();
            }
        }
Exemple #22
0
        public void Test_EntityRepo_Update()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                #endregion

                jordan.LastName += "EDIT";
                jordan.IsAlive = true;
                jordan.Born = jordan.Born.AddDays(2.0);
                jordan.Rating -= 0.5m;

                repository.Update(jordan);
                var q = new EntityQuery2("author", jordan.Id);
                q.AddProperties("lastname", "isalive", "born", "rating");
                var e = repository.Read(q);
                var updated = new Author(e);
                Assert.AreEqual(jordan.LastName, updated.LastName);
                Assert.AreEqual(jordan.Born, updated.Born);
                Assert.AreEqual(jordan.IsAlive, updated.IsAlive);
                Assert.AreEqual(jordan.Rating, updated.Rating);
            }
        }
Exemple #23
0
        public void Test_EntityRepo_ReadWithRel()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                EntityQuery2 q = new EntityQuery2("book", fb1.Id);
                q.AddProperties("title", "price");
                q.Include("author", "author");
                var e = repository.Read(q);
                Assert.AreEqual(2, e.Data.Count);
                foreach (var p in q.Properties)
                {
                    Assert.AreEqual(fb1.Data[p], e.Data[p]);
                }

                Assert.AreEqual(1, e.RelationsData.Count);
                var authorRel = e.GetSingleRelation("author", "author");
                foreach (var d in feist.Data)
                {
                    Assert.AreEqual(d.Value, authorRel.Entity.Data[d.Key]);
                }

                //repository.Complete();
            }
        }
Exemple #24
0
        public void Test_EntityRepo_SearchRelated()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                var jb1 = new Book()
                {
                    Title = "The Shadow is Rising",
                    Price = 21.15m
                };
                var jb2 = new Book()
                {
                    Title = "The Eye of the World",
                    Price = 25.80m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Create(jb1);
                repository.Create(jb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                repository.Attach(jordan, new Relation("author", jb1));
                repository.Attach(jordan, new Relation("author", jb2));
                #endregion

                EntityQuery2 query = new EntityQuery2("book");
                query.AllProperties = true;
                var fq = new RelationQuery("author", "author", feist.Id);
                query.RelatedTo.Add(fq);
                var res = repository.Search(query);
                Assert.AreEqual(2, res.Count());
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb1.Title));
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb2.Title));

                query.Include("author", "author");
                res = repository.Search(query);
                Assert.AreEqual(2, res.Count());
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb1.Title));
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb2.Title));
            }
        }
Exemple #25
0
        public void Test_EntityRepo_Read()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                jordan.Id = repository.Create(jordan);
                Assert.IsTrue(jordan.Id > 0);
                feist.Id = repository.Create(feist);
                Assert.IsTrue(feist.Id > 0);

                EntityQuery2 q = new EntityQuery2("author", jordan.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                var e = repository.Read(q);
                Assert.AreEqual(5, e.Data.Count);
                foreach (var p in q.Properties)
                {
                    Assert.AreEqual(jordan.Data[p], e.Data[p]);
                }
            }
        }
Exemple #26
0
        //[TestMethod]
        public void Test_EntityRepo_Perf()
        {
            var r = new Random();
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                for (int i = 0; i < 1000; i++)
                {
                    EntityUpdate update = new EntityUpdate("author");
                    update.Set("firstname", "Robert" + i);
                    update.Set("lastname", "Jordan");
                    update.Set("isalive", false);
                    update.Set("Born", new DateTime(1948, 10, 17));
                    update.Set("Rating", 5.5m + r.Next(4));
                    var id = repository.Create(update.ToEntity());
                    Assert.IsTrue(id > 0);

                    EntityQuery2 q = new EntityQuery2("author", id);
                    q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                    var e = repository.Read(q);
                    foreach (var pu in update.PropertyUpdates)
                    {
                        Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }

                    EntityUpdate update2 = new EntityUpdate(e.Name, e.Id);
                    update2.Set("rating", 5.5m + r.Next(4));
                    update2.Set("lastname", e.Data["lastname"] + "_EDIT");

                    repository.Update(update2.ToEntity());
                    e = repository.Read(q);
                    foreach (var pu in update2.PropertyUpdates)
                    {
                        Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }
                    foreach (var pu in update.PropertyUpdates)
                    {
                        if (!pu.Key.Equals("rating", StringComparison.InvariantCultureIgnoreCase) && !pu.Key.Equals("lastname", StringComparison.InvariantCultureIgnoreCase))
                            Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }

                }

                ctx.Complete();
            }

            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var qAll = new EntityQuery2("Author");
                var all = repository.Search(qAll);
                Assert.AreEqual(1000, all.Count());
                foreach (var a in all)
                    repository.Delete(a);
                Assert.AreEqual(0, repository.Search(qAll).Count());
            }
        }
Exemple #27
0
        public void Test_EntityRepo_Paging()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data

                int aCnt = 20;
                int bCnt = 3;
                for (int i = 0; i < aCnt; i++)
                {
                    var a = new Author()
                    {
                        FirstName = "Fname" + i,
                        LastName = "Lname" + i,
                        Born = DateTime.Now.AddYears(-20).AddDays(i),
                        NumberOfAwards = i / 3
                    };
                    repository.Create(a);
                    for (int j = 0; j < bCnt; j++)
                    {
                        var b = new Book()
                        {
                            Title = string.Format("Book_{0}_{1}", i, j),
                            Genre = Genre.SciFi,
                            Price = 10.0m + j,
                            ISBN = string.Format("{0}_{1}", a.LastName, j)
                        };
                        repository.Create(b);
                        repository.Attach(b, new Relation("author", a));
                    }
                }

                Assert.AreEqual(aCnt, repository.Search(new EntityQuery2("author")).Count());
                Assert.AreEqual(aCnt * bCnt, repository.Search(new EntityQuery2("book")).Count());

                #endregion

                var query = new EntityQuery2("Author");
                query.AddProperties("FirstName", "LastName");
                query.Include("book", "author");
                query.Paging = new Paging(1, 10);
                var res = repository.Search(query);
                Assert.AreEqual(10, res.Count());
                int idx = 0;
                foreach (var r in res)
                {
                    Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname"));
                    var books = r.GetManyRelations("book", "author");
                    int bidx = 0;
                    foreach (var b in books)
                    {
                        Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title"));
                    }
                    idx++;
                }

                //assert second page
                query.Paging.Page++;
                res = repository.Search(query);
                Assert.AreEqual(10, res.Count());
                foreach (var r in res)
                {
                    Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname"));
                    var books = r.GetManyRelations("book", "author");
                    int bidx = 0;
                    foreach (var b in books)
                    {
                        Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title"));
                    }
                    idx++;
                }
            }
        }
Exemple #28
0
        public void Test_EntityRepo_Delete()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            bool ex = false;
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                //var jb1 = new Book()
                //{
                //    Title = "The Wheel of Time",
                //    Price = 21.0m
                //};

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                //repository.Create(jb1);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                //repository.Attach(jordan, new Relation("author", jb1));

                #endregion

                var r1 = new EntityQuery2("author", jordan.Id);
                Assert.IsNotNull(repository.Read(r1));
                repository.Delete(jordan);
                Assert.IsNull(repository.Read(r1));

                var r2 = new EntityQuery2("author", feist.Id);
                Assert.IsNotNull(repository.Read(r2));

                try { repository.Delete(feist); }
                catch (Exception) { ex = true; }
            }
            Assert.IsTrue(ex, "Exception not thrown when deleting author with attached relations");
        }
Exemple #29
0
        public void Test_EntityRepo_CreateSequences()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m,
                    Genre = Genre.Fantasy
                };

                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                var order = new Entity("Order");
                order.SetData<decimal>("total", fb1.Price + fb2.Price);
                order.SetData<DateTime>("createdon", DateTime.Now);
                repository.Create(order);
                repository.Attach(order, new Relation("ordered", fb1));
                repository.Attach(order, new Relation("ordered", fb2));

                var q = new EntityQuery2("order", order.Id);
                q.AllProperties = true;
                var saved = repository.Read(q);
                Assert.AreEqual(string.Format("BO-1-{0}", DateTime.Now.Year), saved.GetData<string>("number"));

                var order2 = new Entity("Order");
                order2.SetData<decimal>("total", fb1.Price);
                order2.SetData<DateTime>("createdon", DateTime.Now);
                repository.Create(order2);
                repository.Attach(order2, new Relation("ordered", fb1));

                q = new EntityQuery2("order", order2.Id);
                q.AllProperties = true;
                saved = repository.Read(q);
                Assert.AreEqual(string.Format("BO-2-{0}", DateTime.Now.Year), saved.GetData<string>("number"));
            }
        }