fs = require('fs')
async = require('async')
assert = require('assert')
yaml = require('js-yaml')
$tableName = 'test_hash_range'

var dynalite = require('dynalite'),
dynaliteServer = dynalite({ createTableMs: 50, db: require('memdown')})
dynaliteServer.listen(4567, function(err) {
	if (err) throw err
})

var AWS = require('aws-sdk')
DynamoDB = require('../../lib/dynamodb')( new AWS.DynamoDB({endpoint: 'http://localhost:4567', "accessKeyId": "akid", "secretAccessKey": "secret", "region": "us-east-1" }))

DynamoDB.on('error', function(op, error, payload ) {
	//console.log(op,error,JSON.stringify(payload))
})
DynamoDB.on('beforeRequest', function(op, payload ) {
	//console.log("--------------------------------")
	//console.log(op,payload)
})

query_handler = function( idx, yml ) {
	return function(done) {
		if (yml.Tests.query[idx].log === true)
			global.DDBSQL = true
		else
			global.DDBSQL = false

		DynamoSQL.query( yml.Tests.query[idx].query, function(err, data ) {
			if (yml.Tests.query[idx].shouldFail) {
				if (err) {
					if (!(yml.Tests.query[idx].validations || []).length)
						return done()

					yml.Tests.query[idx].validations.forEach(function(el) {
						assert.deepEqual(eval( el.key ), eval( el.value ))
					})
					done()
				}
			} else {
				if (err)
					throw err

				if (yml.Tests.query[idx].log === true)
					console.log("result=", JSON.stringify(data, null, "\t"))

				if (yml.Tests.query[idx].results)
					assert.equal(data.length, yml.Tests.query[idx].results)

				if (yml.Tests.query[idx].validations) {
					yml.Tests.query[idx].validations.forEach(function(el) {
						assert.deepEqual(eval( el.key ), eval( el.value ))
					})
				}
				done()
			}
		})
	}
}

before_test = function(data) {
	return function(done) {
		async.each(data, function(q, cb ) {
			DynamoSQL.query(q, {}, cb )
		}, function(err) {
			if (err)
				throw err

			done()
		})
	}
}

run_test = function(test_name, yml_file ) {
	
	describe(test_name, function () {
		var yml = yaml.safeLoad(fs.readFileSync(yml_file, 'utf8'))
		before(before_test(yml.Prepare.Data))
		// beforeEach

		yml.Tests.query.forEach(function(v,k) {
			it(yml.Tests.query[k].title || yml.Tests.query[k].query, query_handler(k, yml ) )
			
		})
	})
}
@@ -908,27 +908,26 @@
 se%0A%0A%09%09Dynamo
-SQL
+DB
 .query( yml.
@@ -1807,19 +1807,18 @@
 %09%09Dynamo
-SQL
+DB
 .query(q
@@ -2039,16 +2039,44 @@
 utf8'))%0A
+%09%09console.log(%22yml=%22, yml )%0A
 %09%09before

read tests from yaml