Ejemplo n.º 1
0
        public async Task CacheKeyIsNormalised()
        {
            // Arrange
            var cache = new Mock <ICache <string> >();

            cache.Setup(c => c.TryGet(It.IsAny <string>()))
            .ReturnsAsync((false, null));
            var client = new Mock <IFunTranslationsApiClient>();

            client.Setup(c => c.ShakespeareTranslation(It.IsAny <string>()))
            .ReturnsAsync(new FunTranslationResponse(new FunTranslationResponseContents("translated")));

            var query = new TranslationQuery(cache.Object, client.Object, NullLogger <TranslationQuery> .Instance);

            var description = @"It was created by
            a scientist after
            years of horrific
            gene splicing and
            DNA engineering
            experiments.";

            // Act
            var translated = await query.Translate(Translation.Shakespeare, description);

            // Assert
            Assert.Equal("translated", translated);
            cache.Verify(c => c.TryGet("Shakespeare-It was created by a scientist after years of horrific gene splicing and DNA engineering experiments."), Times.Once);
            cache.Verify(c => c.Set("Shakespeare-It was created by a scientist after years of horrific gene splicing and DNA engineering experiments.", translated), Times.Once);
        }
Ejemplo n.º 2
0
 public IEnumerable <KeyValuePair <string, string> > GetKeyValue(TranslationQuery query)
 {
     return((from t in _context.Translation
             where t.ClientKey == query.ClientKey || query.ClientKey == null
             where t.LanguageKey == query.LanguageKey || query.LanguageKey == null
             where t.ContainerKey == query.ContainerKey || query.ContainerKey == null
             where t.Key == query.TranslationKey || query.TranslationKey == null
             select new KeyValuePair <string, string>(t.Key, t.Text)).ToArray());
 }
Ejemplo n.º 3
0
 //
 // GET translation
 //
 public IEnumerable <Translation> Get(TranslationQuery query)
 {
     return((from t in _context.Translation
             where  t.ClientKey == query.ClientKey || query.ClientKey == null
             where  t.LanguageKey == query.LanguageKey || query.LanguageKey == null
             where  t.ContainerKey == query.ContainerKey || query.ContainerKey == null
             where  t.Key == query.TranslationKey || query.TranslationKey == null
             select t).ToArray());
 }
Ejemplo n.º 4
0
        public IActionResult Update([FromQuery] TranslationQuery query, [FromBody] Translation translation)
        {
            if (query == null || translation == null || query.ClientKey != translation.ClientKey ||
                query.LanguageKey != translation.LanguageKey ||
                query.ContainerKey != translation.ContainerKey ||
                query.TranslationKey != translation.Key)
            {
                return(BadRequest());
            }

            return(_translationRepo.Update(query, translation));
        }
Ejemplo n.º 5
0
        public IActionResult Delete([FromQuery] TranslationQuery query)
        {
            if (query == null || query.ClientKey == null ||
                query.LanguageKey == null ||
                query.ContainerKey == null ||
                query.TranslationKey == null)
            {
                return(BadRequest());
            }

            return(_translationRepo.Delete(query));
        }
Ejemplo n.º 6
0
        public IActionResult Delete(TranslationQuery query)
        {
            var translation = _context.Translation.First(
                t => t.ClientKey == query.ClientKey &&
                t.LanguageKey == query.LanguageKey &&
                t.ContainerKey == query.ContainerKey &&
                t.Key == query.TranslationKey);

            if (translation == null)
            {
                return(new StatusCodeResult(404));
            }

            _context.Translation.Remove(translation);
            _context.SaveChanges();
            return(new StatusCodeResult(200)); // OK
        }
Ejemplo n.º 7
0
        public async Task ValueRetrievedFromCacheIfAvailable(Translation translation)
        {
            // Arrange
            var cache = new Mock <ICache <string> >();

            cache.Setup(c => c.TryGet(It.IsAny <string>()))
            .ReturnsAsync((true, "cached-description"));
            var client = new Mock <IFunTranslationsApiClient>();

            var query = new TranslationQuery(cache.Object, client.Object, NullLogger <TranslationQuery> .Instance);

            // Act
            var translated = await query.Translate(translation, "some-description");

            // Assert
            Assert.Equal("cached-description", translated);
            cache.Verify(c => c.TryGet($"{translation}-some-description"), Times.Once);
            client.Verify(c => c.YodaTranslation(It.IsAny <string>()), Times.Never);
            client.Verify(c => c.ShakespeareTranslation(It.IsAny <string>()), Times.Never);
        }
Ejemplo n.º 8
0
        public IActionResult Update(TranslationQuery query, Translation translation)
        {
            var _translation = _context.Translation.First(
                t => t.ClientKey == query.ClientKey &&
                t.LanguageKey == query.LanguageKey &&
                t.ContainerKey == query.ContainerKey &&
                t.Key == query.TranslationKey);

            if (_translation == null)
            {
                return(new StatusCodeResult(404));
            }

            _translation.Key        = translation.Key;
            _translation.Text       = translation.Text;
            _translation.IsComplete = translation.IsComplete;

            _context.Translation.Update(_translation);
            _context.SaveChanges();
            return(new StatusCodeResult(204));
        }
Ejemplo n.º 9
0
        public async Task ShakespeareTranslationValueSetInCacheOnceRetrieved()
        {
            // Arrange
            var cache = new Mock <ICache <string> >();

            cache.Setup(c => c.TryGet(It.IsAny <string>()))
            .ReturnsAsync((false, null));
            var client = new Mock <IFunTranslationsApiClient>();

            client.Setup(c => c.ShakespeareTranslation(It.IsAny <string>()))
            .ReturnsAsync(new FunTranslationResponse(new FunTranslationResponseContents("translated")));

            var query = new TranslationQuery(cache.Object, client.Object, NullLogger <TranslationQuery> .Instance);

            // Act
            var translated = await query.Translate(Translation.Shakespeare, "some-description");

            // Assert
            Assert.Equal("translated", translated);
            cache.Verify(c => c.Set("Shakespeare-some-description", translated), Times.Once);
        }
 public static ITranslationQueryInitialized <TEntity> WithLanguage <TEntity>([NotNull] this IQueryable <TEntity> source, params object[] languageKey)
     where TEntity : class
 => TranslationQuery <TEntity> .Initialize(source, languageKey);
Ejemplo n.º 11
0
 public IEnumerable <KeyValuePair <string, string> > GetKeyValue([FromQuery] TranslationQuery query)
 {
     return(_translationRepo.GetKeyValue(query));
 }
Ejemplo n.º 12
0
 public IEnumerable <Translation> Get([FromQuery] TranslationQuery query)
 {
     return(_translationRepo.Get(query));
 }