Пример #1
0
        public void Objects()
        {
            Assert.Throws <ArgumentNullException>(() => _index.SetSettings(null));
            Assert.Throws <ArgumentNullException>(() => _index.GetObject <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.GetObjects <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.Browse <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.BrowseFrom <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.PartialUpdateObject <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.SaveObject <object>(null));

            Assert.Throws <ArgumentNullException>(() => _index.SaveObjects <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.ReplaceAllObjects <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.Batch <object>(operations: null));
            Assert.Throws <ArgumentNullException>(() => _index.Batch <object>(request: null));
            Assert.Throws <ArgumentNullException>(() => _index.DeleteObject(null));
            Assert.Throws <ArgumentNullException>(() => _index.DeleteBy(null));
            Assert.Throws <ArgumentNullException>(() => _index.SearchForFacetValue(null));
            Assert.Throws <ArgumentNullException>(() => _index.Search <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.CopyTo(""));
            Assert.Throws <ArgumentNullException>(() => _index.MoveFrom(""));

            Assert.Throws <ArgumentException>(() => _index.SaveObject <object>(new List <object> {
            }));
            Assert.Throws <ArgumentException>(() => _index.PartialUpdateObject <object>(new List <object> {
            }));
        }
Пример #2
0
        public List <Item> RandomItems()
        {
            SearchClient         client        = new SearchClient("BIW6EL1FTD", "a5af55b1831c11747c108cc179f2d790");
            SearchIndex          index         = client.InitIndex("Items");
            IndexIterator <Item> indexIterator = index.Browse <Item>(new BrowseIndexQuery {
            });

            var hits = new List <Item>();

            int max = 0;

            foreach (var hit in indexIterator)
            {
                if (hit.ImageURL != "null")
                {
                    hits.Add(hit);
                }

                if (max > 100)
                {
                    break;
                }
                else
                {
                    max++;
                }
            }
            Random rnd = new Random();

            return(hits.Where(t => t.ImageURL.ToLower() != "null").OrderBy(x => rnd.Next()).Take(20).ToList());
        }
Пример #3
0
        public void Objects()
        {
            Assert.Throws <ArgumentNullException>(() => _index.SetSettings(null));
            Assert.Throws <ArgumentNullException>(() => _index.GetObject <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.GetObjects <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.Browse <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.BrowseFrom <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.PartialUpdateObject <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.SaveObject <object>(null));

            Assert.Throws <ArgumentNullException>(() => _index.SaveObjects <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.ReplaceAllObjects <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.Batch <object>(operations: null));
            Assert.Throws <ArgumentNullException>(() => _index.Batch <object>(request: null));
            Assert.That(Assert.Throws <ArgumentNullException>(() => _index.DeleteObject(null)).Message, Is.EqualTo("Argument parameter cannot be null (Parameter 'objectId')"));
            Assert.That(Assert.Throws <ArgumentNullException>(() => _index.DeleteObjects(null)).Message, Is.EqualTo("Argument parameter cannot be null (Parameter 'objectIds')"));
            Assert.Throws <ArgumentNullException>(() => _index.DeleteBy(null));
            Assert.Throws <ArgumentNullException>(() => _index.SearchForFacetValue(null));
            Assert.Throws <ArgumentNullException>(() => _index.Search <object>(null));
            Assert.Throws <ArgumentNullException>(() => _index.CopyTo(""));
            Assert.Throws <ArgumentNullException>(() => _index.MoveFrom(""));

            Assert.Throws <ArgumentException>(() => _index.SaveObject <object>(new List <object>()));
            Assert.Throws <ArgumentException>(() => _index.PartialUpdateObject <object>(new List <object>()));
        }
