我有using Microsoft.VisualStudio.TestTools.UnitTesting;
using Npgsql;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
namespace TestCases
{
[TestClass]
public class ServiceTests
{
readonly string connectionString = "Server=127.0.0.1;Port=5432;Database=test_db;User Id=postgres;Password=postgres;MaxPoolSize=10;Pooling=true;";
private Service service;
[TestInitialize]
public async Task Initialize()
{
service = new Service(this.connectionString);
using (var conn = new NpgsqlConnection(this.connectionString))
{
await conn.OpenAsync();
var query = "CREATE TABLE IF NOT EXISTS data(value varchar(255));";
var cmd = new NpgsqlCommand(query, conn);
await cmd.ExecuteNonQueryAsync();
}
}
[TestCleanup]
public async Task Cleanup()
{
using (var conn = new NpgsqlConnection(this.connectionString))
{
await conn.OpenAsync();
var query = "DROP TABLE IF EXISTS data;";
var cmd = new NpgsqlCommand(query, conn);
await cmd.ExecuteNonQueryAsync();
}
}
/// <summary>
/// Failing with prepared PG 55000
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task CanAddConcurrently()
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await Task.WhenAll(
Enumerable.Range(1, 10).Select(async i =>
{
var val = string.Format("CanAddConcurrently_Q{0};", i);
await service.Add(val);
})
);
scope.Complete();
}
using (var conn = new NpgsqlConnection(this.connectionString))
{
await conn.OpenAsync();
var query = "select count(*) from data WHERE value like 'CanAddConcurrently_Q%';";
var cmd = new NpgsqlCommand(query, conn);
long count = (long)await cmd.ExecuteScalarAsync();
Assert.AreEqual((long)100, count);
}
}
/// <summary>
/// Timing out
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task DoesNotTimeout()
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await Task.WhenAll(
Enumerable.Range(1, 100).Select(async i =>
{
var val = string.Format("DoesNotTimeout_Q{0};", i);
await service.Add(val);
})
);
scope.Complete();
}
using (var conn = new NpgsqlConnection(this.connectionString))
{
await conn.OpenAsync();
var query = "select count(*) from data WHERE value like 'DoesNotTimeout_Q%';";
var cmd = new NpgsqlCommand(query, conn);
long count = (long)await cmd.ExecuteScalarAsync();
Assert.AreEqual((long)100, count);
}
}
/// <summary>
/// Passes OK
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task CanAddSequentially()
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
for (long i = 0; i < 100; i++)
{
var val = string.Format("CanAddSequentially_Q{0};", i);
await service.Add(val);
}
scope.Complete();
}
using (var conn = new NpgsqlConnection(this.connectionString))
{
await conn.OpenAsync();
var query = "select count(*) from data WHERE value like 'CanAddSequentially_Q%';";
var cmd = new NpgsqlCommand(query, conn);
long count = (long)await cmd.ExecuteScalarAsync();
Assert.AreEqual((long)100, count);
}
}
/// <summary>
/// Passes OK
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task RollsBackIfError()
{
try
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
for (long i = 0; i < 100; i++)
{
var val = string.Format("RollsBackIfError_Q{0};", i);
if (i == 99)
{
val = val.PadRight(300, 'e'); // trigger error on last item
}
await service.Add(val);
}
scope.Complete();
}
}
catch (Exception ex)
{
Assert.IsInstanceOfType(ex, typeof(NpgsqlException));
}
using (var conn = new NpgsqlConnection(this.connectionString))
{
await conn.OpenAsync();
var query = "select count(*) from data WHERE value like 'RollsBackIfError_Q%';";
var cmd = new NpgsqlCommand(query, conn);
long count = (long)await cmd.ExecuteScalarAsync();
Assert.AreEqual((long)0, count);
}
}
}
}
我有ul列表
let prodObject = {schema: {}};
我想为prodObject.schema对象中列表中的每个项目添加对象。 我要使最终对象看起来像这样。
<ul class="product-lists">
<li>CD Covers</li>
<li>DVD Covers</li>
<li>Cards</li>
<li>Posters</li>
<li>Cups</li>
</ul>
答案 0 :(得分:1)
有两个部分。使用querySelectorAll选择所有元素,然后使用replace,toLowerCase和toUpperCase将对象键的标题转换为camelCase:
const prodObject = {schema:{}};
Array.from( document.querySelectorAll( '.product-lists li' ), el => el.innerText ).forEach( title => {
const camelCaseTitle = title.replace( /^\w*/, x => x.toLowerCase( ) ).replace( /\s+(\w)(\w*)/, (_,f,r) => f.toUpperCase() + r.toLowerCase( ) );
prodObject.schema[ camelCaseTitle ] = { type: 'string', title };
} );
console.log( prodObject );
.product-lists {
display: none;
}
<ul class="product-lists">
<li>CD Covers</li>
<li>DVD Covers</li>
<li>Cards</li>
<li>Posters</li>
<li>Cups</li>
</ul>
答案 1 :(得分:0)
var prodObject = {schema: {}};
var productList = document.getElementsByClassName('product-lists')[0].children;
for (var i = 0; i < productList.length; i++) {
var product = productList[i];
var text = product.innerText;
var key = text.split(' ').map(function(el) {
return capitalizeFirstLetter(el);
}).join('');
key = key.charAt(0).toLowerCase() + key.slice(1);
prodObject.schema[key] = {
type: 'string',
title: text
};
}
console.log(prodObject);
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
<ul class="product-lists">
<li>CD Covers</li>
<li>DVD Covers</li>
<li>Cards</li>
<li>Posters</li>
<li>Cups</li>
</ul>