schemas FEATURE add schema getters from the context

Also check for the duplicities in the context when parsing a new schema.
diff --git a/tests/src/test_context.c b/tests/src/test_context.c
index d0fc202..992fdfa 100644
--- a/tests/src/test_context.c
+++ b/tests/src/test_context.c
@@ -14,6 +14,7 @@
 
 #include "tests/config.h"
 #include "../../src/context.c"
+#include "../../src/tree_schema.c"
 
 #include <stdarg.h>
 #include <stddef.h>
@@ -77,6 +78,7 @@
     struct ly_ctx *ctx;
     const char * const *list;
 
+    will_return_count(__wrap_ly_set_add, 0, 6);
     assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
 
     /* invalid arguments */
@@ -177,6 +179,8 @@
     (void) state; /* unused */
 
     struct ly_ctx *ctx;
+
+    will_return_always(__wrap_ly_set_add, 0);
     assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
 
     /* invalid arguments */
@@ -255,11 +259,13 @@
 {
     (void) state; /* unused */
 
+    struct ly_ctx *ctx;
+
     /* invalid arguments */
     assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
     logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
 
-    struct ly_ctx *ctx;
+    will_return_always(__wrap_ly_set_add, 0);
     assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
     assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
 
@@ -267,12 +273,71 @@
     ly_ctx_destroy(ctx, NULL);
 }
 
+static void
+test_get_models(void **state)
+{
+    (void) state; /* unused */
+
+    struct ly_ctx *ctx;
+    const struct lys_module *mod, *mod2;
+    const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
+    const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
+
+    will_return_always(__wrap_ly_set_add, 0);
+    assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
+
+    /* invalid arguments */
+    assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
+    logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
+    assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
+    logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
+    assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
+    logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
+    assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
+    logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
+    assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
+
+    /* internal modules */
+    assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
+    mod = ly_ctx_get_module_implemented(ctx, "yang");
+    assert_non_null(mod);
+    assert_non_null(mod->parsed);
+    assert_string_equal("yang", mod->parsed->name);
+    mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->parsed->ns);
+    assert_ptr_equal(mod, mod2);
+    assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
+    assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
+    assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
+    assert_non_null(ly_ctx_get_module(ctx, "ietf-datastores", "2017-08-17"));
+
+    /* select module by revision */
+    mod = lys_parse_mem(ctx, str1, LYS_IN_YANG);
+    /* invalid attempts - implementing module of the same name and inserting the same module */
+    assert_null(lys_parse_mem(ctx, str2, LYS_IN_YANG));
+    logbuf_assert("Module \"a\" is already implemented in the context.");
+    assert_null(lys_parse_mem_(ctx, str1, LYS_IN_YANG, NULL, 0));
+    logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
+    /* insert the second module only as imported, not implemented */
+    mod2 = lys_parse_mem_(ctx, str2, LYS_IN_YANG, NULL, 0);
+    assert_non_null(mod);
+    assert_non_null(mod2);
+    assert_ptr_not_equal(mod, mod2);
+    mod = ly_ctx_get_module_latest(ctx, "a");
+    assert_ptr_equal(mod, mod2);
+    mod2 = ly_ctx_get_module_latest_ns(ctx, mod->parsed->ns);
+    assert_ptr_equal(mod, mod2);
+
+    /* cleanup */
+    ly_ctx_destroy(ctx, NULL);
+}
+
 int main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test_setup(test_searchdirs, logger_setup),
         cmocka_unit_test_setup(test_options, logger_setup),
         cmocka_unit_test_setup(test_models, logger_setup),
+        cmocka_unit_test_setup(test_get_models, logger_setup),
     };
 
     return cmocka_run_group_tests(tests, NULL, NULL);
diff --git a/tests/src/test_hash_table.c b/tests/src/test_hash_table.c
index 1afeb83..05cd1f9 100644
--- a/tests/src/test_hash_table.c
+++ b/tests/src/test_hash_table.c
@@ -12,8 +12,7 @@
  *     https://opensource.org/licenses/BSD-3-Clause
  */
 
-#define _BSD_SOURCE
-#define _DEFAULT_SOURCE
+#include "common.h"
 
 #include "tests/config.h"
 #include "../../src/hash_table.c"
diff --git a/tests/src/test_parser_yang.c b/tests/src/test_parser_yang.c
index db73f63..8f5085c 100644
--- a/tests/src/test_parser_yang.c
+++ b/tests/src/test_parser_yang.c
@@ -785,7 +785,7 @@
                  assert_string_equal("RFC7950", mod->ref));
     /* revision */
     TEST_GENERIC("revision 2018-10-12;}", mod->revs,
-                 assert_string_equal("2018-10-12", mod->revs[0].rev));
+                 assert_string_equal("2018-10-12", mod->revs[0].date));
     /* rpc */
     TEST_GENERIC("rpc test;}", mod->rpcs,
                  assert_string_equal("test", mod->rpcs[0].name));
diff --git a/tests/src/test_set.c b/tests/src/test_set.c
index a6c53c4..982163f 100644
--- a/tests/src/test_set.c
+++ b/tests/src/test_set.c
@@ -11,9 +11,8 @@
  *
  *     https://opensource.org/licenses/BSD-3-Clause
  */
+#include "common.h"
 
-#define _BSD_SOURCE
-#define _DEFAULT_SOURCE
 #include <stdarg.h>
 #include <stddef.h>
 #include <setjmp.h>
diff --git a/tests/src/test_tree_schema_helpers.c b/tests/src/test_tree_schema_helpers.c
index 3dc7d7c..ac4e7bf 100644
--- a/tests/src/test_tree_schema_helpers.c
+++ b/tests/src/test_tree_schema_helpers.c
@@ -91,9 +91,9 @@
 
     /* revisions are stored in wrong order - the newest is the last */
     LY_ARRAY_NEW_RET(NULL, revs, rev,);
-    strcpy(rev->rev, "2018-01-01");
+    strcpy(rev->date, "2018-01-01");
     LY_ARRAY_NEW_RET(NULL, revs, rev,);
-    strcpy(rev->rev, "2018-12-31");
+    strcpy(rev->date, "2018-12-31");
 
     assert_int_equal(2, LY_ARRAY_SIZE(revs));
     assert_string_equal("2018-01-01", &revs[0]);
diff --git a/tests/src/test_xml.c b/tests/src/test_xml.c
index 568e55e..95d1302 100644
--- a/tests/src/test_xml.c
+++ b/tests/src/test_xml.c
@@ -12,8 +12,8 @@
  *     https://opensource.org/licenses/BSD-3-Clause
  */
 
-#define _BSD_SOURCE
-#define _DEFAULT_SOURCE
+#include "common.h"
+
 #include <stdarg.h>
 #include <stddef.h>
 #include <setjmp.h>