Fix strange condition after insert or query some triples, add checks and tests