Пример #4
0
        public (List <Brand>, List <ProductCategory>, List <Product>) ListProducts()
        {
            SearchIndex indexToGetDataFrom = _client.InitIndex("dev_product2");

            var algoliaProductVMs = indexToGetDataFrom.Browse <AlgoliaProductVM>(new Algolia.Search.Models.Common.BrowseIndexQuery()).Take(100).ToList();

            var brandsFromAlgolia            = algoliaProductVMs.Select(x => x.Brand).Distinct().ToList();
            var productCategoriesFromAlgolia = algoliaProductVMs.Select(x => x.Category).Distinct().ToList();

            var products          = new List <Product>();
            var brands            = new List <Brand>();
            var productCategories = new List <ProductCategory>();

            foreach (var brand in brandsFromAlgolia)
            {
                brands.Add(new Brand(brand));
            }
            foreach (var productCategory in productCategoriesFromAlgolia)
            {
                productCategories.Add(new ProductCategory(productCategory));
            }

            foreach (var productVM in algoliaProductVMs)
            {
                var brand           = brands.FirstOrDefault(z => z.Name == productVM.Brand);
                var productCategory = productCategories.FirstOrDefault(z => z.Name == productVM.Category);

                var product = new Product("b74f5c89-cd7d-4e5e-9179-99b4c1e1ab12", productVM.Name, productVM.Barcode, productVM.ImgUrl, productVM.AvailableToSell, brand.Id.ToString(), productCategory.Id.ToString());
                foreach (var unit in productVM.Units)
                {
                    product.AddUnitToProduct(unit.Name, 1, 1, unit.Price, unit.ConsumerPrice, 1, unit.IsAvailable);
                }
                products.Add(product);
            }

            return(brands, productCategories, products);
        }
        public async Task IndexOperationsAsyncTest()
        {
            // AddObject with ID
            var objectOne = new AlgoliaStub {
                ObjectId = "one"
            };
            var addObject = _index.SaveObjectAsync(objectOne);

            // AddObject without ID
            var objectWoId    = new AlgoliaStub();
            var addObjectWoId = _index.SaveObjectAsync(objectWoId, autoGenerateObjectId: true);

            // Save two objects with objectID
            var objectsWithIds = new List <AlgoliaStub>
            {
                new AlgoliaStub {
                    ObjectId = "two"
                },
                new AlgoliaStub {
                    ObjectId = "three"
                }
            };

            var addObjects = _index.SaveObjectsAsync(objectsWithIds);

            // Save two objects w/o objectIDs
            var objectsWoId = new List <AlgoliaStub>
            {
                new AlgoliaStub {
                    Property = "addObjectsWoId"
                },
                new AlgoliaStub {
                    Property = "addObjectsWoId"
                }
            };

            var addObjectsWoId = _index.SaveObjectsAsync(objectsWoId, autoGenerateObjectId: true);

            // Batch 1000 objects
            var objectsToBatch = new List <AlgoliaStub>();
            var ids            = new List <string>();

            for (int i = 0; i < 1000; i++)
            {
                var id = (i + 1).ToString();
                objectsToBatch.Add(new AlgoliaStub {
                    ObjectId = id, Property = $"Property{id}"
                });
                ids.Add(id);
            }

            var batch = _index.SaveObjectsAsync(objectsToBatch);

            // Wait for all http call to finish
            var responses = await Task.WhenAll(new[] { addObject, addObjectWoId, addObjects, addObjectsWoId, batch }).ConfigureAwait(false);

            // Wait for Algolia's task to finish (indexing)
            responses.Wait();

            // Six first records
            var generatedId = addObjectWoId.Result.Responses[0].ObjectIDs.ToList();

            objectWoId.ObjectId = generatedId.ElementAt(0);

            var generatedIDs = addObjectsWoId.Result.Responses[0].ObjectIDs.ToList();

            objectsWoId[0].ObjectId = generatedIDs.ElementAt(0);
            objectsWoId[1].ObjectId = generatedIDs.ElementAt(1);

            var settedIds = new List <string> {
                "one", "two", "three"
            };

            var sixFirstRecordsIds = settedIds.Concat(generatedId).Concat(generatedIDs).ToList();
            var sixFirstRecords    = (await _index.GetObjectsAsync <AlgoliaStub>(sixFirstRecordsIds)).ToList();

            Assert.True(sixFirstRecords.Count() == 6);

            var objectsToCompare = new List <AlgoliaStub> {
                objectOne
            }.Concat(objectsWithIds)
            .Concat(new List <AlgoliaStub> {
                objectWoId
            })
            .Concat(objectsWoId)
            .ToList();

            // Check retrieved objects againt original content
            Parallel.For(0, sixFirstRecords.Count, i =>
            {
                Assert.True(TestHelper.AreObjectsEqual(sixFirstRecords[i], objectsToCompare[i]));
            });

            // 1000 records
            var batchResponse = (await _index.GetObjectsAsync <AlgoliaStub>(ids)).ToList();

            Assert.True(batchResponse.Count() == 1000);

            // Check retrieved objects againt original content
            Parallel.For(0, batchResponse.Count, i =>
            {
                Assert.True(TestHelper.AreObjectsEqual(objectsToBatch[i], batchResponse[i]));
            });

            // Browse all index to assert that we have 1006 objects
            var objectsBrowsed = new List <AlgoliaStub>();

            foreach (var item in _index.Browse <AlgoliaStub>(new BrowseIndexQuery()))
            {
                objectsBrowsed.Add(item);
            }

            Assert.True(objectsBrowsed.Count() == 1006);

            // Update one object
            var objectToPartialUpdate = objectsToBatch.ElementAt(0);

            objectToPartialUpdate.Property = "PartialUpdated";

            var partialUpdateObject = await _index.PartialUpdateObjectAsync(objectToPartialUpdate);

            partialUpdateObject.Wait();

            var getUpdatedObject = await _index.GetObjectAsync <AlgoliaStub>(objectToPartialUpdate.ObjectId);

            Assert.True(getUpdatedObject.Property.Equals(objectToPartialUpdate.Property));

            // Update two objects
            var objectToPartialUpdate1 = objectsToBatch.ElementAt(1);

            objectToPartialUpdate1.Property = "PartialUpdated1";
            var objectToPartialUpdate2 = objectsToBatch.ElementAt(2);

            objectToPartialUpdate2.Property = "PartialUpdated2";

            var partialUpdateObjects = await _index.PartialUpdateObjectsAsync(new List <AlgoliaStub>
            {
                objectToPartialUpdate1,
                objectToPartialUpdate2
            });

            partialUpdateObjects.Wait();

            var getUpdatedObjects = (await _index.GetObjectsAsync <AlgoliaStub>(new List <string>
            {
                objectToPartialUpdate1.ObjectId,
                objectToPartialUpdate2.ObjectId
            })).ToList();

            Assert.True(getUpdatedObjects.ElementAt(0).Property.Equals(objectToPartialUpdate1.Property));
            Assert.True(getUpdatedObjects.ElementAt(1).Property.Equals(objectToPartialUpdate2.Property));

            // Delete six first objects
            var deleteObjects = await _index.DeleteObjectsAsync(sixFirstRecordsIds);

            // Assert that the objects were deleted
            var objectsBrowsedAfterDelete = new List <AlgoliaStub>();

            deleteObjects.Wait();
            foreach (var item in _index.Browse <AlgoliaStub>(new BrowseIndexQuery()))
            {
                objectsBrowsedAfterDelete.Add(item);
            }

            Assert.True(objectsBrowsedAfterDelete.Count() == 1000);

            // Delete remaining objects
            var deleteRemainingObjects = await _index.DeleteObjectsAsync(ids);

            deleteRemainingObjects.Wait();

            // Assert that all objects were deleted
            var search = await _index.SearchAsync <AlgoliaStub>(new Query(""));

            Assert.True(search.Hits.Count == 0);
        